diff --git a/android-exoplayer/build.gradle b/android-exoplayer/build.gradle index 32fe9318..b25b8164 100644 --- a/android-exoplayer/build.gradle +++ b/android-exoplayer/build.gradle @@ -12,8 +12,8 @@ android { dependencies { provided 'com.facebook.react:react-native:+' - compile 'com.google.android.exoplayer:exoplayer:r2.2.0' - compile('com.google.android.exoplayer:extension-okhttp:r2.2.0') { + compile 'com.google.android.exoplayer:exoplayer:r2.4.0' + compile('com.google.android.exoplayer:extension-okhttp:r2.4.0') { exclude group: 'com.squareup.okhttp3', module: 'okhttp' } compile 'com.squareup.okhttp3:okhttp:3.4.2' diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java index 4a270870..224a2d81 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java @@ -15,6 +15,7 @@ import android.widget.FrameLayout; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlayer; +import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.metadata.Metadata; @@ -225,6 +226,11 @@ public final class ExoPlayerView extends FrameLayout { updateForCurrentTrackSelections(); } + @Override + public void onPlaybackParametersChanged(PlaybackParameters params) { + // Do nothing + } + @Override public void onMetadata(Metadata metadata) { Log.d("onMetadata", "onMetadata"); @@ -232,4 +238,3 @@ public final class ExoPlayerView extends FrameLayout { } } - diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 85db1c93..52bf24e6 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -21,6 +21,7 @@ import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; @@ -38,7 +39,7 @@ import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource; import com.google.android.exoplayer2.source.hls.HlsMediaSource; import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource; import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; -import com.google.android.exoplayer2.trackselection.AdaptiveVideoTrackSelection; +import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.MappingTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; @@ -86,6 +87,7 @@ class ReactExoplayerView extends FrameLayout implements private boolean loadVideoStarted; private boolean isPaused = true; private boolean isBuffering; + private float rate = 1f; // Props from React private Uri srcUri; @@ -200,7 +202,7 @@ class ReactExoplayerView extends FrameLayout implements private void initializePlayer() { if (player == null) { - TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(BANDWIDTH_METER); + TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, new DefaultLoadControl()); player.addListener(this); @@ -209,6 +211,9 @@ class ReactExoplayerView extends FrameLayout implements audioBecomingNoisyReceiver.setListener(this); setPlayWhenReady(!isPaused); playerNeedsSource = true; + + PlaybackParameters params = new PlaybackParameters(rate, 1f); + player.setPlaybackParameters(params); } if (playerNeedsSource && srcUri != null) { MediaSource mediaSource = buildMediaSource(srcUri, extension); @@ -469,6 +474,11 @@ class ReactExoplayerView extends FrameLayout implements // Do Nothing. } + @Override + public void onPlaybackParametersChanged(PlaybackParameters params) { + eventEmitter.playbackRateChange(params.speed); + } + @Override public void onPlayerError(ExoPlaybackException e) { String errorString = null; @@ -605,9 +615,13 @@ class ReactExoplayerView extends FrameLayout implements } } - public void setRateModifier(float rate) { - // TODO: waiting on ExoPlayer implementation - // https://github.com/google/ExoPlayer/issues/26 + public void setRateModifier(float newRate) { + rate = newRate; + + if (player != null) { + PlaybackParameters params = new PlaybackParameters(rate, 1f); + player.setPlaybackParameters(params); + } } diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java index 6fe6ab61..b2ee4a15 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java @@ -39,6 +39,7 @@ class VideoEventEmitter { private static final String EVENT_TIMED_METADATA = "onTimedMetadata"; private static final String EVENT_AUDIO_BECOMING_NOISY = "onAudioBecomingNoisy"; private static final String EVENT_AUDIO_FOCUS_CHANGE = "onAudioFocusChanged"; + private static final String EVENT_PLAYBACK_RATE_CHANGE = "onPlaybackRateChange"; static final String[] Events = { EVENT_LOAD_START, @@ -55,6 +56,7 @@ class VideoEventEmitter { EVENT_TIMED_METADATA, EVENT_AUDIO_BECOMING_NOISY, EVENT_AUDIO_FOCUS_CHANGE, + EVENT_PLAYBACK_RATE_CHANGE, }; @Retention(RetentionPolicy.SOURCE) @@ -73,6 +75,7 @@ class VideoEventEmitter { EVENT_TIMED_METADATA, EVENT_AUDIO_BECOMING_NOISY, EVENT_AUDIO_FOCUS_CHANGE, + EVENT_PLAYBACK_RATE_CHANGE, }) @interface VideoEvents { } @@ -94,6 +97,7 @@ class VideoEventEmitter { private static final String EVENT_PROP_ORIENTATION = "orientation"; private static final String EVENT_PROP_HAS_AUDIO_FOCUS = "hasAudioFocus"; private static final String EVENT_PROP_IS_BUFFERING = "isBuffering"; + private static final String EVENT_PROP_PLAYBACK_RATE = "playbackRate"; private static final String EVENT_PROP_ERROR = "error"; private static final String EVENT_PROP_ERROR_STRING = "errorString"; @@ -178,6 +182,12 @@ class VideoEventEmitter { receiveEvent(EVENT_ERROR, event); } + void playbackRateChange(float rate) { + WritableMap map = Arguments.createMap(); + map.putDouble(EVENT_PROP_PLAYBACK_RATE, (double)rate); + receiveEvent(EVENT_PLAYBACK_RATE_CHANGE, map); + } + void timedMetadata(Metadata metadata) { WritableArray metadataArray = Arguments.createArray();