@UnstableApi
class FakePlayer : SimpleBasePlayer


A fake Player that uses SimpleBasePlayer's minimal number of default methods implementations to build upon to simulate realistic playback scenarios for testing.

Summary

Public constructors

FakePlayer(
    playbackState: @Player.State Int,
    playWhenReady: Boolean,
    playlist: List<SimpleBasePlayer.MediaItemData>,
    playbackSpeed: Float,
    bufferingDelayMs: Long
)

Public functions

Unit
addCommands(vararg commands: Int)

Add commands to those already available to the player.

Unit
removeCommands(vararg commands: Int)

Remove commands from those already available to the player.

Unit
renderFirstFrame(newlyRenderedFirstFrame: Boolean)

Sets whether a frame has been rendered for the first time since setting the surface, a rendering reset, or since the stream being rendered was changed.

Unit
setBufferedPositionMs(bufferedPositionMs: Long)

Sets the buffered content playback position in milliseconds.

Unit
setDuration(uid: String, durationMs: Long)

Sets the duration of the media item in the playlist, in milliseconds.

Unit

Sets the {@linkplain Player.State state} of the player.

Unit
setPosition(positionMs: Long)

Sets the current content playback position in milliseconds.

Unit

Sets the PositionSupplier for the current content playback position using extrapolation.

Unit
setSeekBackIncrementMs(seekBackIncrementMs: Long)

Sets the Player.seekBack increment in milliseconds.

Unit
setSeekForwardIncrementMs(seekForwardIncrementMs: Long)

Sets the Player.seekForward increment in milliseconds.

Unit
setVideoSize(videoSize: VideoSize)

Sets the current video size.

Protected functions

open SimpleBasePlayer.State

Returns the current State of the player.

open ListenableFuture<*>
handleClearVideoOutput(videoOutput: Any?)

Handles calls to clear the video output.

open ListenableFuture<*>

Handles calls to prepare.

open ListenableFuture<*>

Handles calls to release.

open ListenableFuture<*>
handleSeek(
    mediaItemIndex: Int,
    positionMs: Long,
    seekCommand: @Player.Command Int
)

Handles calls to seekTo and other seek operations (for example, seekToNext).

open ListenableFuture<*>
handleSetMediaItems(
    mediaItems: MutableList<MediaItem>,
    startIndex: Int,
    startPositionMs: Long
)

Handles calls to setMediaItem and setMediaItems.

open ListenableFuture<*>

Handles calls to setPlayWhenReady, play and pause.

open ListenableFuture<*>

Handles calls to setPlaybackParameters or setPlaybackSpeed.

open ListenableFuture<*>
handleSetRepeatMode(repeatMode: Int)

Handles calls to setRepeatMode.

open ListenableFuture<*>
handleSetShuffleModeEnabled(shuffleModeEnabled: Boolean)

Handles calls to setShuffleModeEnabled.

open ListenableFuture<*>

Handles calls to setTrackSelectionParameters.

open ListenableFuture<*>
handleSetVideoOutput(videoOutput: Any)

Handles calls to set the video output.

open ListenableFuture<*>
handleSetVolume(
    volume: Float,
    volumeOperationType: @C.VolumeOperationType Int
)

Handles calls to setVolume.

open ListenableFuture<*>

Handles calls to stop.

Public properties

Boolean

Whether the player is released

Any?

Current video output

Protected properties

open SimpleBasePlayer.State

Inherited functions

From androidx.media3.common.BasePlayer
Unit
seekTo(
    mediaItemIndex: Int,
    positionMs: Long,
    @Player.Command seekCommand: @Player.Command Int,
    isRepeatingCurrentItem: Boolean
)

Seeks to a position in the specified MediaItem.

From androidx.media3.common.Player
Unit

Registers a listener to receive all events from the player.

Unit
addMediaItem(mediaItem: MediaItem)

Adds a media item to the end of the playlist.

Unit
addMediaItem(index: Int, mediaItem: MediaItem)

Adds a media item at the given index of the playlist.

Unit

Adds a list of media items to the end of the playlist.

