1

在我的代码中,我希望能够暂时隐藏(并随后显示)视频。由于要求,我正在使用 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 吗?

4

0 回答 0