Skip to content

Commit

Permalink
Prevent crash in case of misuse of AudioEngine.asPlayer().release (re…
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Jun 7, 2024
1 parent a226128 commit 852d1c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ public class ExoPlayerEngine private constructor(
private val _playback: MutableStateFlow<AudioEngine.Playback> =
MutableStateFlow(exoPlayer.playback)

private val sessionPlayer = object :
ForwardingPlayer(exoPlayer) {

override fun release() {
// This object does not own the ExoAudiobookPlayer instance, do not close it.
}
}

init {
coroutineScope.launch {
val positionRefreshDelay = (1.0 / configuration.positionRefreshRate.value).seconds
Expand Down Expand Up @@ -227,7 +235,7 @@ public class ExoPlayerEngine private constructor(
}

override fun asPlayer(): Player {
return exoPlayer
return sessionPlayer
}

override fun submitPreferences(preferences: ExoPlayerPreferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public interface AudioEngine<S : Configurable.Settings, P : Configurable.Prefere
/**
* The player is ready to play.
*/
public object Ready : State()
public data object Ready : State()

/**
* The end of the content has been reached.
*/
public object Ended : State()
public data object Ended : State()

/**
* The engine cannot play because the buffer is starved.
*/
public object Buffering : State()
public data object Buffering : State()

/**
* The engine cannot play because an error occurred.
*/
public data class Failure(val error: AudioEngine.Error) : State()
public data class Failure(val error: Error) : State()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package org.readium.navigator.media.tts
import org.readium.r2.navigator.preferences.Configurable
import org.readium.r2.shared.ExperimentalReadiumApi
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.Error
import org.readium.r2.shared.util.Language

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public class TtsNavigator<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,

public sealed class State {

public object Ready : MediaNavigator.State.Ready
public data object Ready : MediaNavigator.State.Ready

public object Ended : MediaNavigator.State.Ended
public data object Ended : MediaNavigator.State.Ended

public data class Failure(val error: Error) : MediaNavigator.State.Failure
}
Expand Down

0 comments on commit 852d1c9

Please sign in to comment.