Commit 1b8426ae authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Se agregaron funciones para cambiar el tiempo de reproducción de un audio

parent e857f992
Loading
Loading
Loading
Loading
+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;
@@ -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);
        }
@@ -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