Skip to content

Commit

Permalink
refactor: pass uri instead of tag in onMediaPlayerError
Browse files Browse the repository at this point in the history
it were used only to build an uri, so just pass the uri
  • Loading branch information
BrayanDSO authored and david-allison committed Feb 2, 2024
1 parent d8dcf7e commit 2ccbb28
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
14 changes: 4 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/SoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class SoundPlayer(
@NeedsTest("ensure the lifecycle is subscribed to in a Reviewer")
fun newInstance(viewer: AbstractFlashcardViewer, soundUriBase: String): SoundPlayer {
val scope = viewer.lifecycleScope
val soundErrorListener = viewer.createSoundErrorListener(soundUriBase)
val soundErrorListener = viewer.createSoundErrorListener()
// tts can take a long time to init, this defers the operation until it's needed
val tts = scope.async(Dispatchers.IO) { AndroidTtsPlayer.createInstance(viewer, viewer.lifecycleScope) }

Expand All @@ -337,7 +337,7 @@ interface SoundErrorListener {
fun onError(uri: Uri): SoundErrorBehavior

@CheckResult
fun onMediaPlayerError(mp: MediaPlayer?, which: Int, extra: Int, tag: SoundOrVideoTag): SoundErrorBehavior
fun onMediaPlayerError(mp: MediaPlayer?, which: Int, extra: Int, uri: Uri): SoundErrorBehavior
fun onTtsError(error: TtsPlayer.TtsError, isAutomaticPlayback: Boolean)
}

Expand All @@ -352,7 +352,7 @@ enum class SoundErrorBehavior {
RETRY_AUDIO
}

fun AbstractFlashcardViewer.createSoundErrorListener(baseUri: String): SoundErrorListener {
fun AbstractFlashcardViewer.createSoundErrorListener(): SoundErrorListener {
val activity = this
return object : SoundErrorListener {
private var handledError: HashSet<String> = hashSetOf()
Expand All @@ -370,15 +370,9 @@ fun AbstractFlashcardViewer.createSoundErrorListener(baseUri: String): SoundErro
mp: MediaPlayer?,
which: Int,
extra: Int,
tag: SoundOrVideoTag
uri: Uri
): SoundErrorBehavior {
Timber.w("Media Error: (%d, %d)", which, extra)
val uri = try {
Uri.parse(baseUri + tag.filename)
} catch (e: Exception) {
Timber.w(e)
return CONTINUE_AUDIO
}
return onError(uri)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,20 @@ class SoundTagPlayer(private val soundUriBase: String) {
continuation.resume(Unit)
}
}

val soundUri = Uri.parse(soundUriBase + tag.filename)
setAudioAttributes(music)
setOnErrorListener { mp, what, extra ->
Timber.w("Media error %d", what)
abandonAudioFocus()
val continuationBehavior =
soundErrorListener.onMediaPlayerError(mp, what, extra, tag)
soundErrorListener.onMediaPlayerError(mp, what, extra, soundUri)
// 15103: setOnErrorListener can be invoked after task cancellation
if (!continuation.isCompleted) {
continuation.resumeWithException(SoundException(continuationBehavior))
}
true // do not call onCompletionListen
}

val soundUri = Uri.parse(soundUriBase + tag.filename)
try {
awaitSetDataSource(soundUri)
} catch (e: Exception) {
Expand Down

0 comments on commit 2ccbb28

Please sign in to comment.