【问题标题】:Android VideoView, how to understand that playback is finishedAndroid VideoView,如何理解播放完毕
【发布时间】:2015-10-17 08:28:51
【问题描述】:

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

注意:我不能在 onCompletion(MediaPlayer mp) 中将“continueLoop”设置为 false...

我想让我的线程来控制动作。比如线程会发送邮件,或者显示Toast等。

谢谢,

【问题讨论】:

  • 你不需要那个线程,使用它有什么特别的原因吗?
  • 同意。没有什么特殊原因!我明白你的意思,我认为这项工作(连续播放多个视频)可以通过在主类上实现“OnCompletionListener”来完成,对吧?

标签: android multithreading android-videoview


【解决方案1】:

刚刚找到一个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();

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多