Android VideoView,如何理解播放完成

时间:2015-10-17 08:28:51

标签: android multithreading android-videoview

在onCreate()方法中,我开始创建一个播放视频的线程,我想让线程理解视频播放已完成。

如何实施?

这是我的代码:

@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();
}

注意:我无法设置" continueLoop"在onCompletion(MediaPlayer mp)中为false ...

我想让我的线程控制动作。例如,线程将发送电子邮件,或显示Toast等。

谢谢,

1 个答案:

答案 0 :(得分:0)

刚刚找到answer,看起来效果很好......

公共类MyVideoActivity扩展了Activity {

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();

}

}