title: Android MediaMuxer illegalstateexception failed to stop the muxer mMediaMuxer.stop()
body: <p>I am having problems stopping the MediaMuxer.</p>
  
  <p>Pretty much have followed <a href="http://bigflake.com/mediacodec/EncodeAndMuxTest.java.txt" rel="nofollow">fadden's example</a> all the way, except I am using audio, so I am not 100% sure the audio and video tracks line up yet. This might be the problem.</p>
  
  <p>When I hit the breakpoint for mMediaMuxer.stop() I get a fatal error:</p>
  
  <pre><code>09-30 17:11:21.677: E/AndroidRuntime(32304): FATAL EXCEPTION: CaptureManager
  09-30 17:11:21.677: E/AndroidRuntime(32304): java.lang.IllegalStateException: Failed to stop the muxer
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.media.MediaMuxer.nativeStop(Native Method)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.media.MediaMuxer.stop(MediaMuxer.java:190)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaMux.stop(MyMediaMux.java:97)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.CaptureManager.stopMuxer(CaptureManager.java:86)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaCodec.stop(MyMediaCodec.java:209)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaCodec.checkEOS(MyMediaCodec.java:190)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaCodec.runQue(MyMediaCodec.java:177)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaCodec.runQue(MyMediaCodec.java:182)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.MyMediaCodec.sendEOS(MyMediaCodec.java:199)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at com.harmonicprocesses.penelopefree.camera.CaptureManager$2.run(CaptureManager.java:94)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.os.Handler.handleCallback(Handler.java:730)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.os.Handler.dispatchMessage(Handler.java:92)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.os.Looper.loop(Looper.java:137)
  09-30 17:11:21.677: E/AndroidRuntime(32304):    at android.os.HandlerThread.run(HandlerThread.java:61)
  </code></pre>
  
  <p>I can step into it far enough to tell MediaMuxer.mState = MUXER_STATE_INITIALIZED when I call stop. But it is failing in the nativeStart(mNativeObject) ...</p>
  
  <pre><code>if (mState == MUXER_STATE_INITIALIZED) {
      nativeStart(mNativeObject);
      mState = MUXER_STATE_STARTED;
  </code></pre>