Skip to content

Commit

Permalink
Merge pull request #1351 from react-native-community/bugfix/android-s…
Browse files Browse the repository at this point in the history
…eek-event

Fire onSeek after seek completes on Android, document onSeek
  • Loading branch information
cobarx authored Nov 28, 2018
2 parents 5e5315b + fcee7b6 commit e1c1eb6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ var styles = StyleSheet.create({
* [onLoad](#onload)
* [onLoadStart](#onloadstart)
* [onProgress](#onprogress)
* [onSeek](#onseek)
* [onTimedMetadata](#ontimedmetadata)

### Methods
Expand Down Expand Up @@ -848,6 +849,29 @@ Example:

Platforms: all

#### onSeek
Callback function that is called when a seek completes.

Payload:

Property | Type | Description
--- | --- | ---
currentTime | number | The current time after the seek
seekTime | number | The requested time

Example:
```
{
currentTime: 100.5
seekTime: 100
}
```

Both the currentTime & seekTime are reported because the video player may not seek to the exact requested position in order to improve seek performance.


Platforms: Android ExoPlayer, Android MediaPlayer, iOS, Windows UWP

#### onTimedMetadata
Callback function that is called when timed metadata becomes available

Expand Down Expand Up @@ -941,7 +965,7 @@ Platforms: iOS

Seek to the specified position represented by seconds. seconds is a float value.

`seek()` can only be called after the `onLoad` event has fired.
`seek()` can only be called after the `onLoad` event has fired. Once completed, the [onSeek](#onseek) event will be called.

Example:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ReactExoplayerView extends FrameLayout implements
private boolean isBuffering;
private float rate = 1f;
private float audioVolume = 1f;
private long seekTime = C.TIME_UNSET;

private int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS;
private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
Expand Down Expand Up @@ -605,7 +606,8 @@ public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

@Override
public void onSeekProcessed() {
// Do nothing.
eventEmitter.seek(player.getCurrentPosition(), seekTime);
seekTime = C.TIME_UNSET;
}

@Override
Expand Down Expand Up @@ -892,7 +894,7 @@ public void setVolumeModifier(float volume) {

public void seekTo(long positionMs) {
if (player != null) {
eventEmitter.seek(player.getCurrentPosition(), positionMs);
seekTime = positionMs;
player.seekTo(positionMs);
}
}
Expand Down
18 changes: 12 additions & 6 deletions android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ReactVideoView extends ScalableVideoView implements
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnSeekCompleteListener,
MediaPlayer.OnCompletionListener,
MediaPlayer.OnInfoListener,
LifecycleEventListener,
Expand Down Expand Up @@ -127,6 +128,7 @@ public String toString() {
private float mProgressUpdateInterval = 250.0f;
private float mRate = 1.0f;
private float mActiveRate = 1.0f;
private long mSeekTime = 0;
private boolean mPlayInBackground = false;
private boolean mBackgroundPaused = false;
private boolean mIsFullscreen = false;
Expand Down Expand Up @@ -213,6 +215,7 @@ private void initializeMediaPlayerIfNeeded() {
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnSeekCompleteListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnInfoListener(this);
if (Build.VERSION.SDK_INT >= 23) {
Expand Down Expand Up @@ -606,15 +609,18 @@ public void onBufferingUpdate(MediaPlayer mp, int percent) {
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
}

public void onSeekComplete(MediaPlayer mp) {
WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_SEEK_TIME, mSeekTime / 1000.0);
mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event);
mSeekTime = 0;
}

@Override
public void seekTo(int msec) {

if (mMediaPlayerValid) {
WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_SEEK_TIME, msec / 1000.0);
mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event);

mSeekTime = msec;
super.seekTo(msec);
if (isCompleted && mVideoDuration != 0 && msec < mVideoDuration) {
isCompleted = false;
Expand Down

0 comments on commit e1c1eb6

Please sign in to comment.