import { useAudioPlayer, useAudioPlayerStatus } from "expo-audio"; import { useState } from "react"; import { Pressable, View } from "react-native"; import { AppText as Text } from "./AppText"; function timestamp(seconds: number) { const value = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0; return `${Math.floor(value / 60)}:${String(value % 60).padStart(2, "0")}`; } export function AudioFilePreview(props: { uri: string; onRetry: () => void }) { const player = useAudioPlayer({ uri: props.uri }, { updateInterval: 500 }); const status = useAudioPlayerStatus(player); const [seekError, setSeekError] = useState(false); const seek = (seconds: number, play = false) => { setSeekError(false); void player .seekTo(seconds) .then(() => { if (play) player.play(); }) .catch(() => setSeekError(true)); }; return ( {timestamp(status.currentTime)} / {timestamp(status.duration)} seek(Math.max(0, status.currentTime - 15))} className="p-4" > −15s { if (status.playing) player.pause(); else if (status.didJustFinish || status.currentTime >= status.duration) seek(0, true); else player.play(); }} className="rounded-xl bg-subtle px-6 py-4" > {!status.isLoaded ? "Loading…" : status.playing ? "Pause" : "Play"} seek(Math.min(status.duration, status.currentTime + 15))} className="p-4" > +15s {status.error || seekError ? ( This audio could not be played. Try again or save it to open in another app. Try again ) : null} ); }