当我尝试setNextMediaPlayer
它会抛出下一个玩家不能自我,我不明白它是什么。提前谢谢。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void createNextMediaPlayer(int resource) {
Uri url = Uri.parse("android.resource://" + mService.getPackageName() + "/" + resource);
mNextPlayer = new MediaPlayer();
mNextPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mNextPlayer.setDataSource(mService, url);
} catch (IOException e) {
e.printStackTrace();
}
mNextPlayer.prepareAsync();
mNextPlayer.setWakeMode(mService.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mNextPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mCurrentPlayer != null)
mCurrentPlayer.setNextMediaPlayer(mp); //ERROR
}
});
if (mCurrentPlayer != null)
mCurrentPlayer.setOnCompletionListener(onCompletionListener);
mOnCompleteResId = resource;
}
07-29 20:35:46.543 14771-14771 / com.supermedaiplayer E / AndroidRuntime: 致命异议:主要 处理:com.supermedaiplayer,PID:14771 java.lang.IllegalArgumentException:下一个玩家不能自我 在android.media.MediaPlayer.setNextMediaPlayer(原生方法) at god.mediaplayers.LocalPlayer $ 2.onPrepared(LocalPlayer.java:410) 在 android.media.MediaPlayer $ EventHandler.handleMessage(MediaPlayer.java:2871) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
答案 0 :(得分:0)
谢谢,我自己发现了这个错误,希望我的回答可能是一个。
我将从mp
获得的onPrepare(Mediaplayer mp)
传递给mCurrentPlayer.setNextMediaPlayer(mp);
,其中有些情况与导致问题的mCurrentPlayer
相同。
根据documentation“下一个MediaPlayer必须与'this'不同。如果next == this,将抛出异常。”所以我将mNextPlayer
传递给mCurrentPlayer.setNextMediaPlayer(mNextPlayer);
,这就是诀窍。
我使用修复程序更新了代码:
private void createNextMediaPlayer(int resource) {
Uri url = Uri.parse("android.resource://" + mService.getPackageName() + "/" + resource);
mNextPlayer = new MediaPlayer();
mNextPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mNextPlayer.setDataSource(mService, url);
} catch (IOException e) {
e.printStackTrace();
}
mNextPlayer.prepareAsync();
mNextPlayer.setWakeMode(mService.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mNextPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mCurrentPlayer != null) {
LogHelper.d(TAG, "Checking reference for mp", mCurrentPlayer, " ", mp);
LogHelper.d(TAG, "Checking reference for mNextPlayer ", mCurrentPlayer, " ", mNextPlayer);
mCurrentPlayer.setNextMediaPlayer(mNextPlayer);
}
}
});
if (mCurrentPlayer != null)
mCurrentPlayer.setOnCompletionListener(onCompletionListener);
mOnCompleteResId = resource;
}
更新:
在我的情况下,它发生在Prepared被多次调用。在某些时候,mNextPlayer
没有准备好,所以它再次崩溃,消息mNextPlayer没有准备好所以我调整了代码以符合我的要求。
代码:
@Override
public void onPrepared(MediaPlayer mp) {
if (mCurrentPlayer != null && mp != mCurrentPlayer) {
mCurrentPlayer.setNextMediaPlayer(mp);
}
}