下面这段代码是android中Music中的处理代码:
复制代码 代码如下:
MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
mIsInitialized = false;
mCurrentMediaPlayer.release();
mCurrentMediaPlayer = new CompatMediaPlayer();
mCurrentMediaPlayer.setWakeMode(MediaPlaybackService.this, PowerManager.PARTIAL_WAKE_LOCK);
mHandler.sendMessageDelayed(mHandler.obtainMessage(SERVER_DIED), 2000);
return true;
default:
Log.d("MultiPlayer", "Error: " + what + "," + extra);
break;
}
return false;
}
};
简单来看,就是遇到MediaServer died这种情况,就release掉原来的MediaPlayer,然后重新创建一个MediaPlayer(会在MediaServer重新启动后关联起来)。然后可以根据原来的状态做相应的动作。