/
ExoPlayer.java
1678 lines (1528 loc) · 65.4 KB
/
ExoPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.os.Looper;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.analytics.AnalyticsCollector;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.google.android.exoplayer2.analytics.DefaultAnalyticsCollector;
import com.google.android.exoplayer2.audio.AudioAttributes;
import com.google.android.exoplayer2.audio.AudioSink;
import com.google.android.exoplayer2.audio.AuxEffectInfo;
import com.google.android.exoplayer2.audio.DefaultAudioSink;
import com.google.android.exoplayer2.audio.MediaCodecAudioRenderer;
import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.metadata.MetadataRenderer;
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ShuffleOrder;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.text.CueGroup;
import com.google.android.exoplayer2.text.TextRenderer;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.PriorityTaskManager;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.MediaCodecVideoRenderer;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import com.google.android.exoplayer2.video.VideoSize;
import com.google.android.exoplayer2.video.spherical.CameraMotionListener;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.List;
/**
* An extensible media player that plays {@link MediaSource}s. Instances can be obtained from {@link
* Builder}.
*
* <h2>Player components</h2>
*
* <p>ExoPlayer is designed to make few assumptions about (and hence impose few restrictions on) the
* type of the media being played, how and where it is stored, and how it is rendered. Rather than
* implementing the loading and rendering of media directly, ExoPlayer implementations delegate this
* work to components that are injected when a player is created or when it's prepared for playback.
* Components common to all ExoPlayer implementations are:
*
* <ul>
* <li><b>{@link MediaSource MediaSources}</b> that define the media to be played, load the media,
* and from which the loaded media can be read. MediaSources are created from {@link MediaItem
* MediaItems} by the {@link MediaSource.Factory} injected into the player {@link
* Builder#setMediaSourceFactory Builder}, or can be added directly by methods like {@link
* #setMediaSource(MediaSource)}. The library provides a {@link DefaultMediaSourceFactory} for
* progressive media files, DASH, SmoothStreaming and HLS, which also includes functionality
* for side-loading subtitle files and clipping media.
* <li><b>{@link Renderer}</b>s that render individual components of the media. The library
* provides default implementations for common media types ({@link MediaCodecVideoRenderer},
* {@link MediaCodecAudioRenderer}, {@link TextRenderer} and {@link MetadataRenderer}). A
* Renderer consumes media from the MediaSource being played. Renderers are injected when the
* player is created. The number of renderers and their respective track types can be obtained
* by calling {@link #getRendererCount()} and {@link #getRendererType(int)}.
* <li>A <b>{@link TrackSelector}</b> that selects tracks provided by the MediaSource to be
* consumed by each of the available Renderers. The library provides a default implementation
* ({@link DefaultTrackSelector}) suitable for most use cases. A TrackSelector is injected
* when the player is created.
* <li>A <b>{@link LoadControl}</b> that controls when the MediaSource buffers more media, and how
* much media is buffered. The library provides a default implementation ({@link
* DefaultLoadControl}) suitable for most use cases. A LoadControl is injected when the player
* is created.
* </ul>
*
* <p>An ExoPlayer can be built using the default components provided by the library, but may also
* be built using custom implementations if non-standard behaviors are required. For example a
* custom LoadControl could be injected to change the player's buffering strategy, or a custom
* Renderer could be injected to add support for a video codec not supported natively by Android.
*
* <p>The concept of injecting components that implement pieces of player functionality is present
* throughout the library. The default component implementations listed above delegate work to
* further injected components. This allows many sub-components to be individually replaced with
* custom implementations. For example the default MediaSource implementations require one or more
* {@link DataSource} factories to be injected via their constructors. By providing a custom factory
* it's possible to load data from a non-standard source, or through a different network stack.
*
* <h2>Threading model</h2>
*
* <p>The figure below shows ExoPlayer's threading model.
*
* <p style="align:center"><img src="doc-files/exoplayer-threading-model.svg" alt="ExoPlayer's
* threading model">
*
* <ul>
* <li>ExoPlayer instances must be accessed from a single application thread. For the vast
* majority of cases this should be the application's main thread. Using the application's
* main thread is also a requirement when using ExoPlayer's UI components or the IMA
* extension. The thread on which an ExoPlayer instance must be accessed can be explicitly
* specified by passing a `Looper` when creating the player. If no `Looper` is specified, then
* the `Looper` of the thread that the player is created on is used, or if that thread does
* not have a `Looper`, the `Looper` of the application's main thread is used. In all cases
* the `Looper` of the thread from which the player must be accessed can be queried using
* {@link #getApplicationLooper()}.
* <li>Registered listeners are called on the thread associated with {@link
* #getApplicationLooper()}. Note that this means registered listeners are called on the same
* thread which must be used to access the player.
* <li>An internal playback thread is responsible for playback. Injected player components such as
* Renderers, MediaSources, TrackSelectors and LoadControls are called by the player on this
* thread.
* <li>When the application performs an operation on the player, for example a seek, a message is
* delivered to the internal playback thread via a message queue. The internal playback thread
* consumes messages from the queue and performs the corresponding operations. Similarly, when
* a playback event occurs on the internal playback thread, a message is delivered to the
* application thread via a second message queue. The application thread consumes messages
* from the queue, updating the application visible state and calling corresponding listener
* methods.
* <li>Injected player components may use additional background threads. For example a MediaSource
* may use background threads to load data. These are implementation specific.
* </ul>
*/
public interface ExoPlayer extends Player {
/**
* @deprecated Use {@link ExoPlayer}, as the {@link AudioComponent} methods are defined by that
* interface.
*/
@Deprecated
interface AudioComponent {
/**
* @deprecated Use {@link ExoPlayer#setAudioAttributes(AudioAttributes, boolean)} instead.
*/
@Deprecated
void setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus);
/**
* @deprecated Use {@link Player#getAudioAttributes()} instead.
*/
@Deprecated
AudioAttributes getAudioAttributes();
/**
* @deprecated Use {@link ExoPlayer#setAudioSessionId(int)} instead.
*/
@Deprecated
void setAudioSessionId(int audioSessionId);
/**
* @deprecated Use {@link ExoPlayer#getAudioSessionId()} instead.
*/
@Deprecated
int getAudioSessionId();
/**
* @deprecated Use {@link ExoPlayer#setAuxEffectInfo(AuxEffectInfo)} instead.
*/
@Deprecated
void setAuxEffectInfo(AuxEffectInfo auxEffectInfo);
/**
* @deprecated Use {@link ExoPlayer#clearAuxEffectInfo()} instead.
*/
@Deprecated
void clearAuxEffectInfo();
/**
* @deprecated Use {@link Player#setVolume(float)} instead.
*/
@Deprecated
void setVolume(float audioVolume);
/**
* @deprecated Use {@link Player#getVolume()} instead.
*/
@Deprecated
float getVolume();
/**
* @deprecated Use {@link ExoPlayer#setSkipSilenceEnabled(boolean)} instead.
*/
@Deprecated
void setSkipSilenceEnabled(boolean skipSilenceEnabled);
/**
* @deprecated Use {@link ExoPlayer#getSkipSilenceEnabled()} instead.
*/
@Deprecated
boolean getSkipSilenceEnabled();
}
/**
* @deprecated Use {@link ExoPlayer}, as the {@link VideoComponent} methods are defined by that
* interface.
*/
@Deprecated
interface VideoComponent {
/**
* @deprecated Use {@link ExoPlayer#setVideoScalingMode(int)} instead.
*/
@Deprecated
void setVideoScalingMode(@C.VideoScalingMode int videoScalingMode);
/**
* @deprecated Use {@link ExoPlayer#getVideoScalingMode()} instead.
*/
@Deprecated
@C.VideoScalingMode
int getVideoScalingMode();
/**
* @deprecated Use {@link ExoPlayer#setVideoChangeFrameRateStrategy(int)} instead.
*/
@Deprecated
void setVideoChangeFrameRateStrategy(
@C.VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy);
/**
* @deprecated Use {@link ExoPlayer#getVideoChangeFrameRateStrategy()} instead.
*/
@Deprecated
@C.VideoChangeFrameRateStrategy
int getVideoChangeFrameRateStrategy();
/**
* @deprecated Use {@link ExoPlayer#setVideoFrameMetadataListener(VideoFrameMetadataListener)}
* instead.
*/
@Deprecated
void setVideoFrameMetadataListener(VideoFrameMetadataListener listener);
/**
* @deprecated Use {@link ExoPlayer#clearVideoFrameMetadataListener(VideoFrameMetadataListener)}
* instead.
*/
@Deprecated
void clearVideoFrameMetadataListener(VideoFrameMetadataListener listener);
/**
* @deprecated Use {@link ExoPlayer#setCameraMotionListener(CameraMotionListener)} instead.
*/
@Deprecated
void setCameraMotionListener(CameraMotionListener listener);
/**
* @deprecated Use {@link ExoPlayer#clearCameraMotionListener(CameraMotionListener)} instead.
*/
@Deprecated
void clearCameraMotionListener(CameraMotionListener listener);
/**
* @deprecated Use {@link Player#clearVideoSurface()} instead.
*/
@Deprecated
void clearVideoSurface();
/**
* @deprecated Use {@link Player#clearVideoSurface(Surface)} instead.
*/
@Deprecated
void clearVideoSurface(@Nullable Surface surface);
/**
* @deprecated Use {@link Player#setVideoSurface(Surface)} instead.
*/
@Deprecated
void setVideoSurface(@Nullable Surface surface);
/**
* @deprecated Use {@link Player#setVideoSurfaceHolder(SurfaceHolder)} instead.
*/
@Deprecated
void setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder);
/**
* @deprecated Use {@link Player#clearVideoSurfaceHolder(SurfaceHolder)} instead.
*/
@Deprecated
void clearVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder);
/**
* @deprecated Use {@link Player#setVideoSurfaceView(SurfaceView)} instead.
*/
@Deprecated
void setVideoSurfaceView(@Nullable SurfaceView surfaceView);
/**
* @deprecated Use {@link Player#clearVideoSurfaceView(SurfaceView)} instead.
*/
@Deprecated
void clearVideoSurfaceView(@Nullable SurfaceView surfaceView);
/**
* @deprecated Use {@link Player#setVideoTextureView(TextureView)} instead.
*/
@Deprecated
void setVideoTextureView(@Nullable TextureView textureView);
/**
* @deprecated Use {@link Player#clearVideoTextureView(TextureView)} instead.
*/
@Deprecated
void clearVideoTextureView(@Nullable TextureView textureView);
/**
* @deprecated Use {@link Player#getVideoSize()} instead.
*/
@Deprecated
VideoSize getVideoSize();
}
/**
* @deprecated Use {@link Player}, as the {@link TextComponent} methods are defined by that
* interface.
*/
@Deprecated
interface TextComponent {
/**
* @deprecated Use {@link Player#getCurrentCues()} instead.
*/
@Deprecated
CueGroup getCurrentCues();
}
/**
* @deprecated Use {@link Player}, as the {@link DeviceComponent} methods are defined by that
* interface.
*/
@Deprecated
interface DeviceComponent {
/**
* @deprecated Use {@link Player#getDeviceInfo()} instead.
*/
@Deprecated
DeviceInfo getDeviceInfo();
/**
* @deprecated Use {@link Player#getDeviceVolume()} instead.
*/
@Deprecated
int getDeviceVolume();
/**
* @deprecated Use {@link Player#isDeviceMuted()} instead.
*/
@Deprecated
boolean isDeviceMuted();
/**
* @deprecated Use {@link Player#setDeviceVolume(int)} instead.
*/
@Deprecated
void setDeviceVolume(int volume);
/**
* @deprecated Use {@link Player#increaseDeviceVolume()} instead.
*/
@Deprecated
void increaseDeviceVolume();
/**
* @deprecated Use {@link Player#decreaseDeviceVolume()} instead.
*/
@Deprecated
void decreaseDeviceVolume();
/**
* @deprecated Use {@link Player#setDeviceMuted(boolean)} instead.
*/
@Deprecated
void setDeviceMuted(boolean muted);
}
/**
* A listener for audio offload events.
*
* <p>This class is experimental, and might be renamed, moved or removed in a future release.
*/
interface AudioOffloadListener {
/**
* Called when the player has started or stopped offload scheduling using {@link
* #experimentalSetOffloadSchedulingEnabled(boolean)}.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*/
default void onExperimentalOffloadSchedulingEnabledChanged(boolean offloadSchedulingEnabled) {}
/**
* Called when the player has started or finished sleeping for offload.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*/
default void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload) {}
/**
* Called when the value of {@link AudioTrack#isOffloadedPlayback} changes.
*
* <p>This should not be generally required to be acted upon. But when offload is critical for
* efficiency, or audio features (gapless, playback speed), this will let the app know.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*/
default void onExperimentalOffloadedPlayback(boolean offloadedPlayback) {}
}
/**
* A builder for {@link ExoPlayer} instances.
*
* <p>See {@link #Builder(Context)} for the list of default values.
*/
@SuppressWarnings("deprecation")
final class Builder {
/* package */ final Context context;
/* package */ Clock clock;
/* package */ long foregroundModeTimeoutMs;
/* package */ Supplier<RenderersFactory> renderersFactorySupplier;
/* package */ Supplier<MediaSource.Factory> mediaSourceFactorySupplier;
/* package */ Supplier<TrackSelector> trackSelectorSupplier;
/* package */ Supplier<LoadControl> loadControlSupplier;
/* package */ Supplier<BandwidthMeter> bandwidthMeterSupplier;
/* package */ Function<Clock, AnalyticsCollector> analyticsCollectorFunction;
/* package */ Looper looper;
@Nullable /* package */ PriorityTaskManager priorityTaskManager;
/* package */ AudioAttributes audioAttributes;
/* package */ boolean handleAudioFocus;
@C.WakeMode /* package */ int wakeMode;
/* package */ boolean handleAudioBecomingNoisy;
/* package */ boolean skipSilenceEnabled;
@C.VideoScalingMode /* package */ int videoScalingMode;
@C.VideoChangeFrameRateStrategy /* package */ int videoChangeFrameRateStrategy;
/* package */ boolean useLazyPreparation;
/* package */ SeekParameters seekParameters;
/* package */ long seekBackIncrementMs;
/* package */ long seekForwardIncrementMs;
/* package */ LivePlaybackSpeedControl livePlaybackSpeedControl;
/* package */ long releaseTimeoutMs;
/* package */ long detachSurfaceTimeoutMs;
/* package */ boolean pauseAtEndOfMediaItems;
/* package */ boolean usePlatformDiagnostics;
/* package */ boolean buildCalled;
/**
* Creates a builder.
*
* <p>Use {@link #Builder(Context, RenderersFactory)}, {@link #Builder(Context,
* MediaSource.Factory)} or {@link #Builder(Context, RenderersFactory, MediaSource.Factory)}
* instead, if you intend to provide a custom {@link RenderersFactory}, {@link
* ExtractorsFactory} or {@link DefaultMediaSourceFactory}. This is to ensure that ProGuard or
* R8 can remove ExoPlayer's {@link DefaultRenderersFactory}, {@link DefaultExtractorsFactory}
* and {@link DefaultMediaSourceFactory} from the APK.
*
* <p>The builder uses the following default values:
*
* <ul>
* <li>{@link RenderersFactory}: {@link DefaultRenderersFactory}
* <li>{@link TrackSelector}: {@link DefaultTrackSelector}
* <li>{@link MediaSource.Factory}: {@link DefaultMediaSourceFactory}
* <li>{@link LoadControl}: {@link DefaultLoadControl}
* <li>{@link BandwidthMeter}: {@link DefaultBandwidthMeter#getSingletonInstance(Context)}
* <li>{@link LivePlaybackSpeedControl}: {@link DefaultLivePlaybackSpeedControl}
* <li>{@link Looper}: The {@link Looper} associated with the current thread, or the {@link
* Looper} of the application's main thread if the current thread doesn't have a {@link
* Looper}
* <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT}
* <li>{@link PriorityTaskManager}: {@code null} (not used)
* <li>{@link AudioAttributes}: {@link AudioAttributes#DEFAULT}, not handling audio focus
* <li>{@link C.WakeMode}: {@link C#WAKE_MODE_NONE}
* <li>{@code handleAudioBecomingNoisy}: {@code false}
* <li>{@code skipSilenceEnabled}: {@code false}
* <li>{@link C.VideoScalingMode}: {@link C#VIDEO_SCALING_MODE_DEFAULT}
* <li>{@link C.VideoChangeFrameRateStrategy}: {@link
* C#VIDEO_CHANGE_FRAME_RATE_STRATEGY_ONLY_IF_SEAMLESS}
* <li>{@code useLazyPreparation}: {@code true}
* <li>{@link SeekParameters}: {@link SeekParameters#DEFAULT}
* <li>{@code seekBackIncrementMs}: {@link C#DEFAULT_SEEK_BACK_INCREMENT_MS}
* <li>{@code seekForwardIncrementMs}: {@link C#DEFAULT_SEEK_FORWARD_INCREMENT_MS}
* <li>{@code releaseTimeoutMs}: {@link #DEFAULT_RELEASE_TIMEOUT_MS}
* <li>{@code detachSurfaceTimeoutMs}: {@link #DEFAULT_DETACH_SURFACE_TIMEOUT_MS}
* <li>{@code pauseAtEndOfMediaItems}: {@code false}
* <li>{@code usePlatformDiagnostics}: {@code true}
* <li>{@link Clock}: {@link Clock#DEFAULT}
* </ul>
*
* @param context A {@link Context}.
*/
public Builder(Context context) {
this(
context,
() -> new DefaultRenderersFactory(context),
() -> new DefaultMediaSourceFactory(context, new DefaultExtractorsFactory()));
}
/**
* Creates a builder with a custom {@link RenderersFactory}.
*
* <p>See {@link #Builder(Context)} for a list of default values.
*
* <p>Note that this constructor is only useful to try and ensure that ExoPlayer's {@link
* DefaultRenderersFactory} can be removed by ProGuard or R8.
*
* @param context A {@link Context}.
* @param renderersFactory A factory for creating {@link Renderer Renderers} to be used by the
* player.
*/
public Builder(Context context, RenderersFactory renderersFactory) {
this(
context,
() -> renderersFactory,
() -> new DefaultMediaSourceFactory(context, new DefaultExtractorsFactory()));
checkNotNull(renderersFactory);
}
/**
* Creates a builder with a custom {@link MediaSource.Factory}.
*
* <p>See {@link #Builder(Context)} for a list of default values.
*
* <p>Note that this constructor is only useful to try and ensure that ExoPlayer's {@link
* DefaultMediaSourceFactory} (and therefore {@link DefaultExtractorsFactory}) can be removed by
* ProGuard or R8.
*
* @param context A {@link Context}.
* @param mediaSourceFactory A factory for creating a {@link MediaSource} from a {@link
* MediaItem}.
*/
public Builder(Context context, MediaSource.Factory mediaSourceFactory) {
this(context, () -> new DefaultRenderersFactory(context), () -> mediaSourceFactory);
checkNotNull(mediaSourceFactory);
}
/**
* Creates a builder with a custom {@link RenderersFactory} and {@link MediaSource.Factory}.
*
* <p>See {@link #Builder(Context)} for a list of default values.
*
* <p>Note that this constructor is only useful to try and ensure that ExoPlayer's {@link
* DefaultRenderersFactory}, {@link DefaultMediaSourceFactory} (and therefore {@link
* DefaultExtractorsFactory}) can be removed by ProGuard or R8.
*
* @param context A {@link Context}.
* @param renderersFactory A factory for creating {@link Renderer Renderers} to be used by the
* player.
* @param mediaSourceFactory A factory for creating a {@link MediaSource} from a {@link
* MediaItem}.
*/
public Builder(
Context context,
RenderersFactory renderersFactory,
MediaSource.Factory mediaSourceFactory) {
this(context, () -> renderersFactory, () -> mediaSourceFactory);
checkNotNull(renderersFactory);
checkNotNull(mediaSourceFactory);
}
/**
* Creates a builder with the specified custom components.
*
* <p>Note that this constructor is only useful to try and ensure that ExoPlayer's default
* components can be removed by ProGuard or R8.
*
* @param context A {@link Context}.
* @param renderersFactory A factory for creating {@link Renderer Renderers} to be used by the
* player.
* @param mediaSourceFactory A {@link MediaSource.Factory}.
* @param trackSelector A {@link TrackSelector}.
* @param loadControl A {@link LoadControl}.
* @param bandwidthMeter A {@link BandwidthMeter}.
* @param analyticsCollector An {@link AnalyticsCollector}.
*/
public Builder(
Context context,
RenderersFactory renderersFactory,
MediaSource.Factory mediaSourceFactory,
TrackSelector trackSelector,
LoadControl loadControl,
BandwidthMeter bandwidthMeter,
AnalyticsCollector analyticsCollector) {
this(
context,
() -> renderersFactory,
() -> mediaSourceFactory,
() -> trackSelector,
() -> loadControl,
() -> bandwidthMeter,
(clock) -> analyticsCollector);
checkNotNull(renderersFactory);
checkNotNull(mediaSourceFactory);
checkNotNull(trackSelector);
checkNotNull(bandwidthMeter);
checkNotNull(analyticsCollector);
}
private Builder(
Context context,
Supplier<RenderersFactory> renderersFactorySupplier,
Supplier<MediaSource.Factory> mediaSourceFactorySupplier) {
this(
context,
renderersFactorySupplier,
mediaSourceFactorySupplier,
() -> new DefaultTrackSelector(context),
DefaultLoadControl::new,
() -> DefaultBandwidthMeter.getSingletonInstance(context),
DefaultAnalyticsCollector::new);
}
private Builder(
Context context,
Supplier<RenderersFactory> renderersFactorySupplier,
Supplier<MediaSource.Factory> mediaSourceFactorySupplier,
Supplier<TrackSelector> trackSelectorSupplier,
Supplier<LoadControl> loadControlSupplier,
Supplier<BandwidthMeter> bandwidthMeterSupplier,
Function<Clock, AnalyticsCollector> analyticsCollectorFunction) {
this.context = checkNotNull(context);
this.renderersFactorySupplier = renderersFactorySupplier;
this.mediaSourceFactorySupplier = mediaSourceFactorySupplier;
this.trackSelectorSupplier = trackSelectorSupplier;
this.loadControlSupplier = loadControlSupplier;
this.bandwidthMeterSupplier = bandwidthMeterSupplier;
this.analyticsCollectorFunction = analyticsCollectorFunction;
looper = Util.getCurrentOrMainLooper();
audioAttributes = AudioAttributes.DEFAULT;
wakeMode = C.WAKE_MODE_NONE;
videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT;
videoChangeFrameRateStrategy = C.VIDEO_CHANGE_FRAME_RATE_STRATEGY_ONLY_IF_SEAMLESS;
useLazyPreparation = true;
seekParameters = SeekParameters.DEFAULT;
seekBackIncrementMs = C.DEFAULT_SEEK_BACK_INCREMENT_MS;
seekForwardIncrementMs = C.DEFAULT_SEEK_FORWARD_INCREMENT_MS;
livePlaybackSpeedControl = new DefaultLivePlaybackSpeedControl.Builder().build();
clock = Clock.DEFAULT;
releaseTimeoutMs = DEFAULT_RELEASE_TIMEOUT_MS;
detachSurfaceTimeoutMs = DEFAULT_DETACH_SURFACE_TIMEOUT_MS;
usePlatformDiagnostics = true;
}
/**
* Sets a limit on the time a call to {@link #setForegroundMode} can spend. If a call to {@link
* #setForegroundMode} takes more than {@code timeoutMs} milliseconds to complete, the player
* will raise an error via {@link Player.Listener#onPlayerError}.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* @param timeoutMs The time limit in milliseconds.
*/
@CanIgnoreReturnValue
public Builder experimentalSetForegroundModeTimeoutMs(long timeoutMs) {
checkState(!buildCalled);
foregroundModeTimeoutMs = timeoutMs;
return this;
}
/**
* Sets the {@link RenderersFactory} that will be used by the player.
*
* @param renderersFactory A {@link RenderersFactory}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setRenderersFactory(RenderersFactory renderersFactory) {
checkState(!buildCalled);
checkNotNull(renderersFactory);
this.renderersFactorySupplier = () -> renderersFactory;
return this;
}
/**
* Sets the {@link MediaSource.Factory} that will be used by the player.
*
* @param mediaSourceFactory A {@link MediaSource.Factory}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory) {
checkState(!buildCalled);
checkNotNull(mediaSourceFactory);
this.mediaSourceFactorySupplier = () -> mediaSourceFactory;
return this;
}
/**
* Sets the {@link TrackSelector} that will be used by the player.
*
* @param trackSelector A {@link TrackSelector}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setTrackSelector(TrackSelector trackSelector) {
checkState(!buildCalled);
checkNotNull(trackSelector);
this.trackSelectorSupplier = () -> trackSelector;
return this;
}
/**
* Sets the {@link LoadControl} that will be used by the player.
*
* @param loadControl A {@link LoadControl}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setLoadControl(LoadControl loadControl) {
checkState(!buildCalled);
checkNotNull(loadControl);
this.loadControlSupplier = () -> loadControl;
return this;
}
/**
* Sets the {@link BandwidthMeter} that will be used by the player.
*
* @param bandwidthMeter A {@link BandwidthMeter}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setBandwidthMeter(BandwidthMeter bandwidthMeter) {
checkState(!buildCalled);
checkNotNull(bandwidthMeter);
this.bandwidthMeterSupplier = () -> bandwidthMeter;
return this;
}
/**
* Sets the {@link Looper} that must be used for all calls to the player and that is used to
* call listeners on.
*
* @param looper A {@link Looper}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setLooper(Looper looper) {
checkState(!buildCalled);
checkNotNull(looper);
this.looper = looper;
return this;
}
/**
* Sets the {@link AnalyticsCollector} that will collect and forward all player events.
*
* @param analyticsCollector An {@link AnalyticsCollector}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector) {
checkState(!buildCalled);
checkNotNull(analyticsCollector);
this.analyticsCollectorFunction = (clock) -> analyticsCollector;
return this;
}
/**
* Sets an {@link PriorityTaskManager} that will be used by the player.
*
* <p>The priority {@link C#PRIORITY_PLAYBACK} will be set while the player is loading.
*
* @param priorityTaskManager A {@link PriorityTaskManager}, or null to not use one.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setPriorityTaskManager(@Nullable PriorityTaskManager priorityTaskManager) {
checkState(!buildCalled);
this.priorityTaskManager = priorityTaskManager;
return this;
}
/**
* Sets {@link AudioAttributes} that will be used by the player and whether to handle audio
* focus.
*
* <p>If audio focus should be handled, the {@link AudioAttributes#usage} must be {@link
* C#USAGE_MEDIA} or {@link C#USAGE_GAME}. Other usages will throw an {@link
* IllegalArgumentException}.
*
* @param audioAttributes {@link AudioAttributes}.
* @param handleAudioFocus Whether the player should handle audio focus.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus) {
checkState(!buildCalled);
this.audioAttributes = checkNotNull(audioAttributes);
this.handleAudioFocus = handleAudioFocus;
return this;
}
/**
* Sets the {@link C.WakeMode} that will be used by the player.
*
* <p>Enabling this feature requires the {@link android.Manifest.permission#WAKE_LOCK}
* permission. It should be used together with a foreground {@link android.app.Service} for use
* cases where playback occurs and the screen is off (e.g. background audio playback). It is not
* useful when the screen will be kept on during playback (e.g. foreground video playback).
*
* <p>When enabled, the locks ({@link android.os.PowerManager.WakeLock} / {@link
* android.net.wifi.WifiManager.WifiLock}) will be held whenever the player is in the {@link
* #STATE_READY} or {@link #STATE_BUFFERING} states with {@code playWhenReady = true}. The locks
* held depend on the specified {@link C.WakeMode}.
*
* @param wakeMode A {@link C.WakeMode}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setWakeMode(@C.WakeMode int wakeMode) {
checkState(!buildCalled);
this.wakeMode = wakeMode;
return this;
}
/**
* Sets whether the player should pause automatically when audio is rerouted from a headset to
* device speakers. See the <a
* href="https://developer.android.com/guide/topics/media-apps/volume-and-earphones#becoming-noisy">
* audio becoming noisy</a> documentation for more information.
*
* @param handleAudioBecomingNoisy Whether the player should pause automatically when audio is
* rerouted from a headset to device speakers.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy) {
checkState(!buildCalled);
this.handleAudioBecomingNoisy = handleAudioBecomingNoisy;
return this;
}
/**
* Sets whether silences silences in the audio stream is enabled.
*
* @param skipSilenceEnabled Whether skipping silences is enabled.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setSkipSilenceEnabled(boolean skipSilenceEnabled) {
checkState(!buildCalled);
this.skipSilenceEnabled = skipSilenceEnabled;
return this;
}
/**
* Sets the {@link C.VideoScalingMode} that will be used by the player.
*
* <p>The scaling mode only applies if a {@link MediaCodec}-based video {@link Renderer} is
* enabled and if the output surface is owned by a {@link SurfaceView}.
*
* @param videoScalingMode A {@link C.VideoScalingMode}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setVideoScalingMode(@C.VideoScalingMode int videoScalingMode) {
checkState(!buildCalled);
this.videoScalingMode = videoScalingMode;
return this;
}
/**
* Sets a {@link C.VideoChangeFrameRateStrategy} that will be used by the player when provided
* with a video output {@link Surface}.
*
* <p>The strategy only applies if a {@link MediaCodec}-based video {@link Renderer} is enabled.
* Applications wishing to use {@link Surface#CHANGE_FRAME_RATE_ALWAYS} should set the mode to
* {@link C#VIDEO_CHANGE_FRAME_RATE_STRATEGY_OFF} to disable calls to {@link
* Surface#setFrameRate} from ExoPlayer, and should then call {@link Surface#setFrameRate}
* directly from application code.
*
* @param videoChangeFrameRateStrategy A {@link C.VideoChangeFrameRateStrategy}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setVideoChangeFrameRateStrategy(
@C.VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy) {
checkState(!buildCalled);
this.videoChangeFrameRateStrategy = videoChangeFrameRateStrategy;
return this;
}
/**
* Sets whether media sources should be initialized lazily.
*
* <p>If false, all initial preparation steps (e.g., manifest loads) happen immediately. If
* true, these initial preparations are triggered only when the player starts buffering the
* media.
*
* @param useLazyPreparation Whether to use lazy preparation.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setUseLazyPreparation(boolean useLazyPreparation) {
checkState(!buildCalled);
this.useLazyPreparation = useLazyPreparation;
return this;
}
/**
* Sets the parameters that control how seek operations are performed.
*
* @param seekParameters The {@link SeekParameters}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setSeekParameters(SeekParameters seekParameters) {
checkState(!buildCalled);
this.seekParameters = checkNotNull(seekParameters);
return this;
}
/**
* Sets the {@link #seekBack()} increment.
*
* @param seekBackIncrementMs The seek back increment, in milliseconds.
* @return This builder.
* @throws IllegalArgumentException If {@code seekBackIncrementMs} is non-positive.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setSeekBackIncrementMs(@IntRange(from = 1) long seekBackIncrementMs) {
checkArgument(seekBackIncrementMs > 0);
checkState(!buildCalled);
this.seekBackIncrementMs = seekBackIncrementMs;
return this;
}
/**
* Sets the {@link #seekForward()} increment.
*
* @param seekForwardIncrementMs The seek forward increment, in milliseconds.
* @return This builder.
* @throws IllegalArgumentException If {@code seekForwardIncrementMs} is non-positive.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setSeekForwardIncrementMs(@IntRange(from = 1) long seekForwardIncrementMs) {
checkArgument(seekForwardIncrementMs > 0);
checkState(!buildCalled);
this.seekForwardIncrementMs = seekForwardIncrementMs;
return this;
}
/**
* Sets a timeout for calls to {@link #release} and {@link #setForegroundMode}.
*
* <p>If a call to {@link #release} or {@link #setForegroundMode} takes more than {@code
* timeoutMs} to complete, the player will report an error via {@link
* Player.Listener#onPlayerError}.
*
* @param releaseTimeoutMs The release timeout, in milliseconds.
* @return This builder.