Skip to content

Commit

Permalink
fix(android): Allow AudioFocus.none (#1534)
Browse files Browse the repository at this point in the history
# Description

- fix(android): allow AudioFocus.none (closes #1495)
- refactor: values for AudioContext enums

## Related Issues

Closes #1495
Closes #1534
  • Loading branch information
Gustl22 committed Jun 12, 2023
1 parent 9299535 commit 858d3f4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 159 deletions.
Expand Up @@ -14,7 +14,7 @@ data class AudioContextAndroid(
val stayAwake: Boolean,
val contentType: Int,
val usageType: Int,
val audioFocus: Int?,
val audioFocus: Int,
val audioMode: Int,
) {
@SuppressLint("InlinedApi") // we are just using numerical constants
Expand All @@ -23,7 +23,7 @@ data class AudioContextAndroid(
stayAwake = false,
contentType = CONTENT_TYPE_MUSIC,
usageType = USAGE_MEDIA,
audioFocus = null,
audioFocus = AudioManager.AUDIOFOCUS_GAIN,
audioMode = AudioManager.MODE_NORMAL,
)

Expand Down
Expand Up @@ -336,7 +336,7 @@ private fun MethodCall.audioContext(): AudioContextAndroid {
stayAwake = argument<Boolean>("stayAwake") ?: error("stayAwake is required"),
contentType = argument<Int>("contentType") ?: error("contentType is required"),
usageType = argument<Int>("usageType") ?: error("usageType is required"),
audioFocus = argument<Int>("audioFocus"),
audioFocus = argument<Int>("audioFocus") ?: error("audioFocus is required"),
audioMode = argument<Int>("audioMode") ?: error("audioMode is required"),
)
}
Expand Down
Expand Up @@ -19,7 +19,7 @@ class FocusManager(
get() = player.audioManager

fun maybeRequestAudioFocus(andThen: () -> Unit) {
if (context.audioFocus == null) {
if (context.audioFocus == AudioManager.AUDIOFOCUS_NONE) {
andThen()
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
newRequestAudioFocus(andThen)
Expand All @@ -30,7 +30,7 @@ class FocusManager(
}

fun handleStop() {
if (context.audioFocus != null) {
if (context.audioFocus != AudioManager.AUDIOFOCUS_NONE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioFocusRequest?.let { audioManager.abandonAudioFocusRequest(it) }
} else {
Expand Down Expand Up @@ -72,4 +72,4 @@ class FocusManager(
andThen()
}
}
}
}
Expand Up @@ -137,7 +137,8 @@ class WrappedPlayer internal constructor(
if (context == audioContext) {
return
}
if (context.audioFocus != null && audioContext.audioFocus == null) {
if (context.audioFocus != AudioManager.AUDIOFOCUS_NONE
&& audioContext.audioFocus == AudioManager.AUDIOFOCUS_NONE) {
focusManager.handleStop()
}
this.context = audioContext.copy()
Expand Down

0 comments on commit 858d3f4

Please sign in to comment.