In my code, I want to be able to temporarily hide (and subsequently show) a video. I am using a SurfaceView + MediaPlayer combination instead of the VideoView due to requirements. However, I am facing difficulties in getting video playback to occur as expected after I show the SurfaceView.
My code excerpts includes the following:
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);
}
}
Based on the above, when I call hide(), surfaceDestroyed(SurfaceHolder) gets triggered. When I later call show(), surfaceCreated(SurfaceHolder) gets triggered, which will call openVideo() and associate the player with the newly provided SurfaceHolder.
The above works as expected, and I believe this should be the correct process; however, when I call mPlayer.start(), I would hear the video's audio playing without any video and see the following error messages (which eventually causes the media playback to stop and complete, as noted by the disconnect logs):
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
Has anyone encountered this issue before and found a workaround? Or would the only option be to create a new MediaPlayer as well?