我正在制作一个非常简单的视频播放器。它利用VideoView和SeekBar。 为了处理播放/暂停呼叫,我在VideoView上使用了onTouchListener。
一切正常(播放,搜索),但是如果用户通过单击视频来暂停视频-则搜索栏将永久暂停:如果用户再次单击视频,则会继续播放,但搜索栏将不会继续关注它。除此之外-它可以按预期工作:用户仍可以滚动视频,但seekbar不再自行更新其位置。
我正在使用的代码如下。任何帮助将不胜感激。
public class VideoActivity extends Activity {
VideoView videoView;
SeekBar seekBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash1);
videoView = (VideoView) findViewById(R.id.videoView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
Uri video = Uri.parse("sdcard/video01.mp4");
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
seekBar.setMax(videoView.getDuration());
seekBar.postDelayed(onEverySecond, 1000);
}
});
videoView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent motionEvent)
{
if (videoView.isPlaying())
{
videoView.pause();
return false;
}
else
{
videoView.start();
return false;
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) {
videoView.seekTo(progress);
}
}
});
videoView.start();
}
private Runnable onEverySecond=new Runnable() {
@Override
public void run() {
if(seekBar != null) {
seekBar.setProgress(videoView.getCurrentPosition());
}
if(videoView.isPlaying()) {
seekBar.postDelayed(onEverySecond, 1000);
}
}
};
}