我正在使用VideoView组件播放视频。手动调用stopPlayback((后,我似乎无法清除VideoView显示。我想重置VideoView,使原始背景可见。
- 在VideoView组件中播放视频
- 选择要播放的新视频
- VideoView一直停留在第一个视频的最后一帧,直到下一个视频开始。如果下一个视频出现错误,则第一个视频中的静态图像将保持不变
- 如果我让视频播放到完成状态,则显示被清除
我使用的代码:
private VideoView videoViewer = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
if (videoViewer != null) {
videoViewer.setOnPreparedListener(new OnPreparedListener());
}
...
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (videoViewer != null) {
videoViewer.stopPlayback();
videoViewer.setVideoURI(Uri.parse("http://my_vido_url/playlist.m3u8"));
}
}
private class OnPreparedListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mp) {
videoViewer.start();
}
}
请注意,基于VideoView.java源代码,stopPlayback((对底层MediaPlayer执行以下操作:
public void stopPlayback() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
对我来说,有效的方法只是隐藏VideoView
,然后使用setVisibility
显示。
public void clearCurrentFrame() {
videoView.setVisibility(GONE);
videoView.setVisibility(VISIBLE);
}
这是因为我希望VideoView
变得透明,而不是纯色。将背景设置为透明不起作用--视频帧仍然显示。
除非有人能提供更好的解决方案,否则我决定使用setBackgroundResource
,它的名称似乎很奇怪,因为它似乎会更改前台资源。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
videoViewer.setBackgroundResource(R.drawable.viewer_background);
...
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (videoViewer != null) {
videoViewer.stopPlayback();
// Set the background back to the drawable resource to clear the current video when switching to a new one.
videoViewer.setBackgroundResource(R.drawable.viewer_background);
videoViewer.setVideoURI(Uri.parse("http://my_vido_url/playlist.m3u8"));
}
}
private class OnPreparedListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mp) {
videoViewer.start();
// Clear the background resource when the video is prepared to play.
videoViewer.setBackgroundResource(0);
}
}
我引用的绘图是一个简单的layer-list
,带有自定义的蓝色背景和居中的徽标图像。
drawable\viewer_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="@color/custom_blue" />
</shape>
</item>
<item>
<bitmap android:src="@drawable/img_logo"
android:gravity="center" />
</item>
</layer-list>
或者,我也可以使用setZOrderOnTop
来控制表面视图的放置位置(您也可以在VideoViewer顶部定义另一个视图,并以这种方式切换可见性(:
videoViewer.setZOrderOnTop(false);
videoViewer.setZOrderOnTop(true);
或者,我也可以使用setBackgoundColor
来完成与setBackgroundResource
相同的事情:
videoViewer.setBackgroundColor(getResources().getColor(R.color.custom_blue));
videoViewer.setBackgroundColor(Color.TRANSPARENT);
仅此代码:
videoView.setVideoURI(null);
对我来说,这个线程中的解决方案都不起作用,所以我尝试了其他方法,下面是对我起作用的解决方案,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mVideoViewPlayList.setVisibility(View.GONE);
mVideoViewPlayList.setVisibility(View.VISIBLE);
mVideoViewPlayList.setVideoURI(Uri.parse(path));
}
}, 500);
你可以这样做
videoView.stopPlayBack();
videoView.seekTo(0);
希望这能帮助您
媒体控制器类本身已经具有此功能,通过下面的简单代码,您可以创建所有必要的交互。
MediaController mediaController = new MediaController (this);
videoView.setMediaController(mediaController);