diff --git a/Video.js b/Video.js index f32725f3..71f38723 100644 --- a/Video.js +++ b/Video.js @@ -11,7 +11,13 @@ const styles = StyleSheet.create({ }, }); -export { TextTrackType }; +const { + ExoPlayerConfig +} = NativeModules + +const VideoPlayerConfig = Platform.OS === "android" ? ExoPlayerConfig : undefined; + +export { TextTrackType, VideoPlayerConfig}; export default class Video extends Component { diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerConfig.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerConfig.java new file mode 100644 index 00000000..90c68d01 --- /dev/null +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerConfig.java @@ -0,0 +1,47 @@ +package com.brentvatne.exoplayer; + +import android.content.Context; + +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.Promise; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + + +public class ExoPlayerConfig extends ReactContextBaseJavaModule { + + public ExoPlayerConfig(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "ExoPlayerConfig"; + } + + @ReactMethod + public void setMinBufferMs(final int newMinBufferMs, final Promise promise) { + ReactExoplayerView.setMinBufferMs(newMinBufferMs); + promise.resolve(null); + } + + @ReactMethod + public void setMaxBufferMs(final int newMaxBufferMs, final Promise promise) { + ReactExoplayerView.setMaxBufferMs(newMaxBufferMs); + promise.resolve(null); + } + + @ReactMethod + public void setBufferForPlaybackMs(final int newBufferForPlaybackMs, final Promise promise) { + ReactExoplayerView.setBufferForPlaybackMs(newBufferForPlaybackMs); + promise.resolve(null); + } + + @ReactMethod + public void setBufferForPlaybackAfterRebufferMs(final int newBufferForPlaybackAfterRebufferMs, final Promise promise) { + ReactExoplayerView.setBufferForPlaybackAfterRebufferMs(newBufferForPlaybackAfterRebufferMs); + promise.resolve(null); + } +} 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 487da343..c401e3d2 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -58,6 +58,7 @@ import com.google.android.exoplayer2.trackselection.MappingTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultAllocator; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; @@ -90,6 +91,11 @@ class ReactExoplayerView extends FrameLayout implements DEFAULT_COOKIE_MANAGER.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); } + private static int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS; + private static int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS; + private static int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS; + private static int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS; + private final VideoEventEmitter eventEmitter; private Handler mainHandler; @@ -234,7 +240,9 @@ class ReactExoplayerView extends FrameLayout implements if (player == null) { TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); - player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, new DefaultLoadControl()); + DefaultAllocator allocator = new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE); + DefaultLoadControl defaultLoadControl = new DefaultLoadControl(allocator, minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, -1, true); + player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, defaultLoadControl); player.addListener(this); player.setMetadataOutput(this); exoPlayerView.setPlayer(player); @@ -931,4 +939,21 @@ class ReactExoplayerView extends FrameLayout implements public void setUseTextureView(boolean useTextureView) { exoPlayerView.setUseTextureView(useTextureView); } + + + public static void setMinBufferMs(int newMinBufferMs) { + minBufferMs = newMinBufferMs; + } + + public static void setMaxBufferMs(int newMaxBufferMs) { + maxBufferMs = newMaxBufferMs; + } + + public static void setBufferForPlaybackMs(int newBufferForPlaybackMs) { + bufferForPlaybackMs = newBufferForPlaybackMs; + } + + public static void setBufferForPlaybackAfterRebufferMs(int newBufferForPlaybackAfterRebufferMs) { + bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs; + } } diff --git a/android-exoplayer/src/main/java/com/brentvatne/react/ReactVideoPackage.java b/android-exoplayer/src/main/java/com/brentvatne/react/ReactVideoPackage.java index 286f972c..34ee588b 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/react/ReactVideoPackage.java +++ b/android-exoplayer/src/main/java/com/brentvatne/react/ReactVideoPackage.java @@ -1,12 +1,14 @@ package com.brentvatne.react; import com.brentvatne.exoplayer.ReactExoplayerViewManager; +import com.brentvatne.exoplayer.ExoPlayerConfig; import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -14,7 +16,11 @@ public class ReactVideoPackage implements ReactPackage { @Override public List createNativeModules(ReactApplicationContext reactContext) { - return Collections.emptyList(); + List modules = new ArrayList<>(); + + modules.add(new ExoPlayerConfig(reactContext)); + + return modules; } // Deprecated RN 0.47