The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Button } from "../../components/ui/Button"; |
| 2 | import { useAudioPlayerStatus } from "expo-audio"; |
| 3 | import { T, useGT } from "gt-react-native"; |
| 4 | import { useEffect, useState } from "react"; |
| 5 | import { AppState, View } from "react-native"; |
| 6 | import { AppText as Text } from "../../components/ui/AppText"; |
| 7 | import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../../lib/videoAudioSession"; |
| 8 | import { createAudioPlaybackSession } from "../../lib/audioSession"; |
| 9 | import { usePlaybackAudioPlayer } from "../voice-input/usePlaybackAudioPlayer"; |
| 10 | |
| 11 | function timestamp(seconds: number) { |
| 12 | const value = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0; |
| 13 | return `${Math.floor(value / 60)}:${String(value % 60).padStart(2, "0")}`; |
| 14 | } |
| 15 | |
| 16 | export function AudioFilePreview(props: { uri: string; onRetry: () => void }) { |
| 17 | const player = usePlaybackAudioPlayer(props.uri); |
| 18 | const gt = useGT(); |
| 19 | const status = useAudioPlayerStatus(player); |
| 20 | const [playbackRequested, setPlaybackRequested] = useState(false); |
| 21 | const playback = createAudioPlaybackSession(player, false, setPlaybackRequested); |
| 22 | const [seekError, setSeekError] = useState(false); |
| 23 | // Video teardown deactivates the shared iOS session, which would stop this player. |
| 24 | useMediaAudioSessionHold(status.playing); |
| 25 | const fullscreenVideoOpen = useFullscreenVideoOpen(); |
| 26 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the shared audio session in step with the native player status and fullscreen video |
| 27 | useEffect(() => { |
| 28 | if (fullscreenVideoOpen || status.didJustFinish || status.error) playback.pause(); |
| 29 | else playback.playingChanged(status.playing); |
| 30 | }, [fullscreenVideoOpen, status.didJustFinish, status.error, status.playing, playback]); |
| 31 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to AppState to pause playback in the background, and pauses on cleanup |
| 32 | useEffect(() => { |
| 33 | const subscription = AppState.addEventListener("change", (state) => { |
| 34 | if (state !== "active") playback.pause(); |
| 35 | }); |
| 36 | return () => { |
| 37 | subscription.remove(); |
| 38 | playback.pause(); |
| 39 | }; |
| 40 | }, [playback]); |
| 41 | const seek = (seconds: number) => { |
| 42 | setSeekError(false); |
| 43 | void player.seekTo(seconds).catch(() => setSeekError(true)); |
| 44 | }; |
| 45 | return ( |
| 46 | <View className="flex-1 items-center justify-center gap-5 p-6"> |
| 47 | <Text className="text-foreground-muted"> |
| 48 | {timestamp(status.currentTime)} / {timestamp(status.duration)} |
| 49 | </Text> |
| 50 | <View className="flex-row items-center gap-4"> |
| 51 | <Button |
| 52 | label="−15s" |
| 53 | tone="text" |
| 54 | accessibilityLabel={gt("Back 15 seconds")} |
| 55 | disabled={!status.isLoaded} |
| 56 | onPress={() => seek(Math.max(0, status.currentTime - 15))} |
| 57 | /> |
| 58 | <Button |
| 59 | label={ |
| 60 | playbackRequested || status.playing |
| 61 | ? gt("Pause") |
| 62 | : !status.isLoaded |
| 63 | ? gt("Loading…") |
| 64 | : gt("Play") |
| 65 | } |
| 66 | tone="secondary" |
| 67 | disabled={!status.isLoaded && !playbackRequested} |
| 68 | onPress={() => { |
| 69 | if (status.playing || playback.requested) playback.pause(); |
| 70 | else { |
| 71 | setSeekError(false); |
| 72 | void playback |
| 73 | .play( |
| 74 | status.didJustFinish || status.currentTime >= status.duration |
| 75 | ? () => player.seekTo(0) |
| 76 | : undefined, |
| 77 | ) |
| 78 | .catch(() => setSeekError(true)); |
| 79 | } |
| 80 | }} |
| 81 | /> |
| 82 | <Button |
| 83 | label="+15s" |
| 84 | tone="text" |
| 85 | accessibilityLabel={gt("Forward 15 seconds")} |
| 86 | disabled={!status.isLoaded} |
| 87 | onPress={() => seek(Math.min(status.duration, status.currentTime + 15))} |
| 88 | /> |
| 89 | </View> |
| 90 | {status.error || seekError ? ( |
| 91 | <View className="items-center gap-3"> |
| 92 | <Text className="text-center text-foreground"> |
| 93 | <T>This audio could not be played. Try again or save it to open in another app.</T> |
| 94 | </Text> |
| 95 | <View className="items-center"> |
| 96 | <Button label={gt("Try again")} tone="secondary" onPress={props.onRetry} /> |
| 97 | </View> |
| 98 | </View> |
| 99 | ) : null} |
| 100 | </View> |
| 101 | ); |
| 102 | } |