SurfaceView 在播放器切到后台播放时,SurfaceView的SurfaceHolder会destroy掉,为了避免切回到前台只有声音没有画面的情况,在surfaceChanged的回调函数里需要加上mMediaPlayer.setDisplay(holder),在 destroy的时候pause,在create的时候 start,如下
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mMediaPlayer.setDisplay(holder);
Log.e("SurfaceHolder","change " +"width: "+width +" height: "+height);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mMediaPlayer !=null &&mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
Log.e("SurfaceHolder","destory");
}
}