Loading mobile/src/hooks/useAudio.ts +25 −7 Original line number Diff line number Diff line import { AVPlaybackSource, AVPlaybackStatus, Audio } from "expo-av"; import { useEffect, useState } from "react"; import { useFocusEffect } from "expo-router"; import { useCallback, useEffect, useState } from "react"; import { set } from 'react-hook-form'; interface AudioPlayerProps { source: AVPlaybackSource; Loading @@ -8,12 +10,27 @@ interface AudioPlayerProps { export const useAudio = ({ source }: AudioPlayerProps) => { const [sound, setSound] = useState<Audio.Sound | null>(null); const [isPlaying, setIsPlaying] = useState(false); const [duration, setDuration] = useState(0); const [position, setPosition] = useState(0); const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => { status.isLoaded && setPosition(status.positionMillis); } const onValueChange = (value: number) => { if (sound) { sound.setPositionAsync(value); } } const loadAudio = async () => { const { sound, status } = await Audio.Sound.createAsync(source, { shouldPlay: false }); if (status.isLoaded) { sound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate); setDuration(status.durationMillis || 0); setSound(sound); setIsPlaying(status.isPlaying); } Loading @@ -30,15 +47,16 @@ export const useAudio = ({ source }: AudioPlayerProps) => { } } const onUnmount = useCallback(() => { sound?.unloadAsync(); }, [sound]); useEffect(() => { loadAudio(); return () => { if (sound) { sound.pauseAsync(); sound.unloadAsync(); } onUnmount(); } }, [source]); }, []); return { togglePlay, isPlaying }; return { togglePlay, isPlaying, duration, position, onValueChange, onUnmount }; } No newline at end of file Loading
mobile/src/hooks/useAudio.ts +25 −7 Original line number Diff line number Diff line import { AVPlaybackSource, AVPlaybackStatus, Audio } from "expo-av"; import { useEffect, useState } from "react"; import { useFocusEffect } from "expo-router"; import { useCallback, useEffect, useState } from "react"; import { set } from 'react-hook-form'; interface AudioPlayerProps { source: AVPlaybackSource; Loading @@ -8,12 +10,27 @@ interface AudioPlayerProps { export const useAudio = ({ source }: AudioPlayerProps) => { const [sound, setSound] = useState<Audio.Sound | null>(null); const [isPlaying, setIsPlaying] = useState(false); const [duration, setDuration] = useState(0); const [position, setPosition] = useState(0); const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => { status.isLoaded && setPosition(status.positionMillis); } const onValueChange = (value: number) => { if (sound) { sound.setPositionAsync(value); } } const loadAudio = async () => { const { sound, status } = await Audio.Sound.createAsync(source, { shouldPlay: false }); if (status.isLoaded) { sound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate); setDuration(status.durationMillis || 0); setSound(sound); setIsPlaying(status.isPlaying); } Loading @@ -30,15 +47,16 @@ export const useAudio = ({ source }: AudioPlayerProps) => { } } const onUnmount = useCallback(() => { sound?.unloadAsync(); }, [sound]); useEffect(() => { loadAudio(); return () => { if (sound) { sound.pauseAsync(); sound.unloadAsync(); } onUnmount(); } }, [source]); }, []); return { togglePlay, isPlaying }; return { togglePlay, isPlaying, duration, position, onValueChange, onUnmount }; } No newline at end of file