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
6 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -68,8 +68,7 @@ const AudioFile: FC = () => {
68
68
state : 'state_playing' ,
69
69
} ) ;
70
70
71
- AudioManager . requestAudioFocus ( { } ) ;
72
- // AudioManager.abandonAudioFocus();
71
+ AudioManager . observeAudioInterruptions ( true ) ;
73
72
74
73
bufferSourceRef . current = audioContextRef . current . createBufferSource ( {
75
74
pitchCorrection : true ,
@@ -153,7 +152,7 @@ const AudioFile: FC = () => {
153
152
}
154
153
) ;
155
154
156
- AudioManager . requestAudioFocus ( { } ) ;
155
+ AudioManager . observeAudioInterruptions ( true ) ;
157
156
158
157
fetchAudioBuffer ( ) ;
159
158
Original file line number Diff line number Diff line change @@ -80,6 +80,11 @@ class AudioAPIModule(
80
80
81
81
override fun getDevicePreferredSampleRate (): Double = MediaSessionManager .getDevicePreferredSampleRate()
82
82
83
+ @RequiresApi(Build .VERSION_CODES .O )
84
+ override fun observeAudioInterruptions (enabled : Boolean ) {
85
+ MediaSessionManager .observeAudioInterruptions(enabled)
86
+ }
87
+
83
88
override fun observeVolumeChanges (enabled : Boolean ) {
84
89
MediaSessionManager .observeVolumeChanges(enabled)
85
90
}
Original file line number Diff line number Diff line change @@ -139,6 +139,15 @@ object MediaSessionManager {
139
139
return sampleRate.toDouble()
140
140
}
141
141
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
+
142
151
fun observeVolumeChanges (observe : Boolean ) {
143
152
if (observe) {
144
153
ContextCompat .registerReceiver(
Original file line number Diff line number Diff line change @@ -107,7 +107,12 @@ - (void)invalidate
107
107
108
108
RCT_EXPORT_METHOD (observeVolumeChanges : (BOOL )enabled)
109
109
{
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];
111
116
}
112
117
113
118
// android-only support for options
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ interface Spec extends TurboModule {
17
17
options : Array < string >
18
18
) : void ;
19
19
getDevicePreferredSampleRate ( ) : number ;
20
+ observeAudioInterruptions ( enabled : boolean ) : void ;
20
21
requestAudioFocus (
21
22
options : {
22
23
[ key : string ] : string | boolean | number | AudioAttributeType | undefined ;
Original file line number Diff line number Diff line change @@ -44,6 +44,10 @@ class AudioManager {
44
44
return NativeAudioAPIModule ! . getDevicePreferredSampleRate ( ) ;
45
45
}
46
46
47
+ observeAudioInterruptions ( enabled : boolean ) {
48
+ NativeAudioAPIModule ! . observeAudioInterruptions ( enabled ) ;
49
+ }
50
+
47
51
requestAudioFocus (
48
52
request : RequestAudioFocusOptions ,
49
53
observeAudioInterruption = true
You can’t perform that action at this time.
0 commit comments