diff --git a/app/src/main/java/com/frank/ffmpeg/hardware/HardwareDecode.java b/app/src/main/java/com/frank/ffmpeg/hardware/HardwareDecode.java index 1cef4dd..55ba7b8 100644 --- a/app/src/main/java/com/frank/ffmpeg/hardware/HardwareDecode.java +++ b/app/src/main/java/com/frank/ffmpeg/hardware/HardwareDecode.java @@ -3,6 +3,7 @@ package com.frank.ffmpeg.hardware; import android.media.MediaCodec; import android.media.MediaExtractor; import android.media.MediaFormat; +import android.os.SystemClock; import android.util.Log; import android.view.Surface; @@ -18,6 +19,7 @@ public class HardwareDecode { private final static String TAG = HardwareDecode.class.getSimpleName(); private final static long DEQUEUE_TIME = 10 * 1000; + private final static int SLEEP_TIME = 10; private final static int RATIO_1080 = 1080; private final static int RATIO_480 = 480; @@ -52,6 +54,12 @@ public class HardwareDecode { } } + public void setPreviewing(boolean previewing) { + if (videoDecodeThread != null) { + videoDecodeThread.setPreviewing(previewing); + } + } + public void release() { if (videoDecodeThread != null && !videoDecodeThread.isInterrupted()) { videoDecodeThread.interrupt(); @@ -66,6 +74,12 @@ public class HardwareDecode { private MediaCodec mediaCodec; + private boolean isPreviewing; + + void setPreviewing(boolean previewing) { + this.isPreviewing = previewing; + } + void seekTo(long seekPosition) { try { if (mediaExtractor != null) { @@ -90,6 +104,10 @@ public class HardwareDecode { } } + /** + * 根据原分辨率大小动态设置预览分辨率 + * @param mediaFormat mediaFormat + */ private void setPreviewRatio(MediaFormat mediaFormat) { if (mediaFormat == null) { return; @@ -154,6 +172,10 @@ public class HardwareDecode { ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers(); while (!isInterrupted()) { + if (!isPreviewing) { + SystemClock.sleep(SLEEP_TIME); + continue; + } //从缓冲区取出一个缓冲块,如果当前无可用缓冲块,返回inputIndex<0 int inputIndex = mediaCodec.dequeueInputBuffer(DEQUEUE_TIME); if (inputIndex >= 0) { diff --git a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java index 4b6ffb2..286407f 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java +++ b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java @@ -142,6 +142,9 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On isShowing = true; texturePreView.setVisibility(VISIBLE); } + if (hardwareDecode != null) { + hardwareDecode.setPreviewing(true); + } } @Override @@ -153,6 +156,9 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On if (mPreviewBarCallback != null) { mPreviewBarCallback.onStopTracking(seekBar.getProgress()); } + if (hardwareDecode != null) { + hardwareDecode.setPreviewing(false); + } } }); }