Android VideoView 使用 MediaController - 如何在流式传输时禁用搜索
Android VideoView with MediaController - how to disable seeking while streaming
如果从设备播放视频,媒体播放器会知道它的确切长度,并且通过移动 mediacontroller 搜索栏中的拇指快进视频是无缝的。
如果从 Internet 播放流媒体视频,视频长度未知,使用拇指搜索会导致视频停止响应。
我想知道如何在视频完全缓冲之前禁用拇指?然后显示它并启用快进。
以下是我的视频播放器代码:
setContentView(R.layout.layout_fragment_video_fullscreen);
vvVideoFullscreen = (VideoView) findViewById(R.id.vvVideoFullscreen);
rlVideoFullscreen = (RelativeLayout) findViewById(R.id.rlVideoFullscreen);
videoUri = Uri.parse(video.getUrl());
vvVideoFullscreen.setVideoURI(videoUri);
mediaController = new MediaController(this);
mediaController.setMediaPlayer(vvVideoFullscreen);
mediaController.setAnchorView(vvVideoFullscreen);
vvVideoFullscreen.setMediaController(mediaController);
vvVideoFullscreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
rlVideoFullscreen.setVisibility(View.VISIBLE);
vvVideoFullscreen.start();
mediaController.show(3000);
}
});
vVideoFullscreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
vvVideoFullscreen.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (vvVideoFullscreen.isPlaying()) {
vvVideoFullscreen.pause();
mediaController.hide();
} else {
vvVideoFullscreen.start();
mediaController.show();
}
}
return true;
}
}); // End of setOnTouchListener
您是否尝试过将 onBufferingUpdateListener()
添加到您的 mediaplayer
?
从文档中说:
public abstract void onBufferingUpdate (MediaPlayer mp, int percent)
Called to update status in buffering a media stream received through
progressive HTTP download. The received buffering percentage indicates
how much of the content has been buffered or played. For example a
buffering update of 80 percent when half the content has already been
played indicates that the next 30 percent of the content to play has
been buffered.
因此,当以 100% 调用此回调时,您就知道您的视频已完全缓冲。
要禁用所有播放控件,只需在缓冲完成时调用 mediaController.setMediaPlayer(...)
。
如果您只想禁用搜索缩略图,我建议实现您自己的 MediaController.MediaPlayerControlInterface
并将所有调用路由到您的 MediaPlayer
对象。然后可以通过使 canSeekForward()
和 canSeekBackward()
方法 return 为 false 并使 seekTo(int pos)
方法不在您的 MediaPlayer 上执行任何操作来禁用搜索。
如果从设备播放视频,媒体播放器会知道它的确切长度,并且通过移动 mediacontroller 搜索栏中的拇指快进视频是无缝的。 如果从 Internet 播放流媒体视频,视频长度未知,使用拇指搜索会导致视频停止响应。
我想知道如何在视频完全缓冲之前禁用拇指?然后显示它并启用快进。
以下是我的视频播放器代码:
setContentView(R.layout.layout_fragment_video_fullscreen);
vvVideoFullscreen = (VideoView) findViewById(R.id.vvVideoFullscreen);
rlVideoFullscreen = (RelativeLayout) findViewById(R.id.rlVideoFullscreen);
videoUri = Uri.parse(video.getUrl());
vvVideoFullscreen.setVideoURI(videoUri);
mediaController = new MediaController(this);
mediaController.setMediaPlayer(vvVideoFullscreen);
mediaController.setAnchorView(vvVideoFullscreen);
vvVideoFullscreen.setMediaController(mediaController);
vvVideoFullscreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
rlVideoFullscreen.setVisibility(View.VISIBLE);
vvVideoFullscreen.start();
mediaController.show(3000);
}
});
vVideoFullscreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
vvVideoFullscreen.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (vvVideoFullscreen.isPlaying()) {
vvVideoFullscreen.pause();
mediaController.hide();
} else {
vvVideoFullscreen.start();
mediaController.show();
}
}
return true;
}
}); // End of setOnTouchListener
您是否尝试过将 onBufferingUpdateListener()
添加到您的 mediaplayer
?
从文档中说:
public abstract void onBufferingUpdate (MediaPlayer mp, int percent)
Called to update status in buffering a media stream received through progressive HTTP download. The received buffering percentage indicates how much of the content has been buffered or played. For example a buffering update of 80 percent when half the content has already been played indicates that the next 30 percent of the content to play has been buffered.
因此,当以 100% 调用此回调时,您就知道您的视频已完全缓冲。
要禁用所有播放控件,只需在缓冲完成时调用 mediaController.setMediaPlayer(...)
。
如果您只想禁用搜索缩略图,我建议实现您自己的 MediaController.MediaPlayerControlInterface
并将所有调用路由到您的 MediaPlayer
对象。然后可以通过使 canSeekForward()
和 canSeekBackward()
方法 return 为 false 并使 seekTo(int pos)
方法不在您的 MediaPlayer 上执行任何操作来禁用搜索。