Unit
addMediaItems(index: Int, mediaItems: MutableList<MediaItem>)

Adds a list of media items at the given index of the playlist.

Boolean

Returns whether the player can be used to advertise a media session.

Unit

Clears the playlist.

Unit

Clears any Surface, SurfaceHolder, SurfaceView or TextureView currently set on the player.

Unit

Clears the Surface onto which video is being rendered if it matches the one passed.

Unit

Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed.

Unit

Clears the SurfaceView onto which video is being rendered if it matches the one passed.

Unit

Clears the TextureView onto which video is being rendered if it matches the one passed.

Unit

This function is deprecated.

Use decreaseDeviceVolume instead.

Unit

Decreases the volume of the device.

MediaItem

Returns the MediaItem at the given index.

Boolean

Returns whether a next MediaItem exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

Boolean

Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

Unit

This function is deprecated.

Use increaseDeviceVolume instead.

Unit

Increases the volume of the device.

Boolean

Returns whether the provided Command is available.

Unit
moveMediaItem(currentIndex: Int, newIndex: Int)

Moves the media item at the current index to the new index.

Unit
moveMediaItems(fromIndex: Int, toIndex: Int, newIndex: Int)

Moves the media item range to the new index.

Unit

Sets the audio volume to 0.

Unit

Pauses playback.

Unit

Resumes playback as soon as getPlaybackState == STATE_READY.

Unit

Prepares the player.

Unit

Releases the player.

Unit

Unregister a listener registered through addListener.

Unit

Removes the media item at the given index of the playlist.

Unit
removeMediaItems(fromIndex: Int, toIndex: Int)

Removes a range of media items from the playlist.

Unit
replaceMediaItem(index: Int, mediaItem: MediaItem)

Replaces the media item at the given index of the playlist.

Unit
replaceMediaItems(
    fromIndex: Int,
    toIndex: Int,
    mediaItems: MutableList<MediaItem>
)

Replaces the media items at the given range of the playlist.

Unit

Seeks back in the current MediaItem by getSeekBackIncrement milliseconds.

Unit

Seeks forward in the current MediaItem by getSeekForwardIncrement milliseconds.

Unit
seekTo(positionMs: Long)

Seeks to a position specified in milliseconds in the current MediaItem.

Unit
seekTo(mediaItemIndex: Int, positionMs: Long)

Seeks to a position specified in milliseconds in the specified MediaItem.

Unit

Seeks to the default position associated with the current MediaItem.

Unit
seekToDefaultPosition(mediaItemIndex: Int)

Seeks to the default position associated with the specified MediaItem.

Unit

Seeks to a later position in the current or next MediaItem (if available).

Unit

Seeks to the default position of the next MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

Unit

Seeks to an earlier position in the current or previous MediaItem (if available).

Unit

Seeks to the default position of the previous MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

Unit
setAudioAttributes(
    audioAttributes: AudioAttributes,
    handleAudioFocus: Boolean
)

Sets the attributes for audio playback, used by the underlying audio track.

Unit
setMediaItem(mediaItem: MediaItem)

Clears the playlist, adds the specified MediaItem and resets the position to the default position.

Unit
setMediaItem(mediaItem: MediaItem, resetPosition: Boolean)

Clears the playlist and adds the specified MediaItem.

Unit
setMediaItem(mediaItem: MediaItem, startPositionMs: Long)

Clears the playlist and adds the specified MediaItem.

Unit

Clears the playlist, adds the specified media items and resets the position to the default position.

Unit
setMediaItems(mediaItems: MutableList<MediaItem>, resetPosition: Boolean)

Clears the playlist and adds the specified media items.

Unit
setMediaItems(
    mediaItems: MutableList<MediaItem>,
    startIndex: Int,
    startPositionMs: Long
)

Clears the playlist and adds the specified media items.

Unit

Changes the rate at which playback occurs.

Unit

Sets the Surface onto which video will be rendered.

Unit

Sets the SurfaceHolder that holds the Surface onto which video will be rendered.

Unit

