Skip to content

Commit

Permalink
fix: should seek to exact end time on media completed
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 authored Sep 29, 2024
1 parent 1db01c6 commit 4bed8f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
34 changes: 10 additions & 24 deletions android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ private void updateHandlePositions() {
}

private void mediaCompleted() {
setPlayPauseViewIcon(false);
mTimingHandler.removeCallbacks(mTimingRunnable);
onMediaPause();

// when mediaCompleted is called, the endTime may not be exactly at the end of the video (can be slightly before), therefore we should seek to exact position on ended
seekTo(endTime, true);
}

private void playOrPause() {
Expand All @@ -345,11 +347,9 @@ private void playOrPause() {
}

public void onMediaPause() {
if (mediaPlayer.isPlaying()) {
mTimingHandler.removeCallbacks(mTimingRunnable);
mediaPlayer.pause();
setPlayPauseViewIcon(false);
}
mTimingHandler.removeCallbacks(mTimingRunnable);
mediaPlayer.pause();
setPlayPauseViewIcon(false);
}

public void setOnTrimVideoListener(VideoTrimListener onTrimVideoListener) {
Expand Down Expand Up @@ -521,27 +521,13 @@ private void startTimingRunnable() {
mTimingRunnable = new Runnable() {
@Override
public void run() {
// prevent crashing when video is playing and we close editor
try {
if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
mTimingHandler.removeCallbacks(mTimingRunnable);
return;
}
updateCurrentTime(true);
mTimingHandler.postDelayed(this, TIMING_UPDATE_INTERVAL);
} catch (IllegalStateException e) {
e.printStackTrace();
mTimingHandler.removeCallbacks(mTimingRunnable);
Log.d(TAG, "startTimingRunnable mediaPlayer is already released");
return;
}

int currentPosition = mediaPlayer.getCurrentPosition();

if (currentPosition >= endTime) {
onMediaPause();
seekTo(endTime, true); // Ensure exact end time display
} else {
updateCurrentTime(true);
mTimingHandler.postDelayed(this, TIMING_UPDATE_INTERVAL);
// this is to catch the error thrown if we close editor while playing (mediaPlayer is released)
}
}
};
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.5",
"version": "2.2.6",
"description": "Video trimmer for your React Native app",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 4bed8f9

Please sign in to comment.