react-native-sound-player icon indicating copy to clipboard operation
react-native-sound-player copied to clipboard

Add new event listener which returns the current duration as the audio progresses.

Open khushal87 opened this issue 2 years ago • 3 comments

Describe the bug

Hey @johnsonsu

This issue isn't a bug but a feature request where an event can be added if possible in which the callback can return the values of the current duration of the video. This will improve the package usage significantly as currently, you need to add a setInterval logic to update the audio duration in the slider.

To Reproduce

Expected behavior A clear and concise description of what you expected to happen.

Platform (please complete the following information):

  • iOS
  • Android

Additional context Add any other context about the problem here.

khushal87 avatar May 19 '22 10:05 khushal87

Can you show your logic with setInterval?

brunoleitem avatar Jun 29 '22 18:06 brunoleitem

I want to how to do that! Please share your code.. I need the way how to make progress bar...

Yeonbin avatar Nov 21 '22 00:11 Yeonbin

Here is how I did it:

    useEffect(() => {
        SoundPlayer.loadUrl(attributes.src);

        const getDuration = async () => {
            const info = await SoundPlayer.getInfo();

            setDuration(info.duration);
        };

        if (Platform.OS === 'ios') {
            getDuration();
        }

        const loadUrlEvent = SoundPlayer.addEventListener('FinishedLoadingURL', ({success}) => {
            if (success && Platform.OS !== 'ios') {
                getDuration();
            }
        });

        const timeInterval = setInterval(async () => {
            const info = await SoundPlayer.getInfo();

            setTime(info.currentTime);
        }, 1000);

        return () => {
            loadUrlEvent.remove();
            clearInterval(timeInterval);
        };
    }, [attributes.src]);

Jobjeuh avatar Oct 12 '23 08:10 Jobjeuh