Sets the SurfaceView onto which video will be rendered.

Unit

Sets the TextureView onto which video will be rendered.

Unit

Stops playback without resetting the playlist.

Unit

If the audio volume is 0, sets the audio volume to a non-zero value decided by the Player to be the most appropriate.

From androidx.media3.common.SimpleBasePlayer
open SimpleBasePlayer.MediaItemData

Returns the placeholder MediaItemData used for a new MediaItem added to the playlist.

open SimpleBasePlayer.State

Returns the placeholder state used while a player method is handled asynchronously.

open ListenableFuture<Any?>

Handles calls to addMediaItem and addMediaItems.

open ListenableFuture<Any?>

Handles calls to decreaseDeviceVolume and decreaseDeviceVolume.

open ListenableFuture<Any?>

Handles calls to increaseDeviceVolume and increaseDeviceVolume.

open ListenableFuture<Any?>
@ForOverride
handleMoveMediaItems(fromIndex: Int, toIndex: Int, newIndex: Int)

Handles calls to moveMediaItem and moveMediaItems.

open ListenableFuture<Any?>
@ForOverride
handleRemoveMediaItems(fromIndex: Int, toIndex: Int)

Handles calls to removeMediaItem and removeMediaItems.

open ListenableFuture<Any?>
@ForOverride
handleReplaceMediaItems(
    fromIndex: Int,
    toIndex: Int,
    mediaItems: MutableList<MediaItem>
)

Handles calls to replaceMediaItem and replaceMediaItems.

open ListenableFuture<Any?>
@ForOverride
handleSetAudioAttributes(
    audioAttributes: AudioAttributes,
    handleAudioFocus: Boolean
)

Handles calls to set the audio attributes.

open ListenableFuture<Any?>

Handles calls to setDeviceMuted and setDeviceMuted.

open ListenableFuture<Any?>
@ForOverride
handleSetDeviceVolume(
    deviceVolume: @IntRange(from = 0) Int,
    @C.VolumeFlags flags: @C.VolumeFlags Int
)

Handles calls to setDeviceVolume and setDeviceVolume.

open ListenableFuture<Any?>

Handles calls to setPlaylistMetadata.

open ListenableFuture<Any?>
@ForOverride
handleSetVolume(volume: @FloatRange(from = 0, to = 1.0) Float)

This function is deprecated.

Use handleSetVolume instead.

Unit

Invalidates the current state.

Unit

Throws an IllegalStateException if the the thread calling this method does not match the Looper thread that was specified upon construction of this instance.

Inherited properties

From androidx.media3.common.BasePlayer
From androidx.media3.common.SimpleBasePlayer

Public constructors

FakePlayer

FakePlayer(
    playbackState: @Player.State Int = STATE_IDLE,
    playWhenReady: Boolean = false,
    playlist: List<SimpleBasePlayer.MediaItemData> = ImmutableList.of(),
    playbackSpeed: Float = 1.0f,
    bufferingDelayMs: Long = 100
)
Parameters
playbackState: @Player.State Int = STATE_IDLE

The initial playback state of the player.

playWhenReady: Boolean = false

Whether playback should start automatically when ready.

playlist: List<SimpleBasePlayer.MediaItemData> = ImmutableList.of()

The initial list of media items to play.

playbackSpeed: Float = 1.0f

The initial playback speed.

bufferingDelayMs: Long = 100

The time spent in Player#STATE_BUFFERING when preparing or seeking, in milliseconds. A value of zero will result in the player transitioning directly to Player#STATE_READY without entering Player#STATE_BUFFERING. Must not be negative.

Public functions

addCommands

fun addCommands(vararg commands: Int): Unit

Add commands to those already available to the player.

removeCommands

fun removeCommands(vararg commands: Int): Unit

Remove commands from those already available to the player.

renderFirstFrame

fun renderFirstFrame(newlyRenderedFirstFrame: Boolean): Unit

Sets whether a frame has been rendered for the first time since setting the surface, a rendering reset, or since the stream being rendered was changed.

setBufferedPositionMs

