gpt4 book ai didi

java - 如何使视频仅从按钮开始?

转载 作者:行者123 更新时间:2023-12-04 03:36:51 24 4
gpt4 key购买 nike

我是编码新手,现在我从 youtube 制作了一个背景视频,该视频在进入 Activity 后立即开始并循环播放(它永远不会停止)。 btStart 上设置了一个计时器,但这与我的要求无关。如何仅在按下 btStart 按钮后才开始播放视频?
我将 videoView.start() 放在 clickListener 中,但没有任何改变。
代码:

public class BreatheActivity extends AppCompatActivity {

private VideoView videoView;
MediaPlayer mediaPlayer;
int currentVideoPosition;

Button btStart;
ImageButton btOut;
ImageView xOut;
Chronometer chrono;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breathe);

xOut = findViewById(R.id.btOutX);
btOut = findViewById(R.id.btOut);
btStart = findViewById(R.id.btBreathe);
chrono = findViewById(R.id.breathe_chrono);

videoView = findViewById(R.id.video_breath);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.breathe);

videoView.setVideoURI(uri);


btStart.setOnClickListener(view -> {
videoView.start();

btStart.setVisibility(View.GONE);

chrono.setBase(SystemClock.elapsedRealtime());

chrono.start();

});

btOut.setOnClickListener(v -> {
BreatheActivity.super.onBackPressed();

});

videoView.setOnPreparedListener(mp -> {
mediaPlayer = mp;

mediaPlayer.setLooping(true);

if (currentVideoPosition != 0) {
mediaPlayer.seekTo(currentVideoPosition);
mediaPlayer.start();
}

});

}

@Override
protected void onPause() {
super.onPause();
currentVideoPosition = mediaPlayer.getCurrentPosition();
videoView.pause();
}

@Override
protected void onResume() {
super.onResume();
videoView.start();
}

@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
mediaPlayer = null;
}

}

最佳答案

我想你忘了在 btStart.setOnClickListener(view -> {}); 中移动 videoView.start();

btStart.setOnClickListener(view -> {
videoView.start();

btStart.setVisibility(View.GONE);

chrono.setBase(SystemClock.elapsedRealtime());

chrono.start();

});
Here 是和你做同样事情的人。

关于java - 如何使视频仅从按钮开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66724294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com