title: Add second audio track to MediaMuxer
body: <p>I have a video file <code>.mp4</code> - video track only.</p>
  
  <p>I'm using <code>MediaExtractor</code> and <code>MediaMuxer</code> to add audio file.
  this Works good.</p>
  
  <p>On the processed file i want to add another audio track.</p>
  
  <p>So i'm using again <code>MediaExtractor</code> and <code>MediaMuxer</code> to kind of copy the file, (Creating video and audio tracks, reading [extractor] and writing [muxer]). In addition i'm trying to add the second audio track to the muxer. but this throws the error <code>Failed to add the track to the muxer</code>. </p>
  
  <p>in this <a href="https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/MediaMuxerTest.java" rel="nofollow">link</a> we can see that muxer does not support multiple tracks.</p>
  
  <p>Code From the link:</p>
  
  <pre><code>// Throws exception b/c 2 audio tracks were added.
  muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
  muxer.addTrack(MediaFormat.createAudioFormat("audio/mp4a-latm", 48000, 1));
  try {
      muxer.addTrack(MediaFormat.createAudioFormat("audio/mp4a-latm", 48000, 1));
      fail("should throw IllegalStateException.");
  } catch (IllegalStateException e) {
     // expected
  }
  </code></pre>
  
  <p>Is there other way to do it ?
  Elegant way ?</p>
  
  <p>BTW, i'm trying to avoid using 3rd parties - like ffmpeg or so.. But if would be my only solution...</p>
  
  <p><strong>--EDIT--</strong></p>
  
  <p>Relevant piece of my code</p>
  
  <pre><code>MediaMuxer muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
  
  MediaExtractor extractor = new MediaExtractor();
  extractor.setDataSource(videoAndAudioFile);
  
  for (int currTrackIdx = 0; currTrackIdx &lt; extractor.getTrackCount(); currTrackIdx++) {
     MediaFormat trackFormat = extractor.getTrackFormat(currTrackIdx);
     tracksIdx.add(muxer.addTrack(trackFormat));
  }
  
  MediaExtractor extractor2 = new MediaExtractor();
  extractor2.setDataSource(secondAudioFile);
  MediaFormat trackFormat = extractor2.getTrackFormat(0);
  tracksIdx.add(muxer.addTrack(trackFormat)); // Crashes here
  </code></pre>