fix(up-wip): recording permissions
This commit is contained in:
parent
4dcf71379e
commit
80f6d1d20d
1 changed files with 51 additions and 38 deletions
|
@ -101,29 +101,28 @@ const withTimeout = (promise, ms = 10000) =>
|
||||||
|
|
||||||
const ensureMicPermission = async () => {
|
const ensureMicPermission = async () => {
|
||||||
if (Platform.OS !== "android") {
|
if (Platform.OS !== "android") {
|
||||||
return true;
|
return { granted: true, status: RESULTS.UNAVAILABLE };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const status = await check(PERMISSIONS.ANDROID.RECORD_AUDIO);
|
const status = await check(PERMISSIONS.ANDROID.RECORD_AUDIO);
|
||||||
if (status === RESULTS.GRANTED) return true;
|
switch (status) {
|
||||||
if (status === RESULTS.BLOCKED) {
|
case RESULTS.GRANTED:
|
||||||
try {
|
return { granted: true, status };
|
||||||
Alert.alert(
|
case RESULTS.DENIED: {
|
||||||
"Autorisation micro bloquée",
|
const r = await request(PERMISSIONS.ANDROID.RECORD_AUDIO);
|
||||||
"Veuillez autoriser le micro dans les paramètres de l'application.",
|
return { granted: r === RESULTS.GRANTED, status: r };
|
||||||
[
|
}
|
||||||
{ text: "Annuler", style: "cancel" },
|
case RESULTS.BLOCKED:
|
||||||
{ text: "Ouvrir les paramètres", onPress: openSettings },
|
return { granted: false, status };
|
||||||
],
|
// NOTE: RESULTS.LIMITED is not applicable to RECORD_AUDIO; treat as not granted.
|
||||||
);
|
case RESULTS.UNAVAILABLE:
|
||||||
} catch (_) {}
|
case RESULTS.LIMITED:
|
||||||
return false;
|
default:
|
||||||
|
return { granted: false, status };
|
||||||
}
|
}
|
||||||
const res = await request(PERMISSIONS.ANDROID.RECORD_AUDIO);
|
|
||||||
return res === RESULTS.GRANTED;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Mic permission check failed", e);
|
console.log("Mic permission check failed", e);
|
||||||
return false;
|
return { granted: false, status: undefined };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,6 +151,7 @@ export default React.memo(function ChatInput({
|
||||||
const [isRecording, setIsRecording] = useState(false);
|
const [isRecording, setIsRecording] = useState(false);
|
||||||
const recorder = useAudioRecorder(recordingOptionsSpeech);
|
const recorder = useAudioRecorder(recordingOptionsSpeech);
|
||||||
const [player, setPlayer] = useState(null);
|
const [player, setPlayer] = useState(null);
|
||||||
|
const requestingMicRef = useRef(false);
|
||||||
|
|
||||||
const insertMessage = useInsertMessage(alertId);
|
const insertMessage = useInsertMessage(alertId);
|
||||||
|
|
||||||
|
@ -198,30 +198,41 @@ export default React.memo(function ChatInput({
|
||||||
}, [insertMessage, text, setText, userId, username]);
|
}, [insertMessage, text, setText, userId, username]);
|
||||||
|
|
||||||
const startRecording = useCallback(async () => {
|
const startRecording = useCallback(async () => {
|
||||||
|
if (requestingMicRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
requestingMicRef.current = true;
|
||||||
try {
|
try {
|
||||||
console.log("Requesting microphone permission..");
|
console.log("Requesting microphone permission..");
|
||||||
const grantedPre = await ensureMicPermission();
|
if (Platform.OS === "android") {
|
||||||
if (!grantedPre) {
|
const { granted, status } = await ensureMicPermission();
|
||||||
console.log("Microphone permission not granted or blocked");
|
if (!granted) {
|
||||||
return;
|
if (status === RESULTS.BLOCKED) {
|
||||||
}
|
try {
|
||||||
try {
|
Alert.alert(
|
||||||
await withTimeout(requestRecordingPermissionsAsync(), 10000);
|
"Autorisation micro bloquée",
|
||||||
} catch (permErr) {
|
"Veuillez autoriser le micro dans les paramètres de l'application.",
|
||||||
console.log("Microphone permission request failed/timed out:", permErr);
|
[
|
||||||
if (Platform.OS === "android") {
|
{ text: "Annuler", style: "cancel" },
|
||||||
try {
|
{ text: "Ouvrir les paramètres", onPress: openSettings },
|
||||||
Alert.alert(
|
],
|
||||||
"Autorisation micro requise",
|
);
|
||||||
"Impossible d'obtenir l'autorisation du microphone. Ouvrir les paramètres pour l'accorder.",
|
} catch (_) {}
|
||||||
[
|
} else {
|
||||||
{ text: "Annuler", style: "cancel" },
|
console.log("Microphone permission not granted", status);
|
||||||
{ text: "Ouvrir les paramètres", onPress: openSettings },
|
}
|
||||||
],
|
return;
|
||||||
);
|
}
|
||||||
} catch (_) {}
|
} else {
|
||||||
|
try {
|
||||||
|
await withTimeout(requestRecordingPermissionsAsync(), 10000);
|
||||||
|
} catch (permErr) {
|
||||||
|
console.log(
|
||||||
|
"Microphone permission request failed/timed out:",
|
||||||
|
permErr,
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
await setAudioModeAsync({
|
await setAudioModeAsync({
|
||||||
allowsRecording: true,
|
allowsRecording: true,
|
||||||
|
@ -266,6 +277,8 @@ export default React.memo(function ChatInput({
|
||||||
console.log("Recording started");
|
console.log("Recording started");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Failed to start recording", err);
|
console.log("Failed to start recording", err);
|
||||||
|
} finally {
|
||||||
|
requestingMicRef.current = false;
|
||||||
}
|
}
|
||||||
}, [player, recorder]);
|
}, [player, recorder]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue