您不需要FFMPEG,可以使用android中提供的标准编解码器。
public void playFile(String fileToPlay) { // see where we find a suitable autiotrack MediaExtractor extractor = new MediaExtractor(); try { extractor.setDataSource(fileToPlay); } catch (IOException e) { out.release(); return; } extractor.selectTrack(0); String fileType=typeForFile(fileToPlay); if (fileType==null) { out.release(); extractor.release(); return; } MediaCodec prec = MediaCodec.createDeprerByType(fileType); MediaFormat wantedFormat=extractor.getTrackFormat(0); prec.configure(wantedFormat,null,null,0); prec.start(); ByteBuffer[] inputBuffers = prec.getInputBuffers(); ByteBuffer[] outputBuffers = prec.getOutputBuffers(); // Allocate our own buffer int maximumBufferSizeBytes = 0; for(ByteBuffer bb:outputBuffers) { int c=bb.capacity(); if (c>maximumBufferSizeBytes) maximumBufferSizeBytes=c; } setupBufferSizes(maximumBufferSizeBytes/4); final MediaCodec.BufferInfo bufferInfo=new MediaCodec.BufferInfo(); MediaFormat format=null; while(true) { long timeoutUs=1000000; int inputBufferIndex = prec.dequeueInputBuffer(timeoutUs); if (inputBufferIndex >= 0) { ByteBuffer targetBuffer = inputBuffers[inputBufferIndex]; int read = extractor.readSampleData(targetBuffer, 0); int flags=extractor.getSampleFlags(); if (read>0) prec.queueInputBuffer(inputBufferIndex, 0,read, 0, flags); else prec.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); extractor.advance(); } int outputBufferIndex = prec.dequeueOutputBuffer(bufferInfo,timeoutUs); if (outputBufferIndex >= 0) { final boolean last = bufferInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM; int s=bufferInfo.size/4; ByteBuffer bytes=outputBuffers[outputBufferIndex]; ((ByteBuffer)bytes.position(bufferInfo.offset)).asShortBuffer().get(shorts,0,s*2); process(shorts,0,s*2); prec.releaseOutputBuffer(outputBufferIndex, false); if (last) break; } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { outputBuffers = prec.getOutputBuffers(); } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { format = prec.getOutputFormat(); } } extractor.release(); prec.stop(); prec.release();
上面的代码处理1个文件的读取。“处理”例程(尚未定义)接收交错的样本数组(左/右/左/右/左/右)。您现在要做的就是将这些通道添加到目标缓冲区。例如
short[] target=new short[LENGTH OF 4 MINUTES];int idx=0;process(short[] audio, int l){ for(int i=0;i<l;i++) target[idx++]+=audio[i]/2;}
生成的目标数组将包含您覆盖的样本。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)