不添加TextureRenderView,或者SurfaceRenderView,就无法响应OnCompletionListener。为什么呢?
下面是我的伪代码
private void playVideo() { //如果不为空释放在new if (mediaPlayer != null) { mediaPlayer.reset(); } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(getContext(), Uri.parse(videoUrl)); if (mSurfaceView.getVisibility() == View.GONE) mSurfaceView.setVisibility(View.VISIBLE); SurfaceHolder holder = mSurfaceView.getHolder(); holder.addCallback(new MyCallBack()); mediaPlayer.prepare(); //准备完成监听 mediaPlayer.setOnPreparedListener(onPreparedListener); mediaPlayer.setOnCompletionListener(onCompletionListener); } catch (Exception e) { e.printStackTrace(); } }
/**
* 准备完成
*/
MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// mediaPlayer.setLooping(true);//循环播放 mp.start(); } };
MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
XLogUtils.e("播放完成");
mSurfaceView.setVisibility(View.GONE);
if (currentVideoIndex == videoUrls.size() - 1) return;
currentVideoIndex++;
videoUrl = videoUrls.get(currentVideoIndex);
XLogUtils.d("播放下一个");
playVideo();
}
};
/**
*
*/
private class MyCallBack implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mediaPlayer != null)
mediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
7 4
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。