android view stop,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.

Play a video in a VideoView component.

Select a new video to play.

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.

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;

}

}

回答1:

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.

回答2:

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:

android:gravity="center" />

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

回答3:

just this code:

videoView.setVideoURI(null);

回答4:

Well, for me none of the solutions from this thread worked, so I tried something else and below is the solution that worked for me,

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

mVideoViewPlayList.setVisibility(View.GONE);

mVideoViewPlayList.setVisibility(View.VISIBLE);

mVideoViewPlayList.setVideoURI(Uri.parse(path));

}

}, 500);

回答5:

You can do like this;

videoView.stopPlayBack();

videoView.seekTo(0);

hope that may help you

回答6:

The Media Controller class already has this feature among itself, with this simple code below you can create all the necessary interactions.

MediaController mediaController = new MediaController (this);

videoView.setMediaController(mediaController);

来源:https://stackoverflow.com/questions/15767742/android-videoview-clear-display-after-stopplayback

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值