diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java index 227b1ccc0..6cef9f6db 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java @@ -43,7 +43,8 @@ public class AudioDecoder extends BaseDecoder { private boolean muted = false; public AudioDecoder(GetMicrophoneData getMicrophoneData, - AudioDecoderInterface audioDecoderInterface) { + AudioDecoderInterface audioDecoderInterface, DecoderInterface decoderInterface) { + super(decoderInterface); TAG = "AudioDecoder"; this.getMicrophoneData = getMicrophoneData; this.audioDecoderInterface = audioDecoderInterface; diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java index 44543225c..98a95a8a9 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java @@ -55,6 +55,11 @@ public abstract class BaseDecoder { //Avoid decode while change output surface protected AtomicBoolean pause = new AtomicBoolean(false); protected volatile boolean looped = false; + private final DecoderInterface decoderInterface; + + public BaseDecoder(DecoderInterface decoderInterface) { + this.decoderInterface = decoderInterface; + } public boolean initExtractor(String filePath) throws IOException { extractor = new MediaExtractor(); @@ -242,6 +247,7 @@ private void decode() { moveTo(0); continue; } else { + decoderInterface.onLoop(); looped = false; } } @@ -281,8 +287,8 @@ private void decode() { } boolean render = decodeOutput(output); codec.releaseOutputBuffer(outIndex, render && bufferInfo.size != 0); - double reamingTime = getDuration() - getTime(); - if (reamingTime < 1 && loopMode) { + boolean finished = extractor.getSampleTime() < 0; + if (finished && loopMode) { moveTo(0); looped = true; } diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/DecoderInterface.kt b/encoder/src/main/java/com/pedro/encoder/input/decoder/DecoderInterface.kt new file mode 100644 index 000000000..ed661ed7e --- /dev/null +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/DecoderInterface.kt @@ -0,0 +1,8 @@ +package com.pedro.encoder.input.decoder + +/** + * Created by pedro on 20/10/23. + */ +interface DecoderInterface { + fun onLoop() +} \ No newline at end of file diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java index da37bee67..52a0c15b7 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java @@ -33,7 +33,8 @@ public class VideoDecoder extends BaseDecoder { private int height; private int fps; - public VideoDecoder(VideoDecoderInterface videoDecoderInterface) { + public VideoDecoder(VideoDecoderInterface videoDecoderInterface, DecoderInterface decoderInterface) { + super(decoderInterface); TAG = "VideoDecoder"; this.videoDecoderInterface = videoDecoderInterface; } diff --git a/library/src/main/java/com/pedro/library/base/FromFileBase.java b/library/src/main/java/com/pedro/library/base/FromFileBase.java index 17214868c..b4832bcbf 100644 --- a/library/src/main/java/com/pedro/library/base/FromFileBase.java +++ b/library/src/main/java/com/pedro/library/base/FromFileBase.java @@ -24,7 +24,6 @@ import android.media.MediaFormat; import android.net.Uri; import android.os.Build; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -36,6 +35,7 @@ import com.pedro.encoder.input.audio.GetMicrophoneData; import com.pedro.encoder.input.decoder.AudioDecoder; import com.pedro.encoder.input.decoder.AudioDecoderInterface; +import com.pedro.encoder.input.decoder.DecoderInterface; import com.pedro.encoder.input.decoder.VideoDecoder; import com.pedro.encoder.input.decoder.VideoDecoderInterface; import com.pedro.encoder.utils.CodecUtil; @@ -117,8 +117,8 @@ private void init(VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { videoEncoder = new VideoEncoder(getVideoData); audioEncoder = new AudioEncoder(getAacData); - videoDecoder = new VideoDecoder(videoDecoderInterface); - audioDecoder = new AudioDecoder(getMicrophoneData, audioDecoderInterface); + videoDecoder = new VideoDecoder(videoDecoderInterface, decoderInterface); + audioDecoder = new AudioDecoder(getMicrophoneData, audioDecoderInterface, decoderInterface); recordController = new AndroidMuxerRecordController(); } @@ -653,4 +653,20 @@ public void onVideoFormat(@NonNull MediaFormat mediaFormat) { recordController.setVideoFormat(mediaFormat, !audioEnabled); } }; + private final DecoderInterface decoderInterface = new DecoderInterface() { + + private int trackFinished = 0; + + @Override + public void onLoop() { + int maxTracks = 0; + if (audioEnabled) maxTracks++; + if (videoEnabled) maxTracks++; + trackFinished++; + if (trackFinished >= maxTracks) { + reSyncFile(); + trackFinished = 0; + } + } + }; }