
Mp4ParserSample
看看类的最后一部分(335-442行),转换Runnable对象只是做这个工作!您必须根据需要调整代码,调整输入和输出文件路径以及转换参数(采样率,比特率等).
public static final String AUdio_RECORDING_file_name = "audio_Capturing-190814-034638.422.wav"; // input PCM filepublic static final String COMpressed_AUdio_file_name = "convertedmp4.m4a"; // Output MP4/M4A filepublic static final String COMpressed_AUdio_file_MIME_TYPE = "audio/mp4a-latm";public static final int COMpressed_AUdio_file_BIT_RATE = 64000; // 64kbpspublic 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,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,dstBuf.limit()); Log.e("bytesRead","Readed "+bytesRead); if (bytesRead == -1) { // -1 implIEs EOS hasMoreData = false; codec.queueinputBuffer(inputBufIndex,(long) presentationTimeUs,MediaCodec.BUFFER_FLAG_END_OF_STREAM); } else { totalBytesRead += bytesRead; dstBuf.put(tempBuffer,bytesRead); codec.queueinputBuffer(inputBufIndex,bytesRead,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... }}; 总结 以上是内存溢出为你收集整理的在Android上编辑wav到AAC全部内容,希望文章能够帮你解决在Android上编辑wav到AAC所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)