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.
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;
}
}
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.
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);
just this code:
videoView.setVideoURI(null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With