title: How can I convert audio .mp3 to .m4a programmatically in android
body: <p>I don't want to use ffmpeg. Currently I was able to mux m4a audio with mp4 video. I wish to add mp3 audio also which required mp3 to m4a conversion.I was able to convert wav to m4a but not mp3 to m4a with below code</p>
  
  <p>Here is my code.</p>
  
  <pre><code> private void convertAudio(String filename) throws IOException {
  
      String outputpath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/converted.m4a";
      // Set up MediaExtractor to read from the source.
  
      MediaExtractor extractor = new MediaExtractor();
      extractor.setDataSource(filename);
  
  
      int trackCount = extractor.getTrackCount();
  
      // Set up MediaMuxer for the destination.
      MediaMuxer muxer;
      muxer = new MediaMuxer(outputpath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
      // Set up the tracks.
      HashMap&lt;Integer, Integer&gt; indexMap = new HashMap&lt;Integer, Integer&gt;(trackCount);
      for (int i = 0; i &lt; trackCount; i++) {
          extractor.selectTrack(i);
          MediaFormat format = extractor.getTrackFormat(i);
          format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AMR_NB);
  
          int dstIndex = muxer.addTrack(format);
          indexMap.put(i, dstIndex);
      }
      // Copy the samples from MediaExtractor to MediaMuxer.
      boolean sawEOS = false;
      int bufferSize = 32000;
      int frameCount = 0;
      int offset = 100;
      ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
      MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
     /* if (degrees &gt;= 0) {
          muxer.setOrientationHint(degrees);
      }*/
      // Test setLocation out of bound cases
  
      muxer.start();
      while (!sawEOS) {
          bufferInfo.offset = offset;
          bufferInfo.size = extractor.readSampleData(dstBuf, offset);
          if (bufferInfo.size &lt; 0) {
  
              sawEOS = true;
              bufferInfo.size = 0;
          } else {
              bufferInfo.presentationTimeUs = extractor.getSampleTime();
              bufferInfo.flags = extractor.getSampleFlags();
              int trackIndex = extractor.getSampleTrackIndex();
              muxer.writeSampleData(indexMap.get(trackIndex), dstBuf,
                      bufferInfo);
              extractor.advance();
              frameCount++;
  
          }
      }
      muxer.stop();
      muxer.release();
  
      return;
  }
  </code></pre>