Attach / detach Android MediaPlayer holder while playing

I am creating a streaming application that will play an rstp video stream. The point is, I need the video to keep playing in the background (audio only) when the user navigates to another activity.

To achieve this, I created a static MediaPlayer object that plays the stream. Background playback works great if I never call the function mediaPlayer.setDisplay (holder). But once I have the SurfaceHolder installed, I cannot get it back to zero without pausing in the MediaPlayer. This is the code in my Activity with SurfaceView.

@Override
public void surfaceCreated(SurfaceHolder holder) {
    StaticMediaPlayer.setHolder(holder);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    StaticMediaPlayer.setHolder(null);
    //I've tried to do StaticMediaPlayer.setHolder(holder);
    //This actually continues the playing, but the mediaPlayer can no longer attach
    //Also, the console is flooded with errors.
}

      

The set holder function is just a setDisplay wrapper function

public static void setHolder(SurfaceHolder holder) {
    mediaPlayer.setDisplay(holder);
    //I've even tried to put mediaPlayer.start() here but it doesn't continue.
}

      

The problem is when I set the holder to zero, the video stream is paused. It usually resumes if I go back to Activity again.

I registered the values ​​of mediaPlayer.getCurrentPosition () and mediaPlayer.isPlaying () to make sure it is still playing.

This is what it looks like when he plays normally

pos: 113623 playing: true
pos: 114518 playing: true
pos: 115582 playing: true
pos: 116590 playing: true

      

And it is like when the SurfaceView is destroyed.

pos: 117917 playing: true //This shows that the mediaplayer is still playing.
pos: 117917 playing: true //But for some reason the position isn't progressing
pos: 117917 playing: true
pos: 117917 playing: true 

      

How do I force the MediaPlayer to continue playing even when the SurfaceHolder is set to null?

+3


source to share





All Articles