如何在媒体播放器完成清除表面的持有人?媒体播放器、持有人、表面、如何在

2023-09-06 03:11:21 作者:姐,只想依赖你

我做了一个视频播放器,surfaceview和媒体播放器。 我有10个视频和10个按钮。 如果点击每个按钮,每个视频的播放。

I made a video player with surfaceview and mediaplayer. i have 10 videos and 10 buttons. if click on each buttons, each videos are playing.

这是我的code ..

here is my code..

//onCreate   
holder = surfaceview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


//Button1
if(mp == null)mp = new MediaPlayer();

mp.setDataSource(mediaplay_path);
mp.setDisplay(holder);
mp.setScreenOnWhilePlaying(true);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();


//Button2
if(mp != null){
    mp.stop();
    mp.reset();
}

mp.setDataSource(mediaplay_path2);
mp.setDisplay(holder);
mp.setScreenOnWhilePlaying(true);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();

//Button3~Button10 is same as Button2..

一切都很好。 我的自定义videoview正在好起来。 但是当视频转向了下,在previous视频的最后一个场景是保持了一段时间,轮流到下一个视频场景。

everything is fine. my custom videoview is working alright. but when the video turns to the next, the last scene of the previous video is remain for a while and turns to the next video scene.

我想这是因为previous surfaceview应该清楚之前,下一个视频播放。 但我不知道如何清除surfaceview或表面持有者。

i think it's because the previous surfaceview should be clear before next video is playing. but i have no idea how to clear the surfaceview or surface holder.

我搜索了这一点,但只能找到如何播放视频,而不是如何清除它设置为从媒体播放器的disaply的surfaceview。

i've searched for this but only could find how to play the video, not how to clear the surfaceview which is set the disaply from mediaplayer.

请帮帮我~~!

推荐答案

SurfaceHolder lockCanvas 中的方法将让你直接绘制到表面。使用画布的 drawColor 方法来填充黑色。

The SurfaceHolder has lockCanvas methods that will allow you to draw directly to the Surface. Use the drawColor method of the Canvas to fill it with black.

这就是说,它可能是preferable删除 SurfaceView (所建议的smile2you),因为这应该销毁的表面完全并释放未使用的资源。绝对确保你调用发布的MediaPlayer 之后一下它们与回放完成。抱着太多的视频资源将崩溃您的应用程序。

That said, it may be preferable to remove the SurfaceView (as suggested by smile2you), because that should destroy the Surface altogether and free up unused resources. And definitely make sure you are calling release on the MediaPlayers after they are done with playback. Holding on to too many video resources will crash your app.