如何将新创建的SurfaceHolder与MediaPlayer相关联

时间:2013-10-23 03:49:20

标签: android android-mediaplayer

在我的代码中,我希望能够暂时隐藏(并随后显示)视频。由于要求,我使用的是SurfaceView + MediaPlayer组合而不是VideoView。但是,在我展示SurfaceView后,我在预期视频播放方面遇到了困难。

我的代码摘录包括以下内容:

public void show() {
    if (mSurface != null) mSurface.setVisibility(View.VISIBLE);
}

public void hide() {
    if (mSurface != null) {
        if (isInPlaybackState()) pause();
        mSurface.setVisibility(View.INVISIBLE);
    }
}

@Override
public void surfaceCreated(final SurfaceHolder holder) {
    mHolder = holder;
    openVideo();
}

@Override
public void surfaceDestroyed(final SurfaceHolder holder) {
    // After this, the surface can't be used again
    mHolder = null;
}

private void openVideo() {
    if (mAssetPath == null || !mAssetPath.isEmpty() || mHolder == null) {
        // Not ready yet; try again later
        return;
    }

    // Pause music playback service
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    getActivity().sendBroadcast(i);

    if (mPlayer == null) {
        initializePlayer();
    } else {
        mPlayer.setDisplay(mHolder);
    }
}

根据以上所述,当我致电hide()时,surfaceDestroyed(SurfaceHolder)会被触发。当我稍后致电show()时,surfaceCreated(SurfaceHolder)会被触发,这会调用openVideo()并将播放器与新提供的SurfaceHolder相关联。

以上工作符合预期,我相信这应该是正确的过程;但是,当我拨打mPlayer.start()时,我会听到视频播放的音频没有任何视频,并看到以下错误消息(这最终导致媒体播放停止并完成,如disconnect日志所述) :

10-23 11:29:42.775: E/MediaPlayer(4204): Error (1,-2147483648)
10-23 11:29:42.795: E/MediaPlayer(4204): Error (-38,0)
10-23 11:29:42.805: E/MediaPlayer(4204): Error (1,-2147483648)
10-23 11:29:42.810: V/MediaPlayer(4204): message received msg=100, ext1=1, ext2=-2147483648
10-23 11:29:42.810: E/MediaPlayer(4204): error (1, -2147483648)
10-23 11:29:42.810: V/MediaPlayer(4204): callback application
10-23 11:29:42.810: V/MediaPlayer(4204): back from callback
10-23 11:29:42.825: E/MediaPlayer(4204): Error (1,-2147483648)
10-23 11:29:42.850: V/MediaPlayer-JNI(4204): getCurrentPosition: 671668 (msec)
10-23 11:29:42.850: V/MediaPlayer-JNI(4204): getCurrentPosition: 671668 (msec)
10-23 11:29:42.850: V/MediaPlayer(4204): message received msg=100, ext1=1, ext2=-2147483648
10-23 11:29:42.850: E/MediaPlayer(4204): error (1, -2147483648)
10-23 11:29:42.850: V/MediaPlayer(4204): callback application
10-23 11:29:42.850: V/MediaPlayer(4204): back from callback
10-23 11:29:42.875: V/MediaPlayer-JNI(4204): stop
10-23 11:29:42.875: V/MediaPlayer(4204): stop
10-23 11:29:42.875: E/MediaPlayer(4204): stop called in state 0
10-23 11:29:42.875: V/MediaPlayer(4204): message received msg=100, ext1=-38, ext2=0
10-23 11:29:42.875: E/MediaPlayer(4204): error (-38, 0)
10-23 11:29:42.875: V/MediaPlayer(4204): callback application
10-23 11:29:42.875: V/MediaPlayer(4204): back from callback
10-23 11:29:42.875: V/MediaPlayer-JNI(4204): reset
10-23 11:29:42.875: V/MediaPlayer(4204): reset
10-23 11:29:42.900: V/MediaPlayer-JNI(4204): release
10-23 11:29:42.900: V/MediaPlayer(4204): setListener
10-23 11:29:42.900: V/MediaPlayer(4204): disconnect
10-23 11:29:42.910: V/MediaPlayer(4204): destructor
10-23 11:29:42.910: V/MediaPlayer(4204): disconnect

之前是否有人遇到此问题并找到了解决方法?或者唯一的选择是创建一个新的MediaPlayer吗?

0 个答案:

没有答案