title: react native - Unable to pause activity (java.util.ConcurrentModificationException)
body: <p>I'm using <a href="https://github.com/futurice/react-native-audio-toolkit" rel="nofollow noreferrer">react-native-audio-toolkit</a> to record and play sounds.</p>
  
  <p>Recording works fine. Player works fine too. But when I'm on the View where Player is initialised and used, and when I pause the app (by going to background), app crashes and the following error is displayed in my Android Debugger:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>This is the function within <code>AudioPlayerModule.java</code> that is causing this error (according to the error):</p>
  
  <pre><code>@Override
  public void onHostPause() {
      for (Map.Entry&lt;Integer, MediaPlayer&gt; 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);
          }
      }
  }
  </code></pre>
  
  <p>I'm using <code>react 16.2.0</code> and <code>react-native 0.53.3</code></p>
  
  <p>Can anyone suggest how I can fix this error?</p>