ExoPlayer.Builder


class ExoPlayer.Builder


A builder for ExoPlayer instances.

See Builder for the list of default values.

Summary

Public constructors

Builder(context: Context!)

Creates a builder.

@UnstableApi
Builder(context: Context!, mediaSourceFactory: MediaSource.Factory!)

Creates a builder with a custom MediaSource.Factory.

@UnstableApi
Builder(context: Context!, renderersFactory: RenderersFactory!)

Creates a builder with a custom RenderersFactory.

@UnstableApi
Builder(
    context: Context!,
    renderersFactory: RenderersFactory!,
    mediaSourceFactory: MediaSource.Factory!
)

Creates a builder with a custom RenderersFactory and MediaSource.Factory.

@UnstableApi
Builder(
    context: Context!,
    renderersFactory: RenderersFactory!,
    mediaSourceFactory: MediaSource.Factory!,
    trackSelector: TrackSelector!,
    loadControl: LoadControl!,
    bandwidthMeter: BandwidthMeter!,
    analyticsCollector: AnalyticsCollector!
)

Creates a builder with the specified custom components.

Public functions

ExoPlayer!

Builds an ExoPlayer instance.

ExoPlayer.Builder!

Sets a limit on the time a call to setForegroundMode can spend.

ExoPlayer.Builder!

Sets the AnalyticsCollector that will collect and forward all player events.

ExoPlayer.Builder!
@CanIgnoreReturnValue
setAudioAttributes(
    audioAttributes: AudioAttributes!,
    handleAudioFocus: Boolean
)

Sets AudioAttributes that will be used by the player and whether to handle audio focus.

ExoPlayer.Builder!

Sets the BandwidthMeter that will be used by the player.

ExoPlayer.Builder!

Sets the Clock that will be used by the player.

ExoPlayer.Builder!

Sets a timeout for detaching a surface from the player.

ExoPlayer.Builder!

Sets whether the player is allowed to set, increase, decrease or mute device volume.

ExoPlayer.Builder!

Sets whether the player should pause automatically when audio is rerouted from a headset to device speakers.

ExoPlayer.Builder!

Sets the LivePlaybackSpeedControl 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!

Sets the LoadControl that will be used by the player.

ExoPlayer.Builder!

Sets the Looper that must be used for all calls to the player and that is used to call listeners on.

ExoPlayer.Builder!

Sets the MediaSource.Factory that will be used by the player.

ExoPlayer.Builder!

Sets whether to pause playback at the end of each media item.

ExoPlayer.Builder!

Sets the Looper that will be used for playback.

ExoPlayer.Builder!

Sets an PriorityTaskManager that will be used by the player.

ExoPlayer.Builder!

Sets a timeout for calls to release and setForegroundMode.

ExoPlayer.Builder!

Sets the RenderersFactory that will be used by the player.

ExoPlayer.Builder!

Sets the seekBack increment.

ExoPlayer.Builder!

Sets the seekForward increment.

ExoPlayer.Builder!

Sets the parameters that control how seek operations are performed.

ExoPlayer.Builder!

Sets whether silences silences in the audio stream is enabled.

ExoPlayer.Builder!

Sets whether the player should suppress playback that is attempted on an unsuitable output.

ExoPlayer.Builder!

Sets the TrackSelector that will be used by the player.

ExoPlayer.Builder!

Sets whether media sources should be initialized lazily.

ExoPlayer.Builder!

Sets whether the player reports diagnostics data to the Android platform.

ExoPlayer.Builder!

Sets a C.VideoChangeFrameRateStrategy that will be used by the player when provided with a video output Surface.

ExoPlayer.Builder!

Sets the C.VideoScalingMode that will be used by the player.

ExoPlayer.Builder!

Sets the C.WakeMode that will be used by the player.

Public constructors

Builder

Builder(context: Context!)

Creates a builder.

Use Builder, Builder or Builder instead, if you intend to provide a custom RenderersFactory, or DefaultMediaSourceFactory. This is to ensure that ProGuard or R8 can remove ExoPlayer's DefaultRenderersFactory, DefaultExtractorsFactory and DefaultMediaSourceFactory from the APK.

The builder uses the following default values:

Parameters
context: Context!

A Context.

Builder

@UnstableApi
Builder(context: Context!, mediaSourceFactory: MediaSource.Factory!)

Creates a builder with a custom MediaSource.Factory.

See Builder for a list of default values.

Note that this constructor is only useful to try and ensure that ExoPlayer's (and therefore DefaultExtractorsFactory) can be removed by ProGuard or R8.

Parameters
context: Context!

