File tree Expand file tree Collapse file tree 6 files changed +27
-4
lines changed
apps/common-app/src/examples/AudioFile
packages/react-native-audio-api
android/src/main/java/com/swmansion/audioapi Expand file tree Collapse file tree 6 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -68,8 +68,7 @@ const AudioFile: FC = () => {
6868 state : 'state_playing' ,
6969 } ) ;
7070
71- AudioManager . requestAudioFocus ( { } ) ;
72- // AudioManager.abandonAudioFocus();
71+ AudioManager . observeAudioInterruptions ( true ) ;
7372
7473 bufferSourceRef . current = audioContextRef . current . createBufferSource ( {
7574 pitchCorrection : true ,
@@ -153,7 +152,7 @@ const AudioFile: FC = () => {
153152 }
154153 ) ;
155154
156- AudioManager . requestAudioFocus ( { } ) ;
155+ AudioManager . observeAudioInterruptions ( true ) ;
157156
158157 fetchAudioBuffer ( ) ;
159158
Original file line number Diff line number Diff line change @@ -80,6 +80,11 @@ class AudioAPIModule(
8080
8181 override fun getDevicePreferredSampleRate (): Double = MediaSessionManager .getDevicePreferredSampleRate()
8282
83+ @RequiresApi(Build .VERSION_CODES .O )
84+ override fun observeAudioInterruptions (enabled : Boolean ) {
85+ MediaSessionManager .observeAudioInterruptions(enabled)
86+ }
87+
8388 override fun observeVolumeChanges (enabled : Boolean ) {
8489 MediaSessionManager .observeVolumeChanges(enabled)
8590 }
Original file line number Diff line number Diff line change @@ -139,6 +139,15 @@ object MediaSessionManager {
139139 return sampleRate.toDouble()
140140 }
141141
142+ @RequiresApi(Build .VERSION_CODES .O )
143+ fun observeAudioInterruptions (observe : Boolean ) {
144+ if (observe) {
145+ audioFocusListener.requestAudioFocus(AudioFocusRequest .Builder (AudioManager .AUDIOFOCUS_GAIN ), true )
146+ } else {
147+ audioFocusListener.abandonAudioFocus()
148+ }
149+ }
150+
142151 fun observeVolumeChanges (observe : Boolean ) {
143152 if (observe) {
144153 ContextCompat .registerReceiver(
Original file line number Diff line number Diff line change @@ -107,7 +107,12 @@ - (void)invalidate
107107
108108RCT_EXPORT_METHOD (observeVolumeChanges : (BOOL )enabled)
109109{
110- [self .notificationManager observeVolumeChanges: (BOOL )enabled];
110+ [self .notificationManager observeVolumeChanges: enabled];
111+ }
112+
113+ RCT_EXPORT_METHOD (observeAudioInterruptions : (BOOL )enabled)
114+ {
115+ [self .notificationManager observeAudioInterruptions: enabled];
111116}
112117
113118// android-only support for options
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ interface Spec extends TurboModule {
1717 options : Array < string >
1818 ) : void ;
1919 getDevicePreferredSampleRate ( ) : number ;
20+ observeAudioInterruptions ( enabled : boolean ) : void ;
2021 requestAudioFocus (
2122 options : {
2223 [ key : string ] : string | boolean | number | AudioAttributeType | undefined ;
Original file line number Diff line number Diff line change @@ -44,6 +44,10 @@ class AudioManager {
4444 return NativeAudioAPIModule ! . getDevicePreferredSampleRate ( ) ;
4545 }
4646
47+ observeAudioInterruptions ( enabled : boolean ) {
48+ NativeAudioAPIModule ! . observeAudioInterruptions ( enabled ) ;
49+ }
50+
4751 requestAudioFocus (
4852 request : RequestAudioFocusOptions ,
4953 observeAudioInterruption = true
You can’t perform that action at this time.
0 commit comments