Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- songOutPath = Environment.getExternalStorageDirectory()+"/My Folder/song.mp4";
- muxer = new MediaMuxer(songOutPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
- mTrackIndex = muxer.addTrack(format);
- muxer.start();
- mMuxerStarted = true;
- MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
- final long timeoutUs = 5000;
- boolean sawInputEOS = false;
- boolean sawOutputEOS = false;
- int noOutputCounter = 0;
- MediaCodec.BufferInfo info2 = new MediaCodec.BufferInfo();
- final long timeoutUs2 = 5000;
- boolean sawInputEOS2 = false;
- boolean sawOutputEOS2 = false;
- int noOutputCounter2 = 0;
- while (!sawOutputEOS && !sawOutputEOS2 && noOutputCounter < 50 && noOutputCounter2 < 50) {
- noOutputCounter++;
- if (!sawInputEOS) {
- int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
- if (inputBufferIndex >= 0) {
- ByteBuffer buffer = codecInputBuffers[inputBufferIndex];
- int sampleSize = extractor.readSampleData(buffer, 0);
- long presentationTimeUs = 0;
- if (sampleSize < 0) {
- sawInputEOS = true;
- sampleSize = 0;
- } else {
- presentationTimeUs = extractor.getSampleTime();
- }
- codec.queueInputBuffer(inputBufferIndex, 0, sampleSize,
- presentationTimeUs,
- sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
- if (!sawInputEOS) {
- extractor.advance();
- }
- }
- }
- noOutputCounter2++;
- if (!sawInputEOS2) {
- int inputBufferIndex2 = codec2.dequeueInputBuffer(timeoutUs2);
- if (inputBufferIndex2 >= 0) {
- ByteBuffer buffer2 = codecInputBuffers2[inputBufferIndex2];
- int sampleSize2 = extractor2.readSampleData(buffer2, 0);
- long presentationTimeUs2 = 0;
- if (sampleSize2 < 0) {
- sawInputEOS2 = true;
- sampleSize2 = 0;
- } else {
- presentationTimeUs2 = extractor2.getSampleTime();
- }
- codec2.queueInputBuffer(inputBufferIndex2, 0, sampleSize2,
- presentationTimeUs2,
- sawInputEOS2 ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
- if (!sawInputEOS2) {
- extractor2.advance();
- }
- }
- }
- int outputBufferIndex = codec.dequeueOutputBuffer(info, timeoutUs);
- int outputBufferIndex2 = codec2.dequeueOutputBuffer(info2, timeoutUs2);
- if (outputBufferIndex >= 0) {
- if (info.size > 0) {
- noOutputCounter = 0;
- }
- if (info2.size > 0) {
- noOutputCounter2 = 0;
- }
- ByteBuffer buffer = codecOutputBuffers[outputBufferIndex];
- ByteBuffer buffer2 = codecOutputBuffers2[outputBufferIndex2];
- shortArrayOne = new short[info.size/2];
- shortArrayTwo = new short[info2.size/2];
- buffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArrayOne);
- buffer.clear();
- buffer2.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArrayTwo);
- buffer2.clear();
- shortArrayThree = mix(shortArrayOne, shortArrayTwo);
- if(shortArrayThree.length > 0){
- //audioTrack.write(shortArrayThree,0,shortArrayThree.length);
- ByteBuffer byteBuf = ByteBuffer.allocate(2*shortArrayThree.length);
- int index;
- for(index = 0; index != shortArrayThree.length; index++)
- {
- byteBuf.putShort(shortArrayThree[index]);
- }
- muxer.writeSampleData(mTrackIndex, byteBuf, info);
- }
- codec.releaseOutputBuffer(outputBufferIndex, false);
- codec2.releaseOutputBuffer(outputBufferIndex2, false);
- if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
- sawOutputEOS = true;
- }
- if ((info2.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
- sawOutputEOS2 = true;
- }
- } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
- codecOutputBuffers = codec.getOutputBuffers();
- if (outputBufferIndex2 == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
- codecOutputBuffers2 = codec2.getOutputBuffers();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement