|
7 | 7 | type RecordingStatus, |
8 | 8 | } from "expo-audio"; |
9 | 9 | import { File } from "expo-file-system"; |
| 10 | +import { activateKeepAwakeAsync, deactivateKeepAwake } from "expo-keep-awake"; |
10 | 11 | import { useFocusEffect } from "@react-navigation/native"; |
11 | | -import { useCallback, useEffect, useRef, useState } from "react"; |
| 12 | +import { useCallback, useEffect, useId, useRef, useState } from "react"; |
12 | 13 | import { AppState } from "react-native"; |
13 | 14 | import { useSharedValue } from "react-native-reanimated"; |
14 | 15 |
|
@@ -71,6 +72,8 @@ export function useVoiceInputController(input: { |
71 | 72 | }) { |
72 | 73 | const [state, setState] = useState<VoiceInputState>(INITIAL_STATE); |
73 | 74 | const [elapsedSeconds, setElapsedSeconds] = useState(0); |
| 75 | + const keepAwakeId = useId(); |
| 76 | + const keepAwakeSessionRef = useRef(0); |
74 | 77 | const elapsedSecondsRef = useRef(0); |
75 | 78 | const audioLevelsRef = useRef(Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0)); |
76 | 79 | const audioLevels = useSharedValue(audioLevelsRef.current); |
@@ -156,6 +159,18 @@ export function useVoiceInputController(input: { |
156 | 159 |
|
157 | 160 | useEffect(() => () => controller.dispose(), [controller]); |
158 | 161 |
|
| 162 | + useEffect(() => { |
| 163 | + if (state.phase !== "recording") return; |
| 164 | + |
| 165 | + const tag = `voice-input:${keepAwakeId}:${++keepAwakeSessionRef.current}`; |
| 166 | + const activation = activateKeepAwakeAsync(tag); |
| 167 | + void activation.catch(() => {}); |
| 168 | + return () => { |
| 169 | + // Release after activation settles, even if the recording ends immediately. |
| 170 | + void activation.then(() => deactivateKeepAwake(tag)).catch(() => {}); |
| 171 | + }; |
| 172 | + }, [keepAwakeId, state.phase]); |
| 173 | + |
159 | 174 | useEffect(() => { |
160 | 175 | if (state.phase !== "preparing" && state.phase !== "recording") return; |
161 | 176 |
|
|
0 commit comments