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 forExoPlayer
instances.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, MediaSourceFactory mediaSourceFactory)
Creates a builder with a customRenderersFactory
andMediaSourceFactory
.Builder(Context context, RenderersFactory renderersFactory, MediaSourceFactory mediaSourceFactory, TrackSelector trackSelector, LoadControl loadControl, BandwidthMeter bandwidthMeter, AnalyticsCollector analyticsCollector)
Creates a builder with the specified custom components.Builder(Context context, MediaSourceFactory mediaSourceFactory)
Creates a builder with a customMediaSourceFactory
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ExoPlayer
build()
Builds anExoPlayer
instance.ExoPlayer.Builder
experimentalSetForegroundModeTimeoutMs(long timeoutMs)
Sets a limit on the time a call toExoPlayer.setForegroundMode(boolean)
can spend.ExoPlayer.Builder
setAnalyticsCollector(AnalyticsCollector analyticsCollector)
Sets theAnalyticsCollector
that will collect and forward all player events.ExoPlayer.Builder
setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus)
SetsAudioAttributes
that will be used by the player and whether to handle audio focus.ExoPlayer.Builder
setBandwidthMeter(BandwidthMeter bandwidthMeter)
Sets theBandwidthMeter
that will be used by the player.ExoPlayer.Builder
setClock(Clock clock)
Sets theClock
that will be used by the player.ExoPlayer.Builder
setDetachSurfaceTimeoutMs(long detachSurfaceTimeoutMs)
Sets a timeout for detaching a surface from the player.ExoPlayer.Builder
setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy)
Sets whether the player should pause automatically when audio is rerouted from a headset to device speakers.ExoPlayer.Builder
setLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlaybackSpeedControl)
Sets theLivePlaybackSpeedControl
that will control the playback speed when playing live streams, in order to maintain a steady target offset from the live stream edge.ExoPlayer.Builder
setLoadControl(LoadControl loadControl)
Sets theLoadControl
that will be used by the player.ExoPlayer.Builder
setLooper(Looper looper)
Sets theLooper
that must be used for all calls to the player and that is used to call listeners on.ExoPlayer.Builder
setMediaSourceFactory(MediaSourceFactory mediaSourceFactory)
Sets theMediaSourceFactory
that will be used by the player.ExoPlayer.Builder
setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems)
Sets whether to pause playback at the end of each media item.ExoPlayer.Builder
setPriorityTaskManager(PriorityTaskManager priorityTaskManager)
Sets anPriorityTaskManager
that will be used by the player.ExoPlayer.Builder
setReleaseTimeoutMs(long releaseTimeoutMs)
Sets a timeout for calls toPlayer.release()
andExoPlayer.setForegroundMode(boolean)
.ExoPlayer.Builder
setRenderersFactory(RenderersFactory renderersFactory)
Sets theRenderersFactory
that will be used by the player.ExoPlayer.Builder
setSeekBackIncrementMs(long seekBackIncrementMs)
Sets thePlayer.seekBack()
increment.ExoPlayer.Builder
setSeekForwardIncrementMs(long seekForwardIncrementMs)
Sets thePlayer.seekForward()
increment.ExoPlayer.Builder
setSeekParameters(SeekParameters seekParameters)
Sets the parameters that control how seek operations are performed.ExoPlayer.Builder
setSkipSilenceEnabled(boolean skipSilenceEnabled)
Sets whether silences silences in the audio stream is enabled.ExoPlayer.Builder
setTrackSelector(TrackSelector trackSelector)
Sets theTrackSelector
that will be used by the player.ExoPlayer.Builder
setUseLazyPreparation(boolean useLazyPreparation)
Sets whether media sources should be initialized lazily.ExoPlayer.Builder
setVideoChangeFrameRateStrategy(int videoChangeFrameRateStrategy)
Sets aC.VideoChangeFrameRateStrategy
that will be used by the player when provided with a video outputSurface
.ExoPlayer.Builder
setVideoScalingMode(int videoScalingMode)
Sets theC.VideoScalingMode
that will be used by the player.ExoPlayer.Builder
setWakeMode(@com.google.android.exoplayer2.C.WakeMode int wakeMode)
Sets theC.WakeMode
that will be used by the player.
-
-
-
Constructor Detail
-
Builder
public Builder(Context context)
Creates a builder.Use
Builder(Context, RenderersFactory)
,Builder(Context, MediaSourceFactory)
orBuilder(Context, RenderersFactory, MediaSourceFactory)
instead, if you intend to provide a customRenderersFactory
,ExtractorsFactory
orDefaultMediaSourceFactory
. This is to ensure that ProGuard or R8 can remove ExoPlayer'sDefaultRenderersFactory
,DefaultExtractorsFactory
andDefaultMediaSourceFactory
from the APK.The builder uses the following default values:
RenderersFactory
:DefaultRenderersFactory
TrackSelector
:DefaultTrackSelector
MediaSourceFactory
:DefaultMediaSourceFactory
LoadControl
:DefaultLoadControl
BandwidthMeter
:DefaultBandwidthMeter.getSingletonInstance(Context)
LivePlaybackSpeedControl
:DefaultLivePlaybackSpeedControl
Looper
: TheLooper
associated with the current thread, or theLooper
of the application's main thread if the current thread doesn't have aLooper
AnalyticsCollector
:AnalyticsCollector
withClock.DEFAULT
PriorityTaskManager
:null
(not used)AudioAttributes
:AudioAttributes.DEFAULT
, not handling audio focusC.WakeMode
:C.WAKE_MODE_NONE
handleAudioBecomingNoisy
:false
skipSilenceEnabled
:false
C.VideoScalingMode
:C.VIDEO_SCALING_MODE_DEFAULT
C.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
releaseTimeoutMs
:ExoPlayer.DEFAULT_RELEASE_TIMEOUT_MS
detachSurfaceTimeoutMs
:ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS
pauseAtEndOfMediaItems
:false
Clock
:Clock.DEFAULT
- 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
DefaultRenderersFactory
can be removed by ProGuard or R8.
-
Builder
public Builder(Context context, MediaSourceFactory mediaSourceFactory)
Creates a builder with a customMediaSourceFactory
.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 aMediaSource
from aMediaItem
.
-
Builder
public Builder(Context context, RenderersFactory renderersFactory, MediaSourceFactory mediaSourceFactory)
Creates a builder with a customRenderersFactory
andMediaSourceFactory
.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 creatingRenderers
to be used by the player.mediaSourceFactory
- A factory for creating aMediaSource
from aMediaItem
.
-
Builder
public Builder(Context context, RenderersFactory renderersFactory, MediaSourceFactory 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 creatingRenderers
to be used by the player.mediaSourceFactory
- AMediaSourceFactory
.trackSelector
- ATrackSelector
.loadControl
- ALoadControl
.bandwidthMeter
- ABandwidthMeter
.analyticsCollector
- AnAnalyticsCollector
.
-
-
Method Detail
-
experimentalSetForegroundModeTimeoutMs
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 thantimeoutMs
milliseconds 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
public ExoPlayer.Builder setRenderersFactory(RenderersFactory renderersFactory)
Sets theRenderersFactory
that will be used by the player.- Parameters:
renderersFactory
- ARenderersFactory
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setMediaSourceFactory
public ExoPlayer.Builder setMediaSourceFactory(MediaSourceFactory mediaSourceFactory)
Sets theMediaSourceFactory
that will be used by the player.- Parameters:
mediaSourceFactory
- AMediaSourceFactory
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setTrackSelector
public ExoPlayer.Builder setTrackSelector(TrackSelector trackSelector)
Sets theTrackSelector
that will be used by the player.- Parameters:
trackSelector
- ATrackSelector
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setLoadControl
public ExoPlayer.Builder setLoadControl(LoadControl loadControl)
Sets theLoadControl
that will be used by the player.- Parameters:
loadControl
- ALoadControl
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setBandwidthMeter
public ExoPlayer.Builder setBandwidthMeter(BandwidthMeter bandwidthMeter)
Sets theBandwidthMeter
that will be used by the player.- Parameters:
bandwidthMeter
- ABandwidthMeter
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setLooper
public ExoPlayer.Builder setLooper(Looper looper)
Sets theLooper
that 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
public ExoPlayer.Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector)
Sets theAnalyticsCollector
that will collect and forward all player events.- Parameters:
analyticsCollector
- AnAnalyticsCollector
.- Returns:
- This builder.
- Throws:
IllegalStateException
- Ifbuild()
has already been called.
-
setPriorityTaskManager
public ExoPlayer.Builder setPriorityTaskManager(@Nullable PriorityTaskManager priorityTaskManager)
Sets anPriorityTaskManager
that will be used by the player.The priority
C.PRIORITY_PLAYBACK
will 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
public ExoPlayer.Builder setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus)
SetsAudioAttributes
that will be used by the player and whether to handle audio focus.If audio focus should be handled, the
AudioAttributes.usage
must beC.USAGE_MEDIA
orC.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
public ExoPlayer.Builder setWakeMode(@WakeMode @com.google.android.exoplayer2.C.WakeMode int wakeMode)
Sets theC.WakeMode
that will be used by the player.Enabling this feature requires the
Manifest.permission.WAKE_LOCK
permission. It should be used together with a foregroundService
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).When enabled, the locks (
PowerManager.WakeLock
/WifiManager.WifiLock
) will be held whenever the player is in thePlayer.STATE_READY
orPlayer.STATE_BUFFERING
states 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
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
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.
-
setVideoScalingMode
public ExoPlayer.Builder setVideoScalingMode(@VideoScalingMode int videoScalingMode)
Sets theC.VideoScalingMode
that will be used by the player.The scaling mode only applies if a
MediaCodec
-based videoRenderer
is 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
public ExoPlayer.Builder setVideoChangeFrameRateStrategy(@VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy)
Sets aC.VideoChangeFrameRateStrategy
that will be used by the player when provided with a video outputSurface
.The strategy only applies if a
MediaCodec
-based videoRenderer
is enabled. Applications wishing to useSurface.CHANGE_FRAME_RATE_ALWAYS
should set the mode toC.VIDEO_CHANGE_FRAME_RATE_STRATEGY_OFF
to 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
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
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
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
- IfseekBackIncrementMs
is non-positive.IllegalStateException
- Ifbuild()
has already been called.
-
setSeekForwardIncrementMs
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
- IfseekForwardIncrementMs
is non-positive.IllegalStateException
- Ifbuild()
has already been called.
-
setReleaseTimeoutMs
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 thantimeoutMs
to 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
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
detachSurfaceTimeoutMs
to 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
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_ITEM
when 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
public ExoPlayer.Builder setLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlaybackSpeedControl)
Sets theLivePlaybackSpeedControl
that 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.
-
setClock
public ExoPlayer.Builder setClock(Clock clock)
Sets theClock
that 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.
-
build
public ExoPlayer build()
Builds anExoPlayer
instance.- Throws:
IllegalStateException
- If this method has already been called.
-
-