我使用react-native-audio-toolkit录制和播放声音。
录制工作正常。播放器工作正常。但是当我在View上初始化和使用Player时,以及当我暂停应用程序时(通过转到后台),应用程序崩溃并在我的Android调试器中显示以下错误:
05-24 21:06:35.584 10759-10759/com.myapp E/MediaPlayerNative: pause called in state 8, mPlayer(0xcdfc5c60)
05-24 21:06:35.584 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.585 10759-16605/com.myapp E/MediaPlayer: Error (-38,0)
05-24 21:06:35.585 10759-10759/com.myapp E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0xcdfc5c60, mCurrentState=0
05-24 21:06:35.585 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.586 10759-10759/com.myapp E/MediaPlayerNative: pause called in state 8, mPlayer(0xcdfc5ec0)
05-24 21:06:35.586 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.586 10759-10759/com.myapp E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0xcdfc5ec0, mCurrentState=0
05-24 21:06:35.586 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.587 10759-16605/com.myapp V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
05-24 21:06:35.587 10759-16605/com.myapp V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
05-24 21:06:35.592 10759-10759/com.myapp E/MediaPlayerNative: pause called in state 8, mPlayer(0xcc47f640)
05-24 21:06:35.592 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.592 10759-10759/com.myapp E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0xcc47f640, mCurrentState=0
05-24 21:06:35.592 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.593 10759-10759/com.myapp E/MediaPlayerNative: pause called in state 8, mPlayer(0xcc47f6a0)
05-24 21:06:35.593 10759-10759/com.myapp E/MediaPlayerNative: error (-38, 0)
05-24 21:06:35.599 10759-10759/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 10759
java.lang.RuntimeException: Unable to pause activity {com.myapp/com.myapp.MainActivity}: java.util.ConcurrentModificationException
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4058)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4024)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3997)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3971)
at android.app.ActivityThread.-wrap15(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1700)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6753)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1441)
at java.util.HashMap$EntryIterator.next(HashMap.java:1475)
at java.util.HashMap$EntryIterator.next(HashMap.java:1475)
at com.futurice.rctaudiotoolkit.AudioPlayerModule.onHostPause(AudioPlayerModule.java:64)
at com.facebook.react.bridge.ReactContext.onHostPause(ReactContext.java:214)
at com.facebook.react.ReactInstanceManager.moveToBeforeResumeLifecycleState(ReactInstanceManager.java:676)
at com.facebook.react.ReactInstanceManager.onHostPause(ReactInstanceManager.java:510)
at com.facebook.react.ReactInstanceManager.onHostPause(ReactInstanceManager.java:529)
at com.facebook.react.ReactActivityDelegate.onPause(ReactActivityDelegate.java:96)
at com.facebook.react.ReactActivity.onPause(ReactActivity.java:60)
at android.app.Activity.performPause(Activity.java:7170)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1552)
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4047)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4024)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3997)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3971)
at android.app.ActivityThread.-wrap15(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1700)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6753)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
05-24 21:06:35.604 10759-16605/com.myapp W/MediaPlayer: mediaplayer went away with unhandled events
05-24 21:06:35.604 10759-16605/com.myapp E/MediaPlayer: Error (-38,0)
05-24 21:06:35.607 10759-16605/com.myapp V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
05-24 21:06:35.608 10759-16605/com.myapp V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
05-24 21:06:35.614 10759-16605/com.myapp W/MediaPlayer: mediaplayer went away with unhandled events
05-24 21:06:35.614 10759-16605/com.myapp E/MediaPlayer: Error (-38,0)
05-24 21:06:35.616 10759-16605/com.myapp V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
05-24 21:06:35.616 10759-16605/com.myapp V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
05-24 21:06:35.620 10759-16605/com.myapp W/MediaPlayer: mediaplayer went away with unhandled events
05-24 21:06:35.641 10759-18225/com.myapp D/OSTracker: OS Event: crash
这是导致此错误的AudioPlayerModule.java
内的函数(根据错误):
@Override
public void onHostPause() {
for (Map.Entry<Integer, MediaPlayer> entry : this.playerPool.entrySet()) {
Integer playerId = entry.getKey();
if (!this.playerContinueInBackground.get(playerId)) {
MediaPlayer player = entry.getValue();
player.pause();
WritableMap info = getInfo(player);
WritableMap data = new WritableNativeMap();
data.putString("message", "Playback paused due to onHostPause");
data.putMap("info", info);
emitEvent(playerId, "pause", data);
}
}
}
我正在使用react 16.2.0
和react-native 0.53.3
有人可以建议我如何修复此错误吗?
答案 0 :(得分:0)
发生错误是因为在父级的每个子级中生成了同一音频文件的实例,导致ConcurrentModificationException
。只需将一个实例调用父组件中的音频文件,然后通过子组件中的实例调用它们。为我工作。