stopPlayback()之后的Android VideoView清除显示

Dan*_*lB6 25 android android-widget

我正在使用VideoView组件播放视频.手动调用stopPlayback()后,我似乎无法清除VideoView显示.我想重置VideoView,以便可以看到原始背景.

  1. 在VideoView组件中播放视频.
  2. 选择要播放的新视频.
  3. VideoView卡在第一个视频的最后一帧,直到下一个视频开始.如果下一个视频出现错误,则第一个视频中的静态图像仍然卡在那里.
  4. 如果我让视频播放到完成状态,则清除显示.

我正在使用的代码:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,基于VideoView.java源,stopPlayback()在底层MediaPlayer上执行以下操作:

public void stopPlayback() {
  if (mMediaPlayer != null) {
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*iam 16

对我来说,有用的只是隐藏然后显示VideoView使用setVisibility.

public void clearCurrentFrame() {
    videoView.setVisibility(GONE);
    videoView.setVisibility(VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

这是因为我希望它VideoView变得透明,而不是纯色.将背景设置为透明不起作用 - 视频帧仍然显示.


Dan*_*lB6 11

我决定使用的解决方案,除非有人能提供更好的解决方案,否则就是使用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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我引用的drawable是一个简单layer-list的自定义蓝色背景和居中的徽标图像.

绘制\ 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>
Run Code Online (Sandbox Code Playgroud)

或者,我也可以用来setZOrderOnTop控制表面视图的放置位置(您还可以在VideoViewer的顶部定义另一个视图并以此方式切换可见性):

videoViewer.setZOrderOnTop(false);
videoViewer.setZOrderOnTop(true);
Run Code Online (Sandbox Code Playgroud)

或者我也可以setBackgoundColor用来完成同样的事情setBackgroundResource:

videoViewer.setBackgroundColor(getResources().getColor(R.color.custom_blue));
videoViewer.setBackgroundColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)