@UnstableApi
public final class PlayerMessage


Defines a player message which can be sent with a Sender and received by a .

Summary

Nested types

public interface PlayerMessage.Sender

A sender for messages.

public interface PlayerMessage.Target

A target for messages.

Public constructors

PlayerMessage(
    PlayerMessage.Sender sender,
    PlayerMessage.Target target,
    Timeline timeline,
    int defaultMediaItemIndex,
    Clock clock,
    Looper defaultLooper
)

Creates a new message.

Public methods

synchronized boolean

Blocks until after the message has been delivered or the player is no longer able to deliver the message.

synchronized boolean
blockUntilDelivered(long timeoutMs)

Blocks until after the message has been delivered or the player is no longer able to deliver the message or the specified timeout elapsed.

synchronized PlayerMessage

Cancels the message delivery.

boolean

Returns whether the message will be deleted after delivery.

Looper

Returns the Looper the message is delivered on.

int

Returns media item index at which the message will be delivered.

@Nullable Object

Returns the message payload forwarded to handleMessage.

long

Returns position in the media item at getMediaItemIndex at which the message will be delivered, in milliseconds.

PlayerMessage.Target

Returns the target the message is sent to.

Timeline

Returns the timeline used for setting the position with setPosition.

int

Returns the message type forwarded to handleMessage.

synchronized boolean

Returns whether the message delivery has been canceled.

synchronized void
markAsProcessed(boolean isDelivered)

Marks the message as processed.

PlayerMessage

Sends the message.

PlayerMessage
@CanIgnoreReturnValue
setDeleteAfterDelivery(boolean deleteAfterDelivery)

Sets whether the message will be deleted after delivery.

PlayerMessage

This method is deprecated.

Use setLooper instead.

PlayerMessage

Sets the Looper the message is delivered on.

PlayerMessage

Sets the message payload forwarded to handleMessage.

PlayerMessage

Sets a position in the current media item at which the message will be delivered.

PlayerMessage
@CanIgnoreReturnValue
setPosition(int mediaItemIndex, long positionMs)

Sets a position in a media item at which the message will be delivered.

PlayerMessage
@CanIgnoreReturnValue
setType(int messageType)

Sets the message type forwarded to handleMessage.

Public fields

deleteAfterDelivery

public boolean deleteAfterDelivery

isCanceled

public boolean isCanceled

looper

public Looper looper

mediaItemIndex

public int mediaItemIndex

payload

public @Nullable Object payload

positionMs

public long positionMs

target

public final PlayerMessage.Target target

timeline

public final Timeline timeline

type

public int type

Public constructors

PlayerMessage

public PlayerMessage(
    PlayerMessage.Sender sender,
    PlayerMessage.Target target,
    Timeline timeline,
    int defaultMediaItemIndex,
    Clock clock,
    Looper defaultLooper
)

Creates a new message.

Parameters
PlayerMessage.Sender sender

The Sender used to send the message.

PlayerMessage.Target target

The Target the message is sent to.

Timeline timeline

The timeline used when setting the position with setPosition. If set to EMPTY, any position can be specified.

int defaultMediaItemIndex

The default media item index in the timeline when no other media item index is specified.

Clock clock

The Clock.

Looper defaultLooper

The default Looper to send the message on when no other looper is specified.

Public methods

blockUntilDelivered

synchronized public boolean blockUntilDelivered()

Blocks until after the message has been delivered or the player is no longer able to deliver the message.

Note that this method must not be called if the current thread is the same thread used by the message looper as it would cause a deadlock.

Returns
boolean

Whether the message was delivered successfully.

Throws
java.lang.IllegalStateException

If this method is called before send.

java.lang.IllegalStateException

If this method is called on the same thread used by the message looper.

java.lang.InterruptedException

If the current thread is interrupted while waiting for the message to be delivered.

blockUntilDelivered

synchronized public boolean blockUntilDelivered(long timeoutMs)

Blocks until after the message has been delivered or the player is no longer able to deliver the message or the specified timeout elapsed.

Note that this method must not be called if the current thread is the same thread used by the message looper as it would cause a deadlock.

Parameters
long timeoutMs

The timeout in milliseconds.

Returns
boolean

Whether the message was delivered successfully.

Throws
java.lang.IllegalStateException

