From c00452563e7634cfb094222722d9ae81e4de0473 Mon Sep 17 00:00:00 2001 From: Duc Trung Mai Date: Sat, 28 Sep 2024 22:19:03 +0800 Subject: [PATCH] fix(android): position starts earlier after seeking to a position --- .../videotrim/widgets/VideoTrimmerView.java | 24 +++++++++++++++---- package.json | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java b/android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java index 55f160c..ad5bcdd 100644 --- a/android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java +++ b/android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java @@ -56,6 +56,9 @@ public class VideoTrimmerView extends FrameLayout implements IVideoTrimmerView { private ReactApplicationContext mContext; private VideoView mVideoView; + // https://stackoverflow.com/a/73361868/7569705 + // the videoPlayer is to solve the issue after manually seek -> hit play -> it starts from a position slightly before with the one we just sought to + private MediaPlayer videoPlayer; private ImageView mPlayView; private LinearLayout mThumbnailContainer; private Uri mSourceUri; @@ -110,6 +113,7 @@ public class VideoTrimmerView extends FrameLayout implements IVideoTrimmerView { private String alertOnFailTitle = "Error"; private String alertOnFailMessage = "Fail to load media. Possibly invalid file or no network connection"; private String alertOnFailCloseText = "Close"; + private View currentSelectedhandle; public VideoTrimmerView(ReactApplicationContext context, ReadableMap config, AttributeSet attrs) { this(context, attrs, 0, config); @@ -172,6 +176,7 @@ public void initByURI(final Uri videoURI) { mVideoView.setOnPreparedListener(mp -> { mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT); mediaPrepared(); + videoPlayer = mp; }); mVideoView.setOnErrorListener(this::onFailToLoadMedia); @@ -403,7 +408,11 @@ public void onCancelTrimClicked() { private void seekTo(long msec, boolean needUpdateProgress) { if (isVideoType) { - mVideoView.seekTo((int) msec); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + videoPlayer.seekTo((int) msec, MediaPlayer.SEEK_CLOSEST); + } else { + mVideoView.seekTo((int) msec); + } } else { audioPlayer.seekTo((int) msec); } @@ -568,6 +577,8 @@ private void updateCurrentTime(boolean needUpdateProgress) { currentPosition = duration; } else if (currentPosition >= endTime - 100) { currentPosition = (int) endTime; + } else if (currentPosition <= startTime + 100) { + currentPosition = (int) startTime; } String currentTime = formatTime(currentPosition); @@ -583,9 +594,13 @@ private void updateCurrentTime(boolean needUpdateProgress) { // Update progressIndicator position float indicatorPosition = (float) currentPosition / duration * (trimmerContainerBg.getWidth() - progressIndicator.getWidth()) + leadingHandle.getWidth(); - float rightBoundary = trimmerContainer.getX() + trimmerContainer.getWidth() - progressIndicator.getWidth(); - - progressIndicator.setX(Math.min(rightBoundary, indicatorPosition)); + if (currentSelectedhandle == leadingHandle) { + float leftBoundary = trimmerContainer.getX(); + progressIndicator.setX(Math.max(leftBoundary, indicatorPosition)); + } else { + float rightBoundary = trimmerContainer.getX() + trimmerContainer.getWidth() - progressIndicator.getWidth(); + progressIndicator.setX(Math.min(rightBoundary, indicatorPosition)); + } } } @@ -655,6 +670,7 @@ private void setHandleTouchListener(View handle, boolean isLeading) { handle.setOnTouchListener((view, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: + currentSelectedhandle = handle; didClampWhilePanning = false; onMediaPause(); fadeOutProgressIndicator(); diff --git a/package.json b/package.json index eb69eaf..b13bde4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-video-trim", - "version": "2.2.3", + "version": "2.2.4", "description": "Video trimmer for your React Native app", "main": "lib/commonjs/index", "module": "lib/module/index",