媒体播放器无法在android中正确恢复

时间:2022-03-01 19:00:48

I have used SurfaceView in my android application and play videos by mediaplayer on this SurfaceView.

我在我的Android应用程序中使用了SurfaceView,并在此SurfaceView上通过mediaplayer播放视频。

In my activity I play, pause and resume video files successfully.

在我的活动中,我成功播放,暂停和恢复视频文件。

When I pause the video in my VideoActivity then open a new activity on it and then come back to my VideoActivity and resume the video, I hear the sound if the resumed video but have no display and the screen is black.

当我在VideoActivity中暂停视频然后在其上打开一个新活动,然后回到我的VideoActivity并恢复视频,如果恢复的视频但没有显示并且屏幕是黑色,我听到声音。

Why? How can I solve this problem?

为什么?我怎么解决这个问题?

My pause and resume methods:

我的暂停和恢复方法:

public void pausePlayback() {

        if (mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();
        }
    }

public void resumePlayback() {

        if (mediaPlayer != null && !mediaPlayer.isPlaying())
        {
            surfaceView1.requestFocus();
            mediaPlayer.start();
        }
    }

1 个解决方案

#1


This is usually a result of failing to connect the MediaPlayer to the new Surface. When you switch activities the SurfaceView and its associated Surface are torn down, so when you come back you need to connect to the new Surface with MediaPlayer#setSurface().

这通常是由于未能将MediaPlayer连接到新Surface而导致的。切换活动时,SurfaceView及其关联的Surface会被拆除,因此当您返回时,需要使用MediaPlayer#setSurface()连接到新的Surface。

#1


This is usually a result of failing to connect the MediaPlayer to the new Surface. When you switch activities the SurfaceView and its associated Surface are torn down, so when you come back you need to connect to the new Surface with MediaPlayer#setSurface().

这通常是由于未能将MediaPlayer连接到新Surface而导致的。切换活动时,SurfaceView及其关联的Surface会被拆除,因此当您返回时,需要使用MediaPlayer#setSurface()连接到新的Surface。