| .. | .. |
|---|
| 1 | | -import { Audio, AVPlaybackStatus } from "expo-av"; |
|---|
| 1 | +import { |
|---|
| 2 | + createAudioPlayer, |
|---|
| 3 | + requestRecordingPermissionsAsync, |
|---|
| 4 | + setAudioModeAsync, |
|---|
| 5 | +} from "expo-audio"; |
|---|
| 2 | 6 | |
|---|
| 3 | 7 | export interface RecordingResult { |
|---|
| 4 | 8 | uri: string; |
|---|
| 5 | 9 | durationMs: number; |
|---|
| 6 | 10 | } |
|---|
| 7 | 11 | |
|---|
| 8 | | -let currentRecording: Audio.Recording | null = null; |
|---|
| 9 | | -let currentSound: Audio.Sound | null = null; |
|---|
| 12 | +let currentPlayer: ReturnType<typeof createAudioPlayer> | null = null; |
|---|
| 10 | 13 | |
|---|
| 11 | | -async function requestPermissions(): Promise<boolean> { |
|---|
| 12 | | - const { status } = await Audio.requestPermissionsAsync(); |
|---|
| 14 | +export async function requestPermissions(): Promise<boolean> { |
|---|
| 15 | + const { status } = await requestRecordingPermissionsAsync(); |
|---|
| 13 | 16 | return status === "granted"; |
|---|
| 14 | | -} |
|---|
| 15 | | - |
|---|
| 16 | | -export async function startRecording(): Promise<Audio.Recording | null> { |
|---|
| 17 | | - const granted = await requestPermissions(); |
|---|
| 18 | | - if (!granted) return null; |
|---|
| 19 | | - |
|---|
| 20 | | - try { |
|---|
| 21 | | - await Audio.setAudioModeAsync({ |
|---|
| 22 | | - allowsRecordingIOS: true, |
|---|
| 23 | | - playsInSilentModeIOS: true, |
|---|
| 24 | | - }); |
|---|
| 25 | | - |
|---|
| 26 | | - const { recording } = await Audio.Recording.createAsync( |
|---|
| 27 | | - Audio.RecordingOptionsPresets.HIGH_QUALITY |
|---|
| 28 | | - ); |
|---|
| 29 | | - |
|---|
| 30 | | - currentRecording = recording; |
|---|
| 31 | | - return recording; |
|---|
| 32 | | - } catch (error) { |
|---|
| 33 | | - console.error("Failed to start recording:", error); |
|---|
| 34 | | - return null; |
|---|
| 35 | | - } |
|---|
| 36 | | -} |
|---|
| 37 | | - |
|---|
| 38 | | -export async function stopRecording(): Promise<RecordingResult | null> { |
|---|
| 39 | | - if (!currentRecording) return null; |
|---|
| 40 | | - |
|---|
| 41 | | - try { |
|---|
| 42 | | - await currentRecording.stopAndUnloadAsync(); |
|---|
| 43 | | - const status = await currentRecording.getStatusAsync(); |
|---|
| 44 | | - const uri = currentRecording.getURI(); |
|---|
| 45 | | - currentRecording = null; |
|---|
| 46 | | - |
|---|
| 47 | | - await Audio.setAudioModeAsync({ |
|---|
| 48 | | - allowsRecordingIOS: false, |
|---|
| 49 | | - }); |
|---|
| 50 | | - |
|---|
| 51 | | - if (!uri) return null; |
|---|
| 52 | | - |
|---|
| 53 | | - const durationMs = (status as { durationMillis?: number }).durationMillis ?? 0; |
|---|
| 54 | | - return { uri, durationMs }; |
|---|
| 55 | | - } catch (error) { |
|---|
| 56 | | - console.error("Failed to stop recording:", error); |
|---|
| 57 | | - currentRecording = null; |
|---|
| 58 | | - return null; |
|---|
| 59 | | - } |
|---|
| 60 | 17 | } |
|---|
| 61 | 18 | |
|---|
| 62 | 19 | export async function playAudio( |
|---|
| 63 | 20 | uri: string, |
|---|
| 64 | 21 | onFinish?: () => void |
|---|
| 65 | | -): Promise<Audio.Sound | null> { |
|---|
| 22 | +): Promise<void> { |
|---|
| 66 | 23 | try { |
|---|
| 67 | 24 | await stopPlayback(); |
|---|
| 68 | 25 | |
|---|
| 69 | | - await Audio.setAudioModeAsync({ |
|---|
| 70 | | - allowsRecordingIOS: false, |
|---|
| 71 | | - playsInSilentModeIOS: true, |
|---|
| 26 | + await setAudioModeAsync({ |
|---|
| 27 | + playsInSilentMode: true, |
|---|
| 72 | 28 | }); |
|---|
| 73 | 29 | |
|---|
| 74 | | - const { sound } = await Audio.Sound.createAsync( |
|---|
| 75 | | - { uri }, |
|---|
| 76 | | - { shouldPlay: true } |
|---|
| 77 | | - ); |
|---|
| 30 | + const player = createAudioPlayer(uri); |
|---|
| 31 | + currentPlayer = player; |
|---|
| 78 | 32 | |
|---|
| 79 | | - currentSound = sound; |
|---|
| 80 | | - |
|---|
| 81 | | - sound.setOnPlaybackStatusUpdate((status: AVPlaybackStatus) => { |
|---|
| 82 | | - if (status.isLoaded && status.didJustFinish) { |
|---|
| 33 | + player.addListener("playbackStatusUpdate", (status) => { |
|---|
| 34 | + if (!status.playing && status.currentTime >= status.duration && status.duration > 0) { |
|---|
| 83 | 35 | onFinish?.(); |
|---|
| 84 | | - sound.unloadAsync().catch(() => {}); |
|---|
| 85 | | - currentSound = null; |
|---|
| 36 | + player.remove(); |
|---|
| 37 | + if (currentPlayer === player) currentPlayer = null; |
|---|
| 86 | 38 | } |
|---|
| 87 | 39 | }); |
|---|
| 88 | 40 | |
|---|
| 89 | | - return sound; |
|---|
| 41 | + player.play(); |
|---|
| 90 | 42 | } catch (error) { |
|---|
| 91 | 43 | console.error("Failed to play audio:", error); |
|---|
| 92 | | - return null; |
|---|
| 93 | 44 | } |
|---|
| 94 | 45 | } |
|---|
| 95 | 46 | |
|---|
| 96 | 47 | export async function stopPlayback(): Promise<void> { |
|---|
| 97 | | - if (currentSound) { |
|---|
| 48 | + if (currentPlayer) { |
|---|
| 98 | 49 | try { |
|---|
| 99 | | - await currentSound.stopAsync(); |
|---|
| 100 | | - await currentSound.unloadAsync(); |
|---|
| 50 | + currentPlayer.pause(); |
|---|
| 51 | + currentPlayer.remove(); |
|---|
| 101 | 52 | } catch { |
|---|
| 102 | | - // Ignore errors during cleanup |
|---|
| 53 | + // Ignore cleanup errors |
|---|
| 103 | 54 | } |
|---|
| 104 | | - currentSound = null; |
|---|
| 55 | + currentPlayer = null; |
|---|
| 105 | 56 | } |
|---|
| 106 | 57 | } |
|---|
| 107 | 58 | |
|---|
| 108 | | -export function encodeAudioToBase64(uri: string): Promise<string> { |
|---|
| 109 | | - // In React Native, we'd use FileSystem from expo-file-system |
|---|
| 110 | | - // For now, return the URI as-is since we may not have expo-file-system |
|---|
| 111 | | - return Promise.resolve(uri); |
|---|
| 59 | +export async function encodeAudioToBase64(uri: string): Promise<string> { |
|---|
| 60 | + const FileSystem = await import("expo-file-system"); |
|---|
| 61 | + const result = await FileSystem.readAsStringAsync(uri, { |
|---|
| 62 | + encoding: FileSystem.EncodingType.Base64, |
|---|
| 63 | + }); |
|---|
| 64 | + return result; |
|---|
| 112 | 65 | } |
|---|