YeDaxia / MusicPlus

MusicPlus based MediaExtractor, MediaMuxer, and MediaCodec and other tools to achieve extraction of audio video, and then after the other audio mix, then a new audio to synthesize new video. If you are interested to convert between audio formats, you can also find the relevant code in them.
https://yedaxia.github.io/Android-MediaExtractor-And-MediaCodec/
Apache License 2.0
155 stars 38 forks source link

Mixing audio problem #4

Open basitali89hfi opened 6 years ago

basitali89hfi commented 6 years ago

When two audios are mixed, the last audio duration is always 15 seconds while the first one is correct. What will be the problem?

ahmadarslan13 commented 6 years ago

This question creates too much confusion When adding audio with the existing video it only write 15 seconds audio byte in the video. I tried alot to re understand the code but it is ok. Can you help me in this ?

I think the issue is only in the below code `if (!sawInputEOS) { inputBufIndex = audioEncoder.dequeueInputBuffer(10000); if (inputBufIndex >= 0) { ByteBuffer inputBuffer = audioInputBuffers[inputBufIndex]; inputBuffer.clear();

                           int bufferSize = inputBuffer.remaining();
                           if(bufferSize != extractAudioBuffer.length){
                               extractAudioBuffer = new byte[bufferSize];
                               mixAudioBuffer = new byte[bufferSize];
                           }

                           if(!readExtractAudioEOS){
                               extractAudioReadCount = fisExtractAudio.read(extractAudioBuffer);
                               if(extractAudioReadCount == -1){
                                   readExtractAudioEOS = true;
                               }
                           }

                           if(!readMixAudioEOS){
                               mixAudioReadCount = fisMixAudio.read(mixAudioBuffer);
                               if(mixAudioReadCount == -1){
                                   readMixAudioEOS = true;
                               }
                           }

                           if(readExtractAudioEOS && readMixAudioEOS){
                               audioEncoder.queueInputBuffer(inputBufIndex,0 , 0 , 0 , MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                               sawInputEOS = true;
                           }else{

                               byte[] mixAudioBytes;
                               if(!readExtractAudioEOS && !readMixAudioEOS){
                                   if(extractAudioReadCount  == mixAudioReadCount){
                                       twoAudioBytes[0] = extractAudioBuffer;
                                       twoAudioBytes[1] = mixAudioBuffer;
                                   }else if(extractAudioReadCount > mixAudioReadCount){
                                       twoAudioBytes[0] = extractAudioBuffer;
                                       Arrays.fill(mixAudioBuffer, mixAudioReadCount -1, bufferSize, (byte)0);
                                   }else{
                                       Arrays.fill(extractAudioBuffer, extractAudioReadCount -1, bufferSize, (byte)0);
                                   }
                                   mixAudioBytes = audioMixer.mixRawAudioBytes(twoAudioBytes);
                                   if(mixAudioBytes == null){
                                       DLog.e(TAG, "mix audio : null");
                                   }
                                   inputBuffer.put(mixAudioBytes);
                                   rawAudioSize += mixAudioBytes.length;
                                   audioEncoder.queueInputBuffer(inputBufIndex, 0, mixAudioBytes.length, audioTimeUs, 0);
                               }else if(!readExtractAudioEOS && readMixAudioEOS){
                                   inputBuffer.put(extractAudioBuffer, 0, extractAudioReadCount);
                                   rawAudioSize += extractAudioReadCount;
                                   audioEncoder.queueInputBuffer(inputBufIndex, 0, extractAudioReadCount, audioTimeUs, 0);
                               }else{
                                   inputBuffer.put(mixAudioBuffer, 0, mixAudioReadCount);
                                   rawAudioSize += mixAudioReadCount;
                                   audioEncoder.queueInputBuffer(inputBufIndex, 0, mixAudioReadCount, audioTimeUs, 0);
                               }

                               audioTimeUs = (long) (1000000 * (rawAudioSize / 2.0) / audioBytesPerSample);
                           }
                     }
                }`