Android VideoView,如何理解播放已完成
在onCreate()方法中,我确实启动了一个线程来播放视频,我希望让线程理解视频播放已经完成 如何实现这一点 这是我的密码:Android VideoView,如何理解播放已完成,android,multithreading,android-videoview,Android,Multithreading,Android Videoview,在onCreate()方法中,我确实启动了一个线程来播放视频,我希望让线程理解视频播放已经完成 如何实现这一点 这是我的密码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mode1); videoView = (VideoView) findViewB
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mode1);
videoView = (VideoView) findViewById(R.id.videoView);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
continueLoop = true;
while (continueLoop) {
String[] allVideoFiles = {"video1.mp4", "video2.mp4", "video3.mp4", "video4.mp4", "video5.mp4"};
for (String video : allVideoFiles) {
final String v = video;
File f = new File("/blablabla" + video);
int duration = 0;
if (!f.exists()) {
continue;
}
duration = MediaPlayer.create(getApplicationContext(), Uri.fromFile(f)).getDuration();
runOnUiThread(new Runnable() {
@Override
public void run() {
videoView101.setVisibility(View.VISIBLE);
MediaController ctrl = new MediaController(getApplicationContext());
ctrl.setVisibility(View.GONE);
videoView.setMediaController(ctrl);
videoView.setVideoPath("/blablabla" + v);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(false);
videoView101.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// how to tell the thread that the video is over???
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v(TAG, "ERROR: " + what + " " + extra);
return false;
}
});
}
});
// wait for video play
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
t.start();
}
注意:我不能在onCompletion(MediaPlayer mp)中将“continueLoop”设置为false
我想让我的线程来控制操作。例如,线程将发送电子邮件,或显示Toast等
谢谢,刚找到一个,看起来效果不错
公共类MyVideoActivity扩展了活动{
public final static String DIR = "/blablabla/";
final String[] allVideoFiles = {"video001.mp4", "video002.mp4", "video003.mp4"};
private long c = -1;
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_video);
videoView = (VideoView) findViewById(R.id.videoView001);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play_video();
}
});
play_video();
}
private void play_video() {
c++;
int index = (int) (c % allVideoFiles.length);
String file = DIR + allVideoFiles[index];
File f = new File(file);
if (!f.exists()) {
Log.v("VIDEO", "File does NOT exist " + file);
return;
}
videoView.setVideoPath(file);
videoView.requestFocus();
videoView.setVisibility(View.VISIBLE);
videoView.start();
}
}您不需要该线程,使用该线程有什么特殊原因吗?同意。没有特别的原因!我明白你的意思,我认为这项工作(连续播放多个视频)可以通过在主类上实现“OnCompletionListener”来完成,对吗?