Class ExoPlayer.Builder
- java.lang.Object
-
- com.google.android.exoplayer2.ExoPlayer.Builder
-
- Enclosing interface:
- ExoPlayer
public static final class ExoPlayer.Builder extends Object
A builder forExoPlayerinstances.See
Builder(Context)for the list of default values.
-
-
Constructor Summary
Constructors Constructor Description Builder(Context context)Creates a builder.Builder(Context context, RenderersFactory renderersFactory)Creates a builder with a customRenderersFactory.Builder(Context context, RenderersFactory renderersFactory, MediaSource.Factory mediaSourceFactory)Creates a builder with a customRenderersFactoryandMediaSource.Factory.Builder(Context context, RenderersFactory renderersFactory, MediaSource.Factory mediaSourceFactory, TrackSelector trackSelector, LoadControl loadControl, BandwidthMeter bandwidthMeter, AnalyticsCollector analyticsCollector)Creates a builder with the specified custom components.Builder(Context context, MediaSource.Factory mediaSourceFactory)Creates a builder with a customMediaSource.Factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ExoPlayerbuild()Builds anExoPlayerinstance.ExoPlayer.BuilderexperimentalSetForegroundModeTimeoutMs(long timeoutMs)Sets a limit on the time a call toExoPlayer.setForegroundMode(boolean)can spend.ExoPlayer.BuildersetAnalyticsCollector(AnalyticsCollector analyticsCollector)Sets theAnalyticsCollectorthat will collect and forward all player events.ExoPlayer.BuildersetAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus)SetsAudioAttributesthat will be used by the player and whether to handle audio focus.ExoPlayer.BuildersetBandwidthMeter(BandwidthMeter bandwidthMeter)Sets theBandwidthMeterthat will be used by the player.ExoPlayer.BuildersetClock(Clock clock)Sets theClockthat will be used by the player.ExoPlayer.BuildersetDetachSurfaceTimeoutMs(long detachSurfaceTimeoutMs)Sets a timeout for detaching a surface from the player.ExoPlayer.BuildersetDeviceVolumeControlEnabled(boolean deviceVolumeControlEnabled)Sets whether the player is allowed to set, increase, decrease or mute device volume.ExoPlayer.BuildersetHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy)Sets whether the player should pause automatically when audio is rerouted from a headset to device speakers.ExoPlayer.BuildersetLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlaybackSpeedControl)Sets theLivePlaybackSpeedControlthat will control the playback speed when playing live streams, in order to maintain a steady target offset from the live stream edge.ExoPlayer.BuildersetLoadControl(LoadControl loadControl)Sets theLoadControlthat will be used by the player.ExoPlayer.BuildersetLooper(Looper looper)Sets theLooperthat must be used for all calls to the player and that is used to call listeners on.ExoPlayer.BuildersetMediaSourceFactory(MediaSource.Factory mediaSourceFactory)Sets theMediaSource.Factorythat will be used by the player.ExoPlayer.BuildersetPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems)Sets whether to pause playback at the end of each media item.ExoPlayer.BuildersetPlaybackLooper(Looper playbackLooper)Sets theLooperthat will be used for playback.ExoPlayer.BuildersetPriorityTaskManager(PriorityTaskManager priorityTaskManager)Sets anPriorityTaskManagerthat will be used by the player.ExoPlayer.BuildersetReleaseTimeoutMs(long releaseTimeoutMs)Sets a timeout for calls toPlayer.release()andExoPlayer.setForegroundMode(boolean).ExoPlayer.BuildersetRenderersFactory(RenderersFactory renderersFactory)Sets theRenderersFactorythat will be used by the player.ExoPlayer.BuildersetSeekBackIncrementMs(long seekBackIncrementMs)Sets thePlayer.seekBack()increment.ExoPlayer.BuildersetSeekForwardIncrementMs(long seekForwardIncrementMs)Sets thePlayer.seekForward()increment.ExoPlayer.BuildersetSeekParameters(SeekParameters seekParameters)Sets the parameters that control how seek operations are performed.ExoPlayer.BuildersetSkipSilenceEnabled(boolean skipSilenceEnabled)Sets whether silences silences in the audio stream is enabled.ExoPlayer.BuildersetTrackSelector(TrackSelector trackSelector)Sets theTrackSelectorthat will be used by the player.ExoPlayer.BuildersetUseLazyPreparation(boolean useLazyPreparation)Sets whether media sources should be initialized lazily.ExoPlayer.BuildersetUsePlatformDiagnostics(boolean usePlatformDiagnostics)Sets whether the player reports diagnostics data to the Android platform.ExoPlayer.BuildersetVideoChangeFrameRateStrategy(@com.google.android.exoplayer2.C.VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy)Sets aC.VideoChangeFrameRateStrategythat will be used by the player when provided with a video outputSurface.ExoPlayer.BuildersetVideoScalingMode(@com.google.android.exoplayer2.C.VideoScalingMode int videoScalingMode)Sets theC.VideoScalingModethat will be used by the player.ExoPlayer.BuildersetWakeMode(@com.google.android.exoplayer2.C.WakeMode int wakeMode)Sets theC.WakeModethat will be used by the player.
-
-
-
Constructor Detail
-
Builder
public Builder(Context context)
Creates a builder.Use
Builder(Context, RenderersFactory),Builder(Context, MediaSource.Factory)orBuilder(Context, RenderersFactory, MediaSource.Factory)instead, if you intend to provide a customRenderersFactory,ExtractorsFactoryorDefaultMediaSourceFactory. This is to ensure that ProGuard or R8 can remove ExoPlayer'sDefaultRenderersFactory,DefaultExtractorsFactoryandDefaultMediaSourceFactoryfrom the APK.The builder uses the following default values:
RenderersFactory:DefaultRenderersFactoryTrackSelector:DefaultTrackSelectorMediaSource.Factory:DefaultMediaSourceFactoryLoadControl:DefaultLoadControlBandwidthMeter:DefaultBandwidthMeter.getSingletonInstance(Context)LivePlaybackSpeedControl:DefaultLivePlaybackSpeedControlLooper: TheLooperassociated with the current thread, or theLooperof the application's main thread if the current thread doesn't have aLooperAnalyticsCollector:AnalyticsCollectorwithClock.DEFAULTPriorityTaskManager:null(not used)AudioAttributes:AudioAttributes.DEFAULT, not handling audio focusC.WakeMode:C.WAKE_MODE_NONEhandleAudioBecomingNoisy:falseskipSilenceEnabled:falseC.VideoScalingMode:C.VIDEO_SCALING_MODE_DEFAULTC.VideoChangeFrameRateStrategy:C.VIDEO_CHANGE_FRAME_RATE_STRATEGY_ONLY_IF_SEAMLESSuseLazyPreparation:trueSeekParameters:SeekParameters.DEFAULTseekBackIncrementMs:C.DEFAULT_SEEK_BACK_INCREMENT_MSseekForwardIncrementMs:C.DEFAULT_SEEK_FORWARD_INCREMENT_MSreleaseTimeoutMs:ExoPlayer.DEFAULT_RELEASE_TIMEOUT_MSdetachSurfaceTimeoutMs:ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MSpauseAtEndOfMediaItems:falseusePlatformDiagnostics:trueClock:Clock.DEFAULTplaybackLooper:null(create new thread)
- Parameters:
context- AContext.
-
Builder
public Builder(Context context, RenderersFactory renderersFactory)
Creates a builder with a customRenderersFactory.See
Builder(Context)for a list of default values.Note that this constructor is only useful to try and ensure that ExoPlayer's
DefaultRenderersFactorycan be removed by ProGuard or R8.
-
Builder
public Builder(Context context, MediaSource.Factory mediaSourceFactory)
Creates a builder with a customMediaSource.Factory.See
Builder(Context)for a list of default values.Note that this constructor is only useful to try and ensure that ExoPlayer's
DefaultMediaSourceFactory(and thereforeDefaultExtractorsFactory) can be removed by ProGuard or R8.- Parameters:
context- AContext.mediaSourceFactory- A factory for creating aMediaSourcefrom aMediaItem.
-
Builder
public Builder(Context context, RenderersFactory renderersFactory, MediaSource.Factory mediaSourceFactory)
Creates a builder with a customRenderersFactoryandMediaSource.Factory.See
Builder(Context)for a list of default values.Note that this constructor is only useful to try and ensure that ExoPlayer's
DefaultRenderersFactory,DefaultMediaSourceFactory(and thereforeDefaultExtractorsFactory) can be removed by ProGuard or R8.- Parameters:
context- AContext.renderersFactory- A factory for creatingRenderersto be used by the player.mediaSourceFactory- A factory for creating aMediaSourcefrom aMediaItem.
-
Builder
public Builder(Context context, RenderersFactory renderersFactory, MediaSource.Factory mediaSourceFactory, TrackSelector trackSelector, LoadControl loadControl, BandwidthMeter bandwidthMeter, AnalyticsCollector analyticsCollector)
Creates a builder with the specified custom components.Note that this constructor is only useful to try and ensure that ExoPlayer's default components can be removed by ProGuard or R8.
- Parameters:
context- AContext.renderersFactory- A factory for creatingRenderersto be used by the player.mediaSourceFactory- AMediaSource.Factory.trackSelector- ATrackSelector.loadControl- ALoadControl.bandwidthMeter- ABandwidthMeter.analyticsCollector- AnAnalyticsCollector.
-
-
Method Detail
-
experimentalSetForegroundModeTimeoutMs
@CanIgnoreReturnValue public ExoPlayer.Builder experimentalSetForegroundModeTimeoutMs(long timeoutMs)
Sets a limit on the time a call toExoPlayer.setForegroundMode(boolean)can spend. If a call toExoPlayer.setForegroundMode(boolean)takes more thantimeoutMsmilliseconds to complete, the player will raise an error viaPlayer.Listener.onPlayerError(com.google.android.exoplayer2.PlaybackException).This method is experimental, and will be renamed or removed in a future release.
- Parameters:
timeoutMs- The time limit in milliseconds.
-
setRenderersFactory
@CanIgnoreReturnValue public ExoPlayer.Builder setRenderersFactory(RenderersFactory renderersFactory)
Sets theRenderersFactorythat will be used by the player.- Parameters:
renderersFactory- ARenderersFactory.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setMediaSourceFactory
@CanIgnoreReturnValue public ExoPlayer.Builder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory)
Sets theMediaSource.Factorythat will be used by the player.- Parameters:
mediaSourceFactory- AMediaSource.Factory.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setTrackSelector
@CanIgnoreReturnValue public ExoPlayer.Builder setTrackSelector(TrackSelector trackSelector)
Sets theTrackSelectorthat will be used by the player.- Parameters:
trackSelector- ATrackSelector.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setLoadControl
@CanIgnoreReturnValue public ExoPlayer.Builder setLoadControl(LoadControl loadControl)
Sets theLoadControlthat will be used by the player.- Parameters:
loadControl- ALoadControl.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setBandwidthMeter
@CanIgnoreReturnValue public ExoPlayer.Builder setBandwidthMeter(BandwidthMeter bandwidthMeter)
Sets theBandwidthMeterthat will be used by the player.- Parameters:
bandwidthMeter- ABandwidthMeter.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setLooper
@CanIgnoreReturnValue public ExoPlayer.Builder setLooper(Looper looper)
Sets theLooperthat must be used for all calls to the player and that is used to call listeners on.- Parameters:
looper- ALooper.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setAnalyticsCollector
@CanIgnoreReturnValue public ExoPlayer.Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector)
Sets theAnalyticsCollectorthat will collect and forward all player events.- Parameters:
analyticsCollector- AnAnalyticsCollector.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setPriorityTaskManager
@CanIgnoreReturnValue public ExoPlayer.Builder setPriorityTaskManager(@Nullable PriorityTaskManager priorityTaskManager)
Sets anPriorityTaskManagerthat will be used by the player.The priority
C.PRIORITY_PLAYBACKwill be set while the player is loading.- Parameters:
priorityTaskManager- APriorityTaskManager, or null to not use one.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setAudioAttributes
@CanIgnoreReturnValue public ExoPlayer.Builder setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus)
SetsAudioAttributesthat will be used by the player and whether to handle audio focus.If audio focus should be handled, the
AudioAttributes.usagemust beC.USAGE_MEDIAorC.USAGE_GAME. Other usages will throw anIllegalArgumentException.- Parameters:
audioAttributes-AudioAttributes.handleAudioFocus- Whether the player should handle audio focus.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setWakeMode
@CanIgnoreReturnValue public ExoPlayer.Builder setWakeMode(@WakeMode @com.google.android.exoplayer2.C.WakeMode int wakeMode)
Sets theC.WakeModethat will be used by the player.Enabling this feature requires the
Manifest.permission.WAKE_LOCKpermission. It should be used together with a foregroundServicefor 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).When enabled, the locks (
PowerManager.WakeLock/WifiManager.WifiLock) will be held whenever the player is in thePlayer.STATE_READYorPlayer.STATE_BUFFERINGstates withplayWhenReady = true. The locks held depend on the specifiedC.WakeMode.- Parameters:
wakeMode- AC.WakeMode.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setHandleAudioBecomingNoisy
@CanIgnoreReturnValue public ExoPlayer.Builder setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy)
Sets whether the player should pause automatically when audio is rerouted from a headset to device speakers. See the audio becoming noisy documentation for more information.- Parameters:
handleAudioBecomingNoisy- Whether the player should pause automatically when audio is rerouted from a headset to device speakers.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setSkipSilenceEnabled
@CanIgnoreReturnValue public ExoPlayer.Builder setSkipSilenceEnabled(boolean skipSilenceEnabled)
Sets whether silences silences in the audio stream is enabled.- Parameters:
skipSilenceEnabled- Whether skipping silences is enabled.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setDeviceVolumeControlEnabled
@CanIgnoreReturnValue public ExoPlayer.Builder setDeviceVolumeControlEnabled(boolean deviceVolumeControlEnabled)
Sets whether the player is allowed to set, increase, decrease or mute device volume.- Parameters:
deviceVolumeControlEnabled- Whether controlling device volume is enabled.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setVideoScalingMode
@CanIgnoreReturnValue public ExoPlayer.Builder setVideoScalingMode(@VideoScalingMode @com.google.android.exoplayer2.C.VideoScalingMode int videoScalingMode)
Sets theC.VideoScalingModethat will be used by the player.The scaling mode only applies if a
MediaCodec-based videoRendereris enabled and if the output surface is owned by aSurfaceView.- Parameters:
videoScalingMode- AC.VideoScalingMode.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setVideoChangeFrameRateStrategy
@CanIgnoreReturnValue public ExoPlayer.Builder setVideoChangeFrameRateStrategy(@VideoChangeFrameRateStrategy @com.google.android.exoplayer2.C.VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy)
Sets aC.VideoChangeFrameRateStrategythat will be used by the player when provided with a video outputSurface.The strategy only applies if a
MediaCodec-based videoRendereris enabled. Applications wishing to useSurface.CHANGE_FRAME_RATE_ALWAYSshould set the mode toC.VIDEO_CHANGE_FRAME_RATE_STRATEGY_OFFto disable calls toSurface.setFrameRate(float, int, int)from ExoPlayer, and should then callSurface.setFrameRate(float, int, int)directly from application code.- Parameters:
videoChangeFrameRateStrategy- AC.VideoChangeFrameRateStrategy.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setUseLazyPreparation
@CanIgnoreReturnValue public ExoPlayer.Builder setUseLazyPreparation(boolean useLazyPreparation)
Sets whether media sources should be initialized lazily.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.
- Parameters:
useLazyPreparation- Whether to use lazy preparation.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setSeekParameters
@CanIgnoreReturnValue public ExoPlayer.Builder setSeekParameters(SeekParameters seekParameters)
Sets the parameters that control how seek operations are performed.- Parameters:
seekParameters- TheSeekParameters.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setSeekBackIncrementMs
@CanIgnoreReturnValue public ExoPlayer.Builder setSeekBackIncrementMs(@IntRange(from=1L) long seekBackIncrementMs)
Sets thePlayer.seekBack()increment.- Parameters:
seekBackIncrementMs- The seek back increment, in milliseconds.- Returns:
- This builder.
- Throws:
IllegalArgumentException- IfseekBackIncrementMsis non-positive.IllegalStateException- Ifbuild()has already been called.
-
setSeekForwardIncrementMs
@CanIgnoreReturnValue public ExoPlayer.Builder setSeekForwardIncrementMs(@IntRange(from=1L) long seekForwardIncrementMs)
Sets thePlayer.seekForward()increment.- Parameters:
seekForwardIncrementMs- The seek forward increment, in milliseconds.- Returns:
- This builder.
- Throws:
IllegalArgumentException- IfseekForwardIncrementMsis non-positive.IllegalStateException- Ifbuild()has already been called.
-
setReleaseTimeoutMs
@CanIgnoreReturnValue public ExoPlayer.Builder setReleaseTimeoutMs(long releaseTimeoutMs)
Sets a timeout for calls toPlayer.release()andExoPlayer.setForegroundMode(boolean).If a call to
Player.release()orExoPlayer.setForegroundMode(boolean)takes more thantimeoutMsto complete, the player will report an error viaPlayer.Listener.onPlayerError(com.google.android.exoplayer2.PlaybackException).- Parameters:
releaseTimeoutMs- The release timeout, in milliseconds.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setDetachSurfaceTimeoutMs
@CanIgnoreReturnValue public ExoPlayer.Builder setDetachSurfaceTimeoutMs(long detachSurfaceTimeoutMs)
Sets a timeout for detaching a surface from the player.If detaching a surface or replacing a surface takes more than
detachSurfaceTimeoutMsto complete, the player will report an error viaPlayer.Listener.onPlayerError(com.google.android.exoplayer2.PlaybackException).- Parameters:
detachSurfaceTimeoutMs- The timeout for detaching a surface, in milliseconds.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setPauseAtEndOfMediaItems
@CanIgnoreReturnValue public ExoPlayer.Builder setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems)
Sets whether to pause playback at the end of each media item.This means the player will pause at the end of each window in the current
timeline. Listeners will be informed by a call toPlayer.Listener.onPlayWhenReadyChanged(boolean, int)with the reasonPlayer.PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEMwhen this happens.- Parameters:
pauseAtEndOfMediaItems- Whether to pause playback at the end of each media item.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setLivePlaybackSpeedControl
@CanIgnoreReturnValue public ExoPlayer.Builder setLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlaybackSpeedControl)
Sets theLivePlaybackSpeedControlthat will control the playback speed when playing live streams, in order to maintain a steady target offset from the live stream edge.- Parameters:
livePlaybackSpeedControl- TheLivePlaybackSpeedControl.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setUsePlatformDiagnostics
@CanIgnoreReturnValue public ExoPlayer.Builder setUsePlatformDiagnostics(boolean usePlatformDiagnostics)
Sets whether the player reports diagnostics data to the Android platform.If enabled, the player will use the
MediaMetricsManagerto create aPlaybackSessionand forward playback events and performance data to this session. This helps to provide system performance and debugging information for media playback on the device. This data may also be collected by Google if sharing usage and diagnostics data is enabled by the user of the device.- Parameters:
usePlatformDiagnostics- Whether the player reports diagnostics data to the Android platform.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setClock
@CanIgnoreReturnValue public ExoPlayer.Builder setClock(Clock clock)
Sets theClockthat will be used by the player. Should only be set for testing purposes.- Parameters:
clock- AClock.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
setPlaybackLooper
@CanIgnoreReturnValue public ExoPlayer.Builder setPlaybackLooper(Looper playbackLooper)
Sets theLooperthat will be used for playback.The backing thread should run with priority
Process.THREAD_PRIORITY_AUDIOand should handle messages within 10ms.- Parameters:
playbackLooper- ALooper.- Returns:
- This builder.
- Throws:
IllegalStateException- Ifbuild()has already been called.
-
build
public ExoPlayer build()
Builds anExoPlayerinstance.- Throws:
IllegalStateException- If this method has already been called.
-
-