Skip to content

Commit e015701

Browse files
author
Shahen Hovhannisyan
committed
fix(VideoPlayer): Fixed replay issue on android
1 parent 59d2e10 commit e015701

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerView.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ public class VideoPlayerView extends ScalableVideoView implements
6161
private ThemedReactContext themedReactContext;
6262
private RCTEventEmitter eventEmitter;
6363
private String mediaSource;
64-
private boolean playerPlaying = true;
64+
private boolean mPlay = true;
6565
private String LOG_TAG = "RNVideoProcessing";
6666
private Runnable progressRunnable = null;
6767
private Handler progressUpdateHandler = new Handler();
6868
private FFmpegMediaMetadataRetriever metadataRetriever = new FFmpegMediaMetadataRetriever();
6969
private int progressUpdateHandlerDelay = 1000;
7070
private int videoStartAt = 0;
7171
private int videoEndAt = -1;
72+
private boolean mLooping = false;
73+
private float mVolume = 10f;
7274

7375

7476
public VideoPlayerView(ThemedReactContext ctx) {
@@ -86,8 +88,8 @@ public void run() {
8688
if (mMediaPlayer.getCurrentPosition() >= videoEndAt && videoEndAt != -1) {
8789
Log.d(LOG_TAG, "run: End time reached");
8890
mMediaPlayer.seekTo(videoStartAt);
89-
if (!mMediaPlayer.isLooping()) {
90-
Log.d(LOG_TAG, "run: Set loop");
91+
if (!mLooping) {
92+
Log.d(LOG_TAG, "run: Pause video, no looping");
9193
pause();
9294
}
9395
}
@@ -145,7 +147,7 @@ public void setSource(final String uriString) {
145147
}
146148
prepare(this);
147149

148-
if (playerPlaying && !mMediaPlayer.isPlaying()) {
150+
if (mPlay && !mMediaPlayer.isPlaying()) {
149151
Log.d(LOG_TAG, "setSource: Start video at once");
150152
start();
151153
}
@@ -158,7 +160,7 @@ public void setSource(final String uriString) {
158160

159161
public void setPlay(final boolean shouldPlay) {
160162
Log.d(LOG_TAG, "setPlay: " + shouldPlay);
161-
playerPlaying = shouldPlay;
163+
mPlay = shouldPlay;
162164

163165
if (mMediaPlayer == null) {
164166
Log.d(LOG_TAG, "setPlay: Player reference is null");
@@ -178,6 +180,7 @@ public void setMediaVolume(float volume) {
178180
if (volume < 0) {
179181
return;
180182
}
183+
mVolume = volume;
181184
if (mMediaPlayer == null) {
182185
return;
183186
}
@@ -198,6 +201,28 @@ public void setCurrentTime(float currentTime) {
198201
seekTo((int) seekTime);
199202
}
200203

204+
public void setRepeat(boolean repeat) {
205+
mLooping = repeat;
206+
207+
if (mMediaPlayer == null) {
208+
return;
209+
}
210+
mMediaPlayer.setLooping(mLooping);
211+
}
212+
213+
private void applyProps() {
214+
if (mMediaPlayer == null) {
215+
Log.d(LOG_TAG, "applyProps: MediaPlayer is null");
216+
}
217+
if (!mMediaPlayer.isLooping()) {
218+
mMediaPlayer.setLooping(mLooping);
219+
}
220+
if (mPlay && !isPlaying()) {
221+
mMediaPlayer.start();
222+
}
223+
mMediaPlayer.setVolume(mVolume, mVolume);
224+
}
225+
201226
public void setVideoEndAt(int endAt) {
202227
videoEndAt = endAt * 1000;
203228
if (mMediaPlayer == null) {
@@ -357,7 +382,8 @@ public void onPrepared(MediaPlayer mp) {
357382
videoEndAt = mp.getDuration();
358383
setScalableType(ScalableType.FIT_XY);
359384
invalidate();
360-
Log.d(LOG_TAG, "onPrepared: " + videoEndAt);
385+
386+
applyProps();
361387
}
362388

363389
@Override
@@ -371,7 +397,7 @@ public void onCompletion(MediaPlayer mp) {
371397
return;
372398
}
373399
Log.d(LOG_TAG, "onCompletion: isLooping " + mp.isLooping());
374-
if (mp.isLooping()) {
400+
if (mLooping) {
375401
Log.d(LOG_TAG, "onCompletion: seek to start at : " + videoStartAt);
376402
mp.seekTo(videoStartAt);
377403
}

android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerViewManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void setPlay(final VideoPlayerView player, boolean shouldPlay) {
143143
@ReactProp(name = SET_REPLAY, defaultBoolean = true)
144144
public void setReplay(final VideoPlayerView player, boolean replay) {
145145
Log.d(VideoPlayerViewManager.REACT_PACKAGE, "set replay: " + String.valueOf(replay));
146-
player.setLooping(replay);
146+
player.setRepeat(replay);
147147
}
148148

149149
@ReactProp(name = SET_VOLUME, defaultFloat = 10f)

0 commit comments

Comments
 (0)