A Context.

mediaSourceFactory: MediaSource.Factory!

A factory for creating a MediaSource from a .

Builder

@UnstableApi
Builder(context: Context!, renderersFactory: RenderersFactory!)

Creates a builder with a custom RenderersFactory.

See Builder for a list of default values.

Note that this constructor is only useful to try and ensure that ExoPlayer's can be removed by ProGuard or R8.

Parameters
context: Context!

A Context.

renderersFactory: RenderersFactory!

A factory for creating Renderers to be used by the player.

Builder

@UnstableApi
Builder(
    context: Context!,
    renderersFactory: RenderersFactory!,
    mediaSourceFactory: MediaSource.Factory!
)

Creates a builder with a custom RenderersFactory and MediaSource.Factory.

See Builder for a list of default values.

Note that this constructor is only useful to try and ensure that ExoPlayer's , DefaultMediaSourceFactory (and therefore ) can be removed by ProGuard or R8.

Parameters
context: Context!

A Context.

renderersFactory: RenderersFactory!

A factory for creating Renderers to be used by the player.

mediaSourceFactory: MediaSource.Factory!

A factory for creating a MediaSource from a .

Builder

@UnstableApi
Builder(
    context: Context!,
    renderersFactory: RenderersFactory!,
    mediaSourceFactory: MediaSource.Factory!,
    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: Context!

A Context.

renderersFactory: RenderersFactory!

A factory for creating Renderers to be used by the player.

mediaSourceFactory: MediaSource.Factory!

A MediaSource.Factory.

trackSelector: TrackSelector!

A TrackSelector.

loadControl: LoadControl!

A LoadControl.

bandwidthMeter: BandwidthMeter!

A BandwidthMeter.

analyticsCollector: AnalyticsCollector!

An AnalyticsCollector.

Public functions

build

fun build(): ExoPlayer!

Builds an ExoPlayer instance.

Throws
java.lang.IllegalStateException

If this method has already been called.

experimentalSetForegroundModeTimeoutMs

@CanIgnoreReturnValue
@UnstableApi
fun experimentalSetForegroundModeTimeoutMs(timeoutMs: Long): ExoPlayer.Builder!

Sets a limit on the time a call to setForegroundMode can spend. If a call to setForegroundMode takes more than timeoutMs milliseconds to complete, the player will raise an error via onPlayerError.

This method is experimental, and will be renamed or removed in a future release.

Parameters
timeoutMs: Long

The time limit in milliseconds.

setAnalyticsCollector

@CanIgnoreReturnValue
@UnstableApi
fun setAnalyticsCollector(analyticsCollector: AnalyticsCollector!): ExoPlayer.Builder!

Sets the AnalyticsCollector that will collect and forward all player events.

Parameters
analyticsCollector: AnalyticsCollector!

An AnalyticsCollector.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setAudioAttributes

@CanIgnoreReturnValue
fun setAudioAttributes(
    audioAttributes: AudioAttributes!,
    handleAudioFocus: Boolean
): ExoPlayer.Builder!

Sets AudioAttributes that will be used by the player and whether to handle audio focus.

If audio focus should be handled, the usage must be USAGE_MEDIA or USAGE_GAME. Other usages will throw an .

Parameters
audioAttributes: AudioAttributes!

AudioAttributes.

handleAudioFocus: Boolean

Whether the player should handle audio focus.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setBandwidthMeter

@CanIgnoreReturnValue
@UnstableApi
fun setBandwidthMeter(bandwidthMeter: BandwidthMeter!): ExoPlayer.Builder!

Sets the BandwidthMeter that will be used by the player.

Parameters
bandwidthMeter: BandwidthMeter!

A BandwidthMeter.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setClock

@CanIgnoreReturnValue
@UnstableApi
@VisibleForTesting
fun setClock(clock: Clock!): ExoPlayer.Builder!

Sets the Clock that will be used by the player. Should only be set for testing purposes.

Parameters
clock: Clock!

A Clock.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setDetachSurfaceTimeoutMs

@CanIgnoreReturnValue
@UnstableApi
fun setDetachSurfaceTimeoutMs(detachSurfaceTimeoutMs: Long): ExoPlayer.Builder!

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 via onPlayerError.

Parameters
detachSurfaceTimeoutMs: Long

The timeout for detaching a surface, in milliseconds.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setDeviceVolumeControlEnabled

@CanIgnoreReturnValue
@UnstableApi
fun setDeviceVolumeControlEnabled(deviceVolumeControlEnabled: Boolean): ExoPlayer.Builder!

Sets whether the player is allowed to set, increase, decrease or mute device volume.

Parameters
deviceVolumeControlEnabled: Boolean

Whether controlling device volume is enabled.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setHandleAudioBecomingNoisy

@CanIgnoreReturnValue
fun setHandleAudioBecomingNoisy(handleAudioBecomingNoisy: Boolean): ExoPlayer.Builder!

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: Boolean

Whether the player should pause automatically when audio is rerouted from a headset to device speakers.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setLivePlaybackSpeedControl

@CanIgnoreReturnValue
@UnstableApi
fun setLivePlaybackSpeedControl(
    livePlaybackSpeedControl: LivePlaybackSpeedControl!
): ExoPlayer.Builder!

Sets the LivePlaybackSpeedControl 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: LivePlaybackSpeedControl!

The LivePlaybackSpeedControl.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setLoadControl

@CanIgnoreReturnValue
@UnstableApi
fun setLoadControl(loadControl: LoadControl!): ExoPlayer.Builder!

Sets the LoadControl that will be used by the player.

Parameters
loadControl: LoadControl!

A LoadControl.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setLooper

@CanIgnoreReturnValue
@UnstableApi
fun setLooper(looper: Looper!): ExoPlayer.Builder!

Sets the Looper that must be used for all calls to the player and that is used to call listeners on.

Parameters
looper: Looper!

A Looper.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setMediaSourceFactory

@CanIgnoreReturnValue
fun setMediaSourceFactory(mediaSourceFactory: MediaSource.Factory!): ExoPlayer.Builder!

Sets the MediaSource.Factory that will be used by the player.

Parameters
mediaSourceFactory: MediaSource.Factory!

A MediaSource.Factory.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setPauseAtEndOfMediaItems

@CanIgnoreReturnValue
@UnstableApi
fun setPauseAtEndOfMediaItems(pauseAtEndOfMediaItems: Boolean): ExoPlayer.Builder!

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 to onPlayWhenReadyChanged with the reason PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM when this happens.

Parameters
pauseAtEndOfMediaItems: Boolean

Whether to pause playback at the end of each media item.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setPlaybackLooper

@CanIgnoreReturnValue
@UnstableApi
fun setPlaybackLooper(playbackLooper: Looper!): ExoPlayer.Builder!

Sets the Looper that will be used for playback.

The backing thread should run with priority THREAD_PRIORITY_AUDIO and should handle messages within 10ms.

Parameters
playbackLooper: Looper!

A Looper.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setPriorityTaskManager

@CanIgnoreReturnValue
@UnstableApi
fun setPriorityTaskManager(priorityTaskManager: PriorityTaskManager?): ExoPlayer.Builder!

Sets an PriorityTaskManager that will be used by the player.

The priority PRIORITY_PLAYBACK will be set while the player is loading.

Parameters
priorityTaskManager: PriorityTaskManager?

A PriorityTaskManager, or null to not use one.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setReleaseTimeoutMs

@CanIgnoreReturnValue
@UnstableApi
fun setReleaseTimeoutMs(releaseTimeoutMs: Long): ExoPlayer.Builder!

Sets a timeout for calls to release and setForegroundMode.

If a call to release or setForegroundMode takes more than timeoutMs to complete, the player will report an error via onPlayerError.

Parameters
releaseTimeoutMs: Long

The release timeout, in milliseconds.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setRenderersFactory

@CanIgnoreReturnValue
@UnstableApi
fun setRenderersFactory(renderersFactory: RenderersFactory!): ExoPlayer.Builder!

Sets the RenderersFactory that will be used by the player.

Parameters
renderersFactory: RenderersFactory!

A RenderersFactory.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setSeekBackIncrementMs

@CanIgnoreReturnValue
@UnstableApi
fun setSeekBackIncrementMs(seekBackIncrementMs: @IntRange(from = 1) Long): ExoPlayer.Builder!

Sets the seekBack increment.

Parameters
seekBackIncrementMs: @IntRange(from = 1) Long

The seek back increment, in milliseconds.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalArgumentException

If seekBackIncrementMs is non-positive.

java.lang.IllegalStateException

If build has already been called.

setSeekForwardIncrementMs

@CanIgnoreReturnValue
@UnstableApi
fun setSeekForwardIncrementMs(seekForwardIncrementMs: @IntRange(from = 1) Long): ExoPlayer.Builder!

Sets the seekForward increment.

Parameters
seekForwardIncrementMs: @IntRange(from = 1) Long

The seek forward increment, in milliseconds.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalArgumentException

If seekForwardIncrementMs is non-positive.

java.lang.IllegalStateException

If build has already been called.

setSeekParameters

@CanIgnoreReturnValue
@UnstableApi
fun setSeekParameters(seekParameters: SeekParameters!): ExoPlayer.Builder!

Sets the parameters that control how seek operations are performed.

Parameters
seekParameters: SeekParameters!

The SeekParameters.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setSkipSilenceEnabled

@CanIgnoreReturnValue
@UnstableApi
fun setSkipSilenceEnabled(skipSilenceEnabled: Boolean): ExoPlayer.Builder!

Sets whether silences silences in the audio stream is enabled.

Parameters
skipSilenceEnabled: Boolean

Whether skipping silences is enabled.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setSuppressPlaybackOnUnsuitableOutput

@CanIgnoreReturnValue
@UnstableApi
fun setSuppressPlaybackOnUnsuitableOutput(
    suppressPlaybackOnUnsuitableOutput: Boolean
): ExoPlayer.Builder!

Sets whether the player should suppress playback that is attempted on an unsuitable output. An example of an unsuitable audio output is the built-in speaker on a Wear OS device (unless it is explicitly selected by the user).

If called with suppressPlaybackOnUnsuitableOutput = true, then a playback attempt on an unsuitable audio output will result in calls to onPlaybackSuppressionReasonChanged with the value PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT.

Callers of this may also want to enable setHandleAudioBecomingNoisy to prevent playback from continuing on the built-in speaker when a headset is disconnected.

Parameters
suppressPlaybackOnUnsuitableOutput: Boolean

Whether the player should suppress the playback when it is attempted on an unsuitable output.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setTrackSelector

@CanIgnoreReturnValue
@UnstableApi
fun setTrackSelector(trackSelector: TrackSelector!): ExoPlayer.Builder!

Sets the TrackSelector that will be used by the player.

Parameters
trackSelector: TrackSelector!

A TrackSelector.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setUseLazyPreparation

@CanIgnoreReturnValue
@UnstableApi
fun setUseLazyPreparation(useLazyPreparation: Boolean): ExoPlayer.Builder!

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: Boolean

Whether to use lazy preparation.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setUsePlatformDiagnostics

@CanIgnoreReturnValue
@UnstableApi
fun setUsePlatformDiagnostics(usePlatformDiagnostics: Boolean): ExoPlayer.Builder!

Sets whether the player reports diagnostics data to the Android platform.

If enabled, the player will use the android.media.metrics.MediaMetricsManager to create a android.media.metrics.PlaybackSession and 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: Boolean

Whether the player reports diagnostics data to the Android platform.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setVideoChangeFrameRateStrategy

@CanIgnoreReturnValue
@UnstableApi
fun setVideoChangeFrameRateStrategy(
    @C.VideoChangeFrameRateStrategy videoChangeFrameRateStrategy: Int
): ExoPlayer.Builder!

Sets a C.VideoChangeFrameRateStrategy that will be used by the player when provided with a video output Surface.

The strategy only applies if a MediaCodec-based video Renderer is enabled. Applications wishing to use CHANGE_FRAME_RATE_ALWAYS should set the mode to VIDEO_CHANGE_FRAME_RATE_STRATEGY_OFF to disable calls to setFrameRate from ExoPlayer, and should then call setFrameRate directly from application code.

Parameters
@C.VideoChangeFrameRateStrategy videoChangeFrameRateStrategy: Int

A C.VideoChangeFrameRateStrategy.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setVideoScalingMode

@CanIgnoreReturnValue
@UnstableApi
fun setVideoScalingMode(@C.VideoScalingMode videoScalingMode: Int): ExoPlayer.Builder!

Sets the C.VideoScalingMode that will be used by the player.

The scaling mode only applies if a MediaCodec-based video Renderer is enabled and if the output surface is owned by a SurfaceView.

Parameters
@C.VideoScalingMode videoScalingMode: Int

A C.VideoScalingMode.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.

setWakeMode

@CanIgnoreReturnValue
fun setWakeMode(@C.WakeMode wakeMode: Int): ExoPlayer.Builder!

Sets the C.WakeMode that will be used by the player.

Enabling this feature requires the WAKE_LOCK permission. It should be used together with a foreground 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).

When enabled, the locks (android.os.PowerManager.WakeLock / ) will be held whenever the player is in the STATE_READY or STATE_BUFFERING states with playWhenReady = true. The locks held depend on the specified C.WakeMode.

Parameters
@C.WakeMode wakeMode: Int

A C.WakeMode.

Returns
ExoPlayer.Builder!

This builder.

Throws
java.lang.IllegalStateException

If build has already been called.