If this method is called before send.

java.lang.IllegalStateException

If this method is called on the same thread used by the message looper.

java.util.concurrent.TimeoutException

If the timeoutMs elapsed and this message has not been delivered and the player is still able to deliver the message.

java.lang.InterruptedException

If the current thread is interrupted while waiting for the message to be delivered.

cancel

@CanIgnoreReturnValue
synchronized public PlayerMessage cancel()

Cancels the message delivery.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If this method is called before send.

getDeleteAfterDelivery

public boolean getDeleteAfterDelivery()

Returns whether the message will be deleted after delivery.

getLooper

public Looper getLooper()

Returns the Looper the message is delivered on.

getMediaItemIndex

public int getMediaItemIndex()

Returns media item index at which the message will be delivered.

getPayload

public @Nullable Object getPayload()

Returns the message payload forwarded to handleMessage.

getPositionMs

public long getPositionMs()

Returns position in the media item at getMediaItemIndex at which the message will be delivered, in milliseconds. If TIME_UNSET, the message will be delivered immediately. If TIME_END_OF_SOURCE, the message will be delivered at the end of the media item at getMediaItemIndex.

getTarget

public PlayerMessage.Target getTarget()

Returns the target the message is sent to.

getTimeline

public Timeline getTimeline()

Returns the timeline used for setting the position with setPosition.

getType

public int getType()

Returns the message type forwarded to handleMessage.

isCanceled

synchronized public boolean isCanceled()

Returns whether the message delivery has been canceled.

markAsProcessed

synchronized public void markAsProcessed(boolean isDelivered)

Marks the message as processed. Should only be called by a Sender and may be called multiple times.

Parameters
boolean isDelivered

Whether the message has been delivered to its target. The message is considered as being delivered when this method has been called with isDelivered set to true at least once.

send

@CanIgnoreReturnValue
public PlayerMessage send()

Sends the message. If the target throws an ExoPlaybackException then it is propagated out of the player as an error using onPlayerError.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If this message has already been sent.

setDeleteAfterDelivery

@CanIgnoreReturnValue
public PlayerMessage setDeleteAfterDelivery(boolean deleteAfterDelivery)

Sets whether the message will be deleted after delivery. If false, the message will be resent if playback reaches the specified position again. Only allowed to be false if a position is set with setPosition.

Parameters
boolean deleteAfterDelivery

Whether the message is deleted after delivery.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If send has already been called.

setHandler

@CanIgnoreReturnValue
public PlayerMessage setHandler(Handler handler)

setLooper

@CanIgnoreReturnValue
public PlayerMessage setLooper(Looper looper)

Sets the Looper the message is delivered on.

Parameters
Looper looper

A Looper.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If send has already been called.

setPayload

@CanIgnoreReturnValue
public PlayerMessage setPayload(@Nullable Object payload)

Sets the message payload forwarded to handleMessage.

Parameters
@Nullable Object payload

The message payload.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If send has already been called.

setPosition

@CanIgnoreReturnValue
public PlayerMessage setPosition(long positionMs)

Sets a position in the current media item at which the message will be delivered.

Parameters
long positionMs

The position in the current media item at which the message will be sent, in milliseconds, or TIME_END_OF_SOURCE to deliver the message at the end of the current media item.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If send has already been called.

setPosition

@CanIgnoreReturnValue
public PlayerMessage setPosition(int mediaItemIndex, long positionMs)

Sets a position in a media item at which the message will be delivered.

Parameters
int mediaItemIndex

The index of the media item at which the message will be sent.

long positionMs

The position in the media item with index mediaItemIndex at which the message will be sent, in milliseconds, or TIME_END_OF_SOURCE to deliver the message at the end of the media item with index mediaItemIndex.

Returns
PlayerMessage

This message.

Throws
androidx.media3.common.IllegalSeekPositionException

If the timeline returned by getTimeline is not empty and the provided media item index is not within the bounds of the timeline.

java.lang.IllegalStateException

If send has already been called.

setType

@CanIgnoreReturnValue
public PlayerMessage setType(int messageType)

Sets the message type forwarded to handleMessage.

Parameters
int messageType

The message type.

Returns
PlayerMessage

This message.

Throws
java.lang.IllegalStateException

If send has already been called.