Matthias Nott
2026-03-02 a0f39302919fbacf7a0d407f01b1a50413ea6f70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
  createAudioPlayer,
  requestRecordingPermissionsAsync,
  setAudioModeAsync,
} from "expo-audio";
export interface RecordingResult {
  uri: string;
  durationMs: number;
}
let currentPlayer: ReturnType<typeof createAudioPlayer> | null = null;
export async function requestPermissions(): Promise<boolean> {
  const { status } = await requestRecordingPermissionsAsync();
  return status === "granted";
}
export async function playAudio(
  uri: string,
  onFinish?: () => void
): Promise<void> {
  try {
    await stopPlayback();
    await setAudioModeAsync({
      playsInSilentMode: true,
    });
    const player = createAudioPlayer(uri);
    currentPlayer = player;
    player.addListener("playbackStatusUpdate", (status) => {
      if (!status.playing && status.currentTime >= status.duration && status.duration > 0) {
        onFinish?.();
        player.remove();
        if (currentPlayer === player) currentPlayer = null;
      }
    });
    player.play();
  } catch (error) {
    console.error("Failed to play audio:", error);
  }
}
export async function stopPlayback(): Promise<void> {
  if (currentPlayer) {
    try {
      currentPlayer.pause();
      currentPlayer.remove();
    } catch {
      // Ignore cleanup errors
    }
    currentPlayer = null;
  }
}
export async function encodeAudioToBase64(uri: string): Promise<string> {
  const FileSystem = await import("expo-file-system");
  const result = await FileSystem.readAsStringAsync(uri, {
    encoding: FileSystem.EncodingType.Base64,
  });
  return result;
}