Skip to content

Commit 61e6535

Browse files
authored
Feature/toggle buffering (#3)
This PR adds the property disableBuffering: boolean for android. The PR was initally created as a personal fork and referenced in crunchyroll/vilos#1227 also updated RNVLoadControl constructor and method to reflect new ExoPlayer.LoadControl api related ticket: https://jira.tenkasu.net/browse/VEX-3776
1 parent 6100981 commit 61e6535

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

Video.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ Video.propTypes = {
470470
ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']),
471471
reportBandwidth: PropTypes.bool,
472472
disableFocus: PropTypes.bool,
473+
disableBuffering: PropTypes.bool,
473474
controls: PropTypes.bool,
474475
audioOnly: PropTypes.bool,
475476
currentTime: PropTypes.number,

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.facebook.react.bridge.WritableArray;
2727
import com.facebook.react.bridge.WritableMap;
2828
import com.facebook.react.uimanager.ThemedReactContext;
29+
import com.facebook.react.util.RNLog;
2930
import com.google.android.exoplayer2.C;
3031
import com.google.android.exoplayer2.DefaultLoadControl;
3132
import com.google.android.exoplayer2.DefaultRenderersFactory;
@@ -70,6 +71,7 @@
7071
import com.google.android.exoplayer2.upstream.DefaultAllocator;
7172
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
7273
import com.google.android.exoplayer2.upstream.HttpDataSource;
74+
import com.google.android.exoplayer2.util.Assertions;
7375
import com.google.android.exoplayer2.util.Util;
7476

7577
import java.net.CookieHandler;
@@ -148,6 +150,7 @@ class ReactExoplayerView extends FrameLayout implements
148150
private Dynamic textTrackValue;
149151
private ReadableArray textTracks;
150152
private boolean disableFocus;
153+
private boolean disableBuffering;
151154
private boolean preventsDisplaySleepDuringVideoPlayback = true;
152155
private float mProgressUpdateInterval = 250.0f;
153156
private boolean playInBackground = false;
@@ -183,7 +186,7 @@ public void handleMessage(Message msg) {
183186
}
184187
}
185188
};
186-
189+
187190
public double getPositionInFirstPeriodMsForCurrentWindow(long currentPosition) {
188191
Timeline.Window window = new Timeline.Window();
189192
if(!player.getCurrentTimeline().isEmpty()) {
@@ -388,6 +391,28 @@ private void reLayout(View view) {
388391
view.layout(view.getLeft(), view.getTop(), view.getMeasuredWidth(), view.getMeasuredHeight());
389392
}
390393

394+
private class RNVLoadControl extends DefaultLoadControl {
395+
public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs, int bufferForPlaybackMs, int bufferForPlaybackAfterRebufferMs, int targetBufferBytes, boolean prioritizeTimeOverSizeThresholds, int backBufferDurationMs, boolean retainBackBufferFromKeyframe) {
396+
super(allocator,
397+
minBufferMs,
398+
maxBufferMs,
399+
bufferForPlaybackMs,
400+
bufferForPlaybackAfterRebufferMs,
401+
targetBufferBytes,
402+
prioritizeTimeOverSizeThresholds,
403+
backBufferDurationMs,
404+
retainBackBufferFromKeyframe);
405+
}
406+
407+
@Override
408+
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
409+
if (ReactExoplayerView.this.disableBuffering) {
410+
return false;
411+
}
412+
return super.shouldContinueLoading(playbackPositionUs, bufferedDurationUs, playbackSpeed);
413+
}
414+
}
415+
391416
private void initializePlayer() {
392417
ReactExoplayerView self = this;
393418
// This ensures all props have been settled, to avoid async racing conditions.
@@ -401,19 +426,24 @@ public void run() {
401426
.setMaxVideoBitrate(maxBitRate == 0 ? Integer.MAX_VALUE : maxBitRate));
402427

403428
DefaultAllocator allocator = new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE);
404-
DefaultLoadControl.Builder defaultLoadControlBuilder = new DefaultLoadControl.Builder();
405-
defaultLoadControlBuilder.setAllocator(allocator);
406-
defaultLoadControlBuilder.setBufferDurationsMs(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs);
407-
defaultLoadControlBuilder.setTargetBufferBytes(-1);
408-
defaultLoadControlBuilder.setPrioritizeTimeOverSizeThresholds(true);
409-
DefaultLoadControl defaultLoadControl = defaultLoadControlBuilder.createDefaultLoadControl();
429+
RNVLoadControl loadControl = new RNVLoadControl(
430+
allocator,
431+
minBufferMs,
432+
maxBufferMs,
433+
bufferForPlaybackMs,
434+
bufferForPlaybackAfterRebufferMs,
435+
-1,
436+
true,
437+
DefaultLoadControl.DEFAULT_BACK_BUFFER_DURATION_MS,
438+
DefaultLoadControl.DEFAULT_RETAIN_BACK_BUFFER_FROM_KEYFRAME
439+
);
410440
DefaultRenderersFactory renderersFactory =
411441
new DefaultRenderersFactory(getContext())
412442
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF);
413443
player = new SimpleExoPlayer.Builder(getContext(), renderersFactory)
414444
.setTrackSelector​(trackSelector)
415445
.setBandwidthMeter(bandwidthMeter)
416-
.setLoadControl(defaultLoadControl)
446+
.setLoadControl(loadControl)
417447
.build();
418448
player.addListener(self);
419449
player.addMetadataOutput(self);
@@ -1282,6 +1312,10 @@ public void setDisableFocus(boolean disableFocus) {
12821312
this.disableFocus = disableFocus;
12831313
}
12841314

1315+
public void setDisableBuffering(boolean disableBuffering) {
1316+
this.disableBuffering = disableBuffering;
1317+
}
1318+
12851319
public void setFullscreen(boolean fullscreen) {
12861320
if (fullscreen == isFullscreen) {
12871321
return; // Avoid generating events when nothing is changing

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
6363
private static final String PROP_MAXIMUM_BIT_RATE = "maxBitRate";
6464
private static final String PROP_PLAY_IN_BACKGROUND = "playInBackground";
6565
private static final String PROP_DISABLE_FOCUS = "disableFocus";
66+
private static final String PROP_DISABLE_BUFFERING = "disableBuffering";
6667
private static final String PROP_FULLSCREEN = "fullscreen";
6768
private static final String PROP_USE_TEXTURE_VIEW = "useTextureView";
6869
private static final String PROP_SELECTED_VIDEO_TRACK = "selectedVideoTrack";
@@ -293,6 +294,11 @@ public void setDisableFocus(final ReactExoplayerView videoView, final boolean di
293294
videoView.setDisableFocus(disableFocus);
294295
}
295296

297+
@ReactProp(name = PROP_DISABLE_BUFFERING, defaultBoolean = false)
298+
public void setDisableBuffering(final ReactExoplayerView videoView, final boolean disableBuffering) {
299+
videoView.setDisableBuffering(disableBuffering);
300+
}
301+
296302
@ReactProp(name = PROP_FULLSCREEN, defaultBoolean = false)
297303
public void setFullscreen(final ReactExoplayerView videoView, final boolean fullscreen) {
298304
videoView.setFullscreen(fullscreen);

0 commit comments

Comments
 (0)