@@ -7,6 +7,7 @@ import android.media.AudioManager
77import android.net.Uri
88import android.os.Build
99import android.os.CountDownTimer
10+ import android.os.Handler
1011import com.facebook.react.bridge.Arguments
1112import com.facebook.react.bridge.Promise
1213import com.facebook.react.bridge.ReactApplicationContext
@@ -54,33 +55,41 @@ class AudioPlayer(
5455 private fun handleAudioFocusChange (focusChange : Int ) {
5556 when (focusChange) {
5657 AudioManager .AUDIOFOCUS_GAIN -> {
57- // Audio focus granted; resume playback if necessary
58- if (! player.isPlaying) {
59- player.play()
60- }
61- player.volume = 1.0f // Restore full volume
58+ player.applicationLooper.let { looper -> Handler (looper).post {
59+ // Audio focus granted; resume playback if necessary
60+ if (! player.isPlaying) {
61+ player.play()
62+ }
63+ player.volume = 1.0f // Restore full volume
64+ }}
6265 }
6366 AudioManager .AUDIOFOCUS_LOSS -> {
64- // Permanent loss of audio focus; pause playback
65- if (player.isPlaying) {
66- val args: WritableMap = Arguments .createMap()
67- stopListening()
68- player.pause()
69- abandonAudioFocus()
70- args.putInt(Constants .finishType, 1 )
71- args.putString(Constants .playerKey, key)
72- appContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)?.emit(" onDidFinishPlayingAudio" , args)
73- }
67+ player.applicationLooper.let { looper -> Handler (looper).post {
68+ // Permanent loss of audio focus; pause playback
69+ if (player.isPlaying) {
70+ val args: WritableMap = Arguments .createMap()
71+ stopListening()
72+ player.pause()
73+ abandonAudioFocus()
74+ args.putInt(Constants .finishType, 1 )
75+ args.putString(Constants .playerKey, key)
76+ appContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)?.emit(" onDidFinishPlayingAudio" , args)
77+ }
78+ }}
7479 }
7580 AudioManager .AUDIOFOCUS_LOSS_TRANSIENT -> {
76- // Temporary loss of audio focus; pause playback
77- if (player.isPlaying) {
78- player.pause()
79- }
81+ player.applicationLooper.let { looper -> Handler (looper).post {
82+ // Temporary loss of audio focus; pause playback
83+ if (player.isPlaying) {
84+ player.pause()
85+ }
86+ }}
8087 }
8188 AudioManager .AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
82- // Temporarily loss of audio focus; but can continue playing at a lower volume.
83- player.volume = 0.2f
89+ player.applicationLooper.let { looper -> Handler (looper).post {
90+ // Temporarily loss of audio focus; but can continue playing at a lower volume.
91+ player.volume = 0.2f
92+ }}
8493 }
8594 }
8695 }
0 commit comments