updated to ExoPlayer 2.4.0, added support for playback rate change (#605)

This commit is contained in:
Michal Drbohlav 2017-06-14 00:45:12 +02:00 committed by Matt Apperson
parent ef2120b418
commit 7f2cd8751a
4 changed files with 37 additions and 8 deletions

View File

@ -12,8 +12,8 @@ android {
dependencies { dependencies {
provided 'com.facebook.react:react-native:+' provided 'com.facebook.react:react-native:+'
compile 'com.google.android.exoplayer:exoplayer:r2.2.0' compile 'com.google.android.exoplayer:exoplayer:r2.4.0'
compile('com.google.android.exoplayer:extension-okhttp:r2.2.0') { compile('com.google.android.exoplayer:extension-okhttp:r2.4.0') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp' exclude group: 'com.squareup.okhttp3', module: 'okhttp'
} }
compile 'com.squareup.okhttp3:okhttp:3.4.2' compile 'com.squareup.okhttp3:okhttp:3.4.2'

View File

@ -15,6 +15,7 @@ import android.widget.FrameLayout;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.Metadata;
@ -225,6 +226,11 @@ public final class ExoPlayerView extends FrameLayout {
updateForCurrentTrackSelections(); updateForCurrentTrackSelections();
} }
@Override
public void onPlaybackParametersChanged(PlaybackParameters params) {
// Do nothing
}
@Override @Override
public void onMetadata(Metadata metadata) { public void onMetadata(Metadata metadata) {
Log.d("onMetadata", "onMetadata"); Log.d("onMetadata", "onMetadata");
@ -232,4 +238,3 @@ public final class ExoPlayerView extends FrameLayout {
} }
} }

View File

@ -21,6 +21,7 @@ import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; 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.hls.HlsMediaSource;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource; import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; 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.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector; import com.google.android.exoplayer2.trackselection.MappingTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
@ -86,6 +87,7 @@ class ReactExoplayerView extends FrameLayout implements
private boolean loadVideoStarted; private boolean loadVideoStarted;
private boolean isPaused = true; private boolean isPaused = true;
private boolean isBuffering; private boolean isBuffering;
private float rate = 1f;
// Props from React // Props from React
private Uri srcUri; private Uri srcUri;
@ -200,7 +202,7 @@ class ReactExoplayerView extends FrameLayout implements
private void initializePlayer() { private void initializePlayer() {
if (player == null) { if (player == null) {
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(BANDWIDTH_METER); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, new DefaultLoadControl()); player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, new DefaultLoadControl());
player.addListener(this); player.addListener(this);
@ -209,6 +211,9 @@ class ReactExoplayerView extends FrameLayout implements
audioBecomingNoisyReceiver.setListener(this); audioBecomingNoisyReceiver.setListener(this);
setPlayWhenReady(!isPaused); setPlayWhenReady(!isPaused);
playerNeedsSource = true; playerNeedsSource = true;
PlaybackParameters params = new PlaybackParameters(rate, 1f);
player.setPlaybackParameters(params);
} }
if (playerNeedsSource && srcUri != null) { if (playerNeedsSource && srcUri != null) {
MediaSource mediaSource = buildMediaSource(srcUri, extension); MediaSource mediaSource = buildMediaSource(srcUri, extension);
@ -469,6 +474,11 @@ class ReactExoplayerView extends FrameLayout implements
// Do Nothing. // Do Nothing.
} }
@Override
public void onPlaybackParametersChanged(PlaybackParameters params) {
eventEmitter.playbackRateChange(params.speed);
}
@Override @Override
public void onPlayerError(ExoPlaybackException e) { public void onPlayerError(ExoPlaybackException e) {
String errorString = null; String errorString = null;
@ -605,9 +615,13 @@ class ReactExoplayerView extends FrameLayout implements
} }
} }
public void setRateModifier(float rate) { public void setRateModifier(float newRate) {
// TODO: waiting on ExoPlayer implementation rate = newRate;
// https://github.com/google/ExoPlayer/issues/26
if (player != null) {
PlaybackParameters params = new PlaybackParameters(rate, 1f);
player.setPlaybackParameters(params);
}
} }

View File

@ -39,6 +39,7 @@ class VideoEventEmitter {
private static final String EVENT_TIMED_METADATA = "onTimedMetadata"; private static final String EVENT_TIMED_METADATA = "onTimedMetadata";
private static final String EVENT_AUDIO_BECOMING_NOISY = "onAudioBecomingNoisy"; private static final String EVENT_AUDIO_BECOMING_NOISY = "onAudioBecomingNoisy";
private static final String EVENT_AUDIO_FOCUS_CHANGE = "onAudioFocusChanged"; private static final String EVENT_AUDIO_FOCUS_CHANGE = "onAudioFocusChanged";
private static final String EVENT_PLAYBACK_RATE_CHANGE = "onPlaybackRateChange";
static final String[] Events = { static final String[] Events = {
EVENT_LOAD_START, EVENT_LOAD_START,
@ -55,6 +56,7 @@ class VideoEventEmitter {
EVENT_TIMED_METADATA, EVENT_TIMED_METADATA,
EVENT_AUDIO_BECOMING_NOISY, EVENT_AUDIO_BECOMING_NOISY,
EVENT_AUDIO_FOCUS_CHANGE, EVENT_AUDIO_FOCUS_CHANGE,
EVENT_PLAYBACK_RATE_CHANGE,
}; };
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@ -73,6 +75,7 @@ class VideoEventEmitter {
EVENT_TIMED_METADATA, EVENT_TIMED_METADATA,
EVENT_AUDIO_BECOMING_NOISY, EVENT_AUDIO_BECOMING_NOISY,
EVENT_AUDIO_FOCUS_CHANGE, EVENT_AUDIO_FOCUS_CHANGE,
EVENT_PLAYBACK_RATE_CHANGE,
}) })
@interface VideoEvents { @interface VideoEvents {
} }
@ -94,6 +97,7 @@ class VideoEventEmitter {
private static final String EVENT_PROP_ORIENTATION = "orientation"; private static final String EVENT_PROP_ORIENTATION = "orientation";
private static final String EVENT_PROP_HAS_AUDIO_FOCUS = "hasAudioFocus"; 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_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 = "error";
private static final String EVENT_PROP_ERROR_STRING = "errorString"; private static final String EVENT_PROP_ERROR_STRING = "errorString";
@ -178,6 +182,12 @@ class VideoEventEmitter {
receiveEvent(EVENT_ERROR, event); 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) { void timedMetadata(Metadata metadata) {
WritableArray metadataArray = Arguments.createArray(); WritableArray metadataArray = Arguments.createArray();