3

我使用播放 rtsp 视频流MediaPlayer并将其显示在SurfaceView. 我在 MediaPlayer 中进行初始工作Activity.onResume()并在Activity.onPause(). 当跳转到正常的活动(调用onPause)然后返回(调用onResume)时,它工作正常。

问题是,在播放视频时,跳转到类似对话框的Activity 并返回,视频无法刷新(视频帧被冻结)但音频很好。在这种情况下,我该怎么做才能正常播放视频流?如何刷新 SurfaceView?

谢谢你们。

ps:如上所述,普通的Activity是填满整个窗口的Activity,而类对话框的Activity是看起来像AlertDialog的Activity,通常是带有android:theme="@android:style/Theme.Dialog"主题的Activity,就像GoogleVoice app一样。这种Activity在前台时,我们可以看到后台Activity的一部分。

编辑:我的代码有点复杂,我粘贴了关键片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //....
    mPreview = (SurfaceView) this.findViewById(R.id.playerView);
    mHolder = mPreview.getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
protected void onResume() {
    mPath = "rtsp://10.10.20.42/Viral.mp4";
    // Create a new media player and set the listeners
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(mPath);
    // mMediaPlayer.setDisplay(holder);
    mMediaPlayer.setScreenOnWhilePlaying(true);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.prepareAsync();
    mMediaPlayer.setOnBufferingUpdateListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnVideoSizeChangedListener(this);
    mMediaPlayer.setOnErrorListener(this);

    super.onResume();
}

@Override
protected void onPause() {
    if (mMediaPlayer != null) {

        mMediaPlayer.release();
        mMediaPlayer = null;
    }
    super.onPause();
}
4

1 回答 1

2

好吧,我终于解决了这个问题。我没有重新设置SurfaceHolderMediaPlayer调用mMediaPlayer.setDisplay(SurfaceHolder holder))。

这是我的代码:

@Override
protected void onResume() {
    mPath = "rtsp://10.10.20.42/Viral.mp4";
    // Create a new media player and set the listeners
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(mPath);
    // mMediaPlayer.setDisplay(holder);
    mMediaPlayer.setScreenOnWhilePlaying(true);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.prepareAsync();
    mMediaPlayer.setOnBufferingUpdateListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnVideoSizeChangedListener(this);
    mMediaPlayer.setOnErrorListener(this);

    if (mHolder != null) {
        mMediaPlayer.setDisplay(mHolder);
    }
    super.onResume();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called");
    this.mHolder = surfaceholder;
    if (mMediaPlayer != null) {
        mMediaPlayer.setDisplay(surfaceholder);
    }
}
于 2013-03-28T08:58:18.200 回答