2011-12-15 31 views
12

MediaRecorderを使用してストリームをAACに直接記録できますが、既存のPCM/WAVファイルをAACにエンコードする方法はないようです。 AACにエンコードする機能は、Androidにネイティブに存在し、私はそれを使用したいと思います。既存のオーディオファイルでこれを行う方法はありませんか?AndroidのAACにwavをエンコードする

答えて

1

ネイティブコードで手を汚すことができ、フレームワーク内のデコーダにIOMX C++インターフェイスを使用できます。しかし、これはビルドセンシティブで、他の携帯電話やアンドロイドのフレーバーでは動作しません。

別のオプションは、ffmpegのようなopensource aacエンコーダを移植し、jniを介してアプリケーションを作成することです。同じアーキテクチャー(arm-9、cortex a8 ..)の電話機では、ほとんどの作業が可能です。

JBにはあなたの希望を満たすためのMediaCodecがあります。しかし、問題はJB搭載デバイスのインストールベースがもっと時間をかけてリーンになるということです。

http://developer.android.com/about/versions/android-4.1.html#Multimedia

+0

MediaCodecの使用例はありますか? – Taras

3

この美しい(と完璧に働い)の例を見て: Mp4ParserSample

クラス(行335から442)の最後の部分を見て、convert Runnableオブジェクトただ仕事をする!必要に応じてそのコードを整形し、入力ファイルと出力ファイルのパスと変換パラメータ(サンプリングレート、ビットレートなど)を調整する必要があります。

public static final String AUDIO_RECORDING_FILE_NAME = "audio_Capturing-190814-034638.422.wav"; // Input PCM file 
public static final String COMPRESSED_AUDIO_FILE_NAME = "convertedmp4.m4a"; // Output MP4/M4A file 
public static final String COMPRESSED_AUDIO_FILE_MIME_TYPE = "audio/mp4a-latm"; 
public static final int COMPRESSED_AUDIO_FILE_BIT_RATE = 64000; // 64kbps 
public static final int SAMPLING_RATE = 48000; 
public static final int BUFFER_SIZE = 48000; 
public static final int CODEC_TIMEOUT_IN_MS = 5000; 
String LOGTAG = "CONVERT AUDIO"; 
Runnable convert = new Runnable() { 
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
    @Override 
    public void run() { 
     android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); 
     try { 
      String filePath = Environment.getExternalStorageDirectory().getPath() + "/" + AUDIO_RECORDING_FILE_NAME; 
      File inputFile = new File(filePath); 
      FileInputStream fis = new FileInputStream(inputFile); 

      File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + COMPRESSED_AUDIO_FILE_NAME); 
      if (outputFile.exists()) outputFile.delete(); 

      MediaMuxer mux = new MediaMuxer(outputFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); 

      MediaFormat outputFormat = MediaFormat.createAudioFormat(COMPRESSED_AUDIO_FILE_MIME_TYPE,SAMPLING_RATE, 1); 
      outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); 
      outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, COMPRESSED_AUDIO_FILE_BIT_RATE); 
      outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384); 

      MediaCodec codec = MediaCodec.createEncoderByType(COMPRESSED_AUDIO_FILE_MIME_TYPE); 
      codec.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); 
      codec.start(); 

      ByteBuffer[] codecInputBuffers = codec.getInputBuffers(); // Note: Array of buffers 
      ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers(); 

      MediaCodec.BufferInfo outBuffInfo = new MediaCodec.BufferInfo(); 
      byte[] tempBuffer = new byte[BUFFER_SIZE]; 
      boolean hasMoreData = true; 
      double presentationTimeUs = 0; 
      int audioTrackIdx = 0; 
      int totalBytesRead = 0; 
      int percentComplete = 0; 
      do { 
       int inputBufIndex = 0; 
       while (inputBufIndex != -1 && hasMoreData) { 
        inputBufIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_IN_MS); 

        if (inputBufIndex >= 0) { 
         ByteBuffer dstBuf = codecInputBuffers[inputBufIndex]; 
         dstBuf.clear(); 

         int bytesRead = fis.read(tempBuffer, 0, dstBuf.limit()); 
         Log.e("bytesRead","Readed "+bytesRead); 
         if (bytesRead == -1) { // -1 implies EOS 
          hasMoreData = false; 
          codec.queueInputBuffer(inputBufIndex, 0, 0, (long) presentationTimeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM); 
         } else { 
          totalBytesRead += bytesRead; 
          dstBuf.put(tempBuffer, 0, bytesRead); 
          codec.queueInputBuffer(inputBufIndex, 0, bytesRead, (long) presentationTimeUs, 0); 
          presentationTimeUs = 1000000l * (totalBytesRead/2)/SAMPLING_RATE; 
         } 
        } 
       } 
       // Drain audio 
       int outputBufIndex = 0; 
       while (outputBufIndex != MediaCodec.INFO_TRY_AGAIN_LATER) { 
        outputBufIndex = codec.dequeueOutputBuffer(outBuffInfo, CODEC_TIMEOUT_IN_MS); 
        if (outputBufIndex >= 0) { 
         ByteBuffer encodedData = codecOutputBuffers[outputBufIndex]; 
         encodedData.position(outBuffInfo.offset); 
         encodedData.limit(outBuffInfo.offset + outBuffInfo.size); 
         if ((outBuffInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0 && outBuffInfo.size != 0) { 
          codec.releaseOutputBuffer(outputBufIndex, false); 
         }else{ 
          mux.writeSampleData(audioTrackIdx, codecOutputBuffers[outputBufIndex], outBuffInfo); 
          codec.releaseOutputBuffer(outputBufIndex, false); 
         } 
        } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { 
         outputFormat = codec.getOutputFormat(); 
         Log.v(LOGTAG, "Output format changed - " + outputFormat); 
         audioTrackIdx = mux.addTrack(outputFormat); 
         mux.start(); 
        } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { 
         Log.e(LOGTAG, "Output buffers changed during encode!"); 
        } else if (outputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER) { 
         // NO OP 
        } else { 
         Log.e(LOGTAG, "Unknown return code from dequeueOutputBuffer - " + outputBufIndex); 
        } 
       } 
       percentComplete = (int) Math.round(((float) totalBytesRead/(float) inputFile.length()) * 100.0); 
       Log.v(LOGTAG, "Conversion % - " + percentComplete); 
      } while (outBuffInfo.flags != MediaCodec.BUFFER_FLAG_END_OF_STREAM); 
      fis.close(); 
      mux.stop(); 
      mux.release(); 
      Log.v(LOGTAG, "Compression done ..."); 
     } catch (FileNotFoundException e) { 
      Log.e(LOGTAG, "File not found!", e); 
     } catch (IOException e) { 
      Log.e(LOGTAG, "IO exception!", e); 
     } 

     //mStop = false; 
     // Notify UI thread... 
    } 
}; 
関連する問題