Skip to content

Commit

Permalink
fix(android): position starts earlier after seeking to a position
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 authored Sep 28, 2024
1 parent b15f733 commit c004525
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
}

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit c004525

Please sign in to comment.