Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4296d83

Browse files
authored
Always play ringtone despite audio focus (#1515)
* fix: Always play ringtone despite audio focus * chore: refactor code
1 parent c57367d commit 4296d83

File tree

1 file changed

+3
-28
lines changed
  • stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds

1 file changed

+3
-28
lines changed

‎stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/CallSoundPlayer.kt‎

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal class CallSoundPlayer(private val context: Context) {
3838
fun playCallSound(soundUri: Uri?, playIfMuted: Boolean = false) {
3939
try {
4040
synchronized(this) {
41-
requestAudioFocus(playIfMuted) {
41+
requestAudioFocus {
4242
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
4343
playWithRingtoneManager(soundUri, playIfMuted)
4444
} else {
@@ -51,7 +51,7 @@ internal class CallSoundPlayer(private val context: Context) {
5151
}
5252
}
5353

54-
private fun requestAudioFocus(playIfMuted: Boolean = false, onGranted: () -> Unit) {
54+
private fun requestAudioFocus(onGranted: () -> Unit) {
5555
if (audioManager == null) {
5656
(context.getSystemService(Context.AUDIO_SERVICE) as? AudioManager)?.let {
5757
audioManager = it
@@ -61,32 +61,7 @@ internal class CallSoundPlayer(private val context: Context) {
6161
}
6262
}
6363

64-
val isGranted = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
65-
audioFocusRequest = AudioFocusRequest
66-
.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
67-
.setAudioAttributes(
68-
if (playIfMuted) {
69-
AudioAttributes.Builder().setVoiceCommunicationAttributes().build()
70-
} else {
71-
AudioAttributes.Builder().setNotificationRingtoneAttributes().build()
72-
},
73-
)
74-
.setAcceptsDelayedFocusGain(false)
75-
.build()
76-
77-
audioFocusRequest?.let {
78-
audioManager?.requestAudioFocus(it) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
79-
} ?: false
80-
} else {
81-
audioManager?.requestAudioFocus(
82-
null,
83-
if (playIfMuted) AudioManager.STREAM_VOICE_CALL else AudioManager.STREAM_RING,
84-
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE,
85-
) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
86-
}
87-
88-
logger.d { "[requestAudioFocus] Audio focus " + if (isGranted) "granted" else "not granted" }
89-
if (isGranted) onGranted()
64+
onGranted()
9065
}
9166

9267
@RequiresApi(Build.VERSION_CODES.P)

0 commit comments

Comments
 (0)