Voice message MediaPlayer: wait until player is ready (#1772)

Change to `MediaPlayer` API to allow waiting for the player to be in a ready state.
This is needed in order to perform some tasks (e.g. read the media duration, seek) after changing the media file.
This commit is contained in:
Marco Romano
2023-11-09 15:34:38 +01:00
committed by GitHub
parent b83d8733e2
commit 878417f557
10 changed files with 110 additions and 28 deletions
@@ -30,13 +30,15 @@ interface MediaPlayer : AutoCloseable {
val state: StateFlow<State>
/**
* Acquires control of the player and starts playing the given media.
* Initialises the player with a new media item, will suspend until the player is ready.
*
* @return the ready state of the player.
*/
fun acquireControlAndPlay(
suspend fun setMedia(
uri: String,
mediaId: String,
mimeType: String,
)
): State
/**
* Plays the current media.
@@ -59,6 +61,10 @@ interface MediaPlayer : AutoCloseable {
override fun close()
data class State(
/**
* Whether the player is ready to play.
*/
val isReady: Boolean,
/**
* Whether the player is currently playing.
*/
@@ -75,8 +81,8 @@ interface MediaPlayer : AutoCloseable {
*/
val currentPosition: Long,
/**
* The duration of the current content.
* The duration of the current content, if available.
*/
val duration: Long,
val duration: Long?,
)
}