fun setBufferedPositionMs(bufferedPositionMs: Long): Unit

Sets the buffered content playback position in milliseconds.

Parameters
bufferedPositionMs: Long

The buffered content playback position in milliseconds.

setDuration

fun setDuration(uid: String, durationMs: Long): Unit

Sets the duration of the media item in the playlist, in milliseconds.

Parameters
uid: String

Unique id of the media item whose duration is being set.

durationMs: Long

The duration of the media item, in milliseconds, or {@link C#TIME_UNSET} if unknown.

setPlaybackState

fun setPlaybackState(playbackState: @Player.State Int): Unit

Sets the {@linkplain Player.State state} of the player.

If the playlist is empty, the state must be either Player.STATE_IDLE or Player.STATE_ENDED.

Parameters
playbackState: @Player.State Int

The Player.State of the player.

setPosition

fun setPosition(positionMs: Long): Unit

Sets the current content playback position in milliseconds.

Parameters
positionMs: Long

The current content playback position in milliseconds, or C.TIME_UNSET to indicate the default start position.

setPositionSupplierDrivenBy

fun setPositionSupplierDrivenBy(testScheduler: TestCoroutineScheduler): Unit

Sets the PositionSupplier for the current content playback position using extrapolation. Start and elapsed time are measured using the TestCoroutineScheduler, and playback speed is taken into account.

Note, that this approach only works

  • for a single media item (does not consider current item's duration and playlist length)

  • assuming the Player is playing (does not react to pauses and discontinuities)

  • assuming the playback speed does not change after the supplier is set

setSeekBackIncrementMs

fun setSeekBackIncrementMs(seekBackIncrementMs: Long): Unit

Sets the Player.seekBack increment in milliseconds.

Parameters
seekBackIncrementMs: Long

The Player.seekBack increment in milliseconds.

setSeekForwardIncrementMs

fun setSeekForwardIncrementMs(seekForwardIncrementMs: Long): Unit

Sets the Player.seekForward increment in milliseconds.

Parameters
seekForwardIncrementMs: Long

The Player.seekForward increment in milliseconds.

setVideoSize

fun setVideoSize(videoSize: VideoSize): Unit

Sets the current video size.

Parameters
videoSize: VideoSize

The current video size.

Protected functions

getState

protected open fun getState(): SimpleBasePlayer.State

Returns the current State of the player.

The State should include all available commands indicating which player methods are allowed to be called.

Note that this method won't be called while asynchronous handling of player methods is in progress. This means that the implementation doesn't need to handle state changes caused by these asynchronous operations until they are done and can return the currently known state directly. The placeholder state used while these asynchronous operations are in progress can be customized by overriding getPlaceholderState if required.

handleClearVideoOutput

protected open fun handleClearVideoOutput(videoOutput: Any?): ListenableFuture<*>

Handles calls to clear the video output.

Will only be called if COMMAND_SET_VIDEO_SURFACE is available.

Parameters
videoOutput: Any?

The video output to clear. If null any current output should be cleared. If non-null, the output should only be cleared if it matches the provided argument. This is either a Surface, SurfaceHolder, TextureView or SurfaceView.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handlePrepare

protected open fun handlePrepare(): ListenableFuture<*>

Handles calls to prepare.

Will only be called if COMMAND_PREPARE is available.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleRelease

protected open fun handleRelease(): ListenableFuture<*>

Handles calls to release.

Will only be called if COMMAND_RELEASE is available.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSeek

protected open fun handleSeek(
    mediaItemIndex: Int,
    positionMs: Long,
    seekCommand: @Player.Command Int
): ListenableFuture<*>

Handles calls to seekTo and other seek operations (for example, seekToNext).

Will only be called if the appropriate Player.Command, for example COMMAND_SEEK_TO_MEDIA_ITEM or COMMAND_SEEK_TO_NEXT, is available.

Parameters
mediaItemIndex: Int

The media item index to seek to. If the original seek operation did not directly specify an index, this is the most likely implied index based on the available player state. If the implied action is to do nothing, this will be INDEX_UNSET.

positionMs: Long

The position in milliseconds to start playback from, or TIME_UNSET to start at the default position in the media item. If the original seek operation did not directly specify a position, this is the most likely implied position based on the available player state.

seekCommand: @Player.Command Int

The Player.Command used to trigger the seek.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetMediaItems

protected open fun handleSetMediaItems(
    mediaItems: MutableList<MediaItem>,
    startIndex: Int,
    startPositionMs: Long
): ListenableFuture<*>

Handles calls to setMediaItem and setMediaItems.

Will only be called if COMMAND_SET_MEDIA_ITEM or COMMAND_CHANGE_MEDIA_ITEMS is available. If only COMMAND_SET_MEDIA_ITEM is available, the list of media items will always contain exactly one item.

Parameters
mediaItems: MutableList<MediaItem>

The media items to add.

startIndex: Int

The index at which to start playback from, or INDEX_UNSET to start at the default item.

startPositionMs: Long

The position in milliseconds to start playback from, or TIME_UNSET to start at the default position in the media item.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetPlayWhenReady

protected open fun handleSetPlayWhenReady(playWhenReady: Boolean): ListenableFuture<*>

Handles calls to setPlayWhenReady, play and pause.

Will only be called if COMMAND_PLAY_PAUSE is available.

Parameters
playWhenReady: Boolean

The requested playWhenReady

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetPlaybackParameters

protected open fun handleSetPlaybackParameters(playbackParameters: PlaybackParameters): ListenableFuture<*>

Handles calls to setPlaybackParameters or setPlaybackSpeed.

Will only be called if COMMAND_SET_SPEED_AND_PITCH is available.

Parameters
playbackParameters: PlaybackParameters

The requested PlaybackParameters.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetRepeatMode

protected open fun handleSetRepeatMode(repeatMode: Int): ListenableFuture<*>

Handles calls to setRepeatMode.

Will only be called if COMMAND_SET_REPEAT_MODE is available.

Parameters
repeatMode: Int

The requested RepeatMode.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetShuffleModeEnabled

protected open fun handleSetShuffleModeEnabled(shuffleModeEnabled: Boolean): ListenableFuture<*>

Handles calls to setShuffleModeEnabled.

Will only be called if COMMAND_SET_SHUFFLE_MODE is available.

Parameters
shuffleModeEnabled: Boolean

Whether shuffle mode was requested to be enabled.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetTrackSelectionParameters

protected open fun handleSetTrackSelectionParameters(
    trackSelectionParameters: TrackSelectionParameters
): ListenableFuture<*>

Handles calls to setTrackSelectionParameters.

Will only be called if COMMAND_SET_TRACK_SELECTION_PARAMETERS is available.

Parameters
trackSelectionParameters: TrackSelectionParameters

The requested TrackSelectionParameters.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetVideoOutput

protected open fun handleSetVideoOutput(videoOutput: Any): ListenableFuture<*>

Handles calls to set the video output.

Will only be called if COMMAND_SET_VIDEO_SURFACE is available.

Parameters
videoOutput: Any

The requested video output. This is either a Surface, SurfaceHolder, TextureView or SurfaceView.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleSetVolume

protected open fun handleSetVolume(
    volume: Float,
    volumeOperationType: @C.VolumeOperationType Int
): ListenableFuture<*>

Handles calls to setVolume.

Will only be called if COMMAND_SET_VOLUME is available.

Parameters
volume: Float

The requested audio volume, with 0 being silence and 1 being unity gain (signal unchanged).

volumeOperationType: @C.VolumeOperationType Int

The C.VolumeOperationType that corresponds to the original command for changing of the volume.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

handleStop

protected open fun handleStop(): ListenableFuture<*>

Handles calls to stop.

Will only be called if COMMAND_STOP is available.

Returns
ListenableFuture<*>

A ListenableFuture indicating the completion of all immediate State changes caused by this call.

Public properties

released

var releasedBoolean

Whether the player is released

videoOutput

val videoOutputAny?

Current video output

Protected properties

state

protected open val stateSimpleBasePlayer.State