Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VideoView clear display after stopPlayback()

I'm using a VideoView component to play videos. I can't seem to clear the VideoView display after I manually invoke stopPlayback(). I would like to reset the VideoView so that the original background is visible.

  1. Play a video in a VideoView component.
  2. Select a new video to play.
  3. The VideoView is stuck on the last frame of the first video until the next video starts. If there is an error with the next video, the static image from the first video remains stuck there.
  4. If I let the video play to completion state, the display is cleared.

The code I'm using:

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

Note that based on the VideoView.java source, stopPlayback() takes the following action on the underlying MediaPlayer:

public void stopPlayback() {
  if (mMediaPlayer != null) {
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
}
like image 212
DanielB6 Avatar asked Apr 02 '13 15:04

DanielB6


3 Answers

For me, what worked was simply to hide then show the VideoView using setVisibility.

public void clearCurrentFrame() {
    videoView.setVisibility(GONE);
    videoView.setVisibility(VISIBLE);
}

This was because I wanted the VideoView to become transparent, not be a solid colour. Setting the background to transparent doesn't work -- the video frame still shows.

like image 100
William Avatar answered Nov 07 '22 14:11

William


The solution I settled on, unless someone can offer a better one, is to use setBackgroundResource, which seems oddly named since it appears to change the foreground resource.

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

The drawable I'm referencing is a simple layer-list with a custom blue background and a centered logo image.

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>

Alternatively I was also able to use setZOrderOnTop to control where the surface view is placed (you could also define another view on top of the VideoViewer and toggle the visibility that way):

videoViewer.setZOrderOnTop(false);
videoViewer.setZOrderOnTop(true);

Alternatively I could also use setBackgoundColor to accomplish the same thing as setBackgroundResource:

videoViewer.setBackgroundColor(getResources().getColor(R.color.custom_blue));
videoViewer.setBackgroundColor(Color.TRANSPARENT);
like image 38
DanielB6 Avatar answered Nov 07 '22 12:11

DanielB6


just this code:

videoView.setVideoURI(null);
like image 2
mojtaba Avatar answered Nov 07 '22 14:11

mojtaba