Skip to content

Commit e6252cd

Browse files
committed
fix: request record permission on 14+ android versions
1 parent c75e9eb commit e6252cd

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

app/app/hooks/useAudioStreamer.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// useAudioStreamer.ts
22
import { useState, useRef, useCallback, useEffect } from 'react';
3-
import { PermissionsAndroid, Platform } from 'react-native';
3+
import { Alert, PermissionsAndroid, Platform } from 'react-native';
44
import notifee, { AndroidImportance } from '@notifee/react-native';
55
import NetInfo from '@react-native-community/netinfo';
66

@@ -53,10 +53,44 @@ async function ensureNotificationPermission() {
5353
}
5454
}
5555

56+
// On Android 14+ (API 34+), FOREGROUND_SERVICE_MICROPHONE requires RECORD_AUDIO
57+
// FOREGROUND_SERVICE_MICROPHONE is automatically granted when RECORD_AUDIO is granted
58+
// We must ensure RECORD_AUDIO is granted BEFORE starting the foreground service
59+
async function ensureForegroundServiceMicPermission() {
60+
if (Platform.OS !== 'android' || Platform.Version < 34) {
61+
return true;
62+
}
63+
try {
64+
const audioGranted = await PermissionsAndroid.check(
65+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
66+
);
67+
68+
if (!audioGranted) {
69+
console.log('[AudioStreamer] Requesting RECORD_AUDIO permission...');
70+
const audioResult = await PermissionsAndroid.request(
71+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
72+
);
73+
if (audioResult !== PermissionsAndroid.RESULTS.GRANTED) {
74+
Alert.alert('RECORD_AUDIO permission is required for microphone foreground service. Please grant microphone permission in app settings.');
75+
return false;
76+
}
77+
}
78+
79+
console.log('[AudioStreamer] RECORD_AUDIO permission granted. FOREGROUND_SERVICE_MICROPHONE should be automatically available.');
80+
return true;
81+
} catch (error) {
82+
console.error('[AudioStreamer] Error ensuring foreground service mic permission:', error);
83+
return false;
84+
}
85+
}
86+
5687
async function startForegroundServiceNotification(title: string, body: string) {
5788
ensureFgsRegistered();
5889
await ensureNotificationPermission();
59-
90+
const success = await ensureForegroundServiceMicPermission();
91+
if (!success) {
92+
return;
93+
}
6094
// Create channel if needed
6195
await notifee.createChannel({
6296
id: FGS_CHANNEL_ID,

0 commit comments

Comments
 (0)