zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.78k stars 748 forks source link

How to know when the file is fully downloaded? #752

Open vahidrn98 opened 2 years ago

vahidrn98 commented 2 years ago

:beetle: Description I'm trying to call seek() after the sound is fully loaded from a remote file in a url. But it seems like the audio is played before the download is finished. so sometimes after seeking to a point the sudio stops and gets stuck like that forever.

:beetle: What have you tried?

I tried using both the callback and isLoaded(). They are both the same

:beetle: Please post your code:

Sound.setCategory('Playback');

this.whoosh = new Sound('https://' + url, null, (error) => {
    this.setState({ track: this.whoosh });

    if (error) {
        console.log(url)
        console.log('failed to load the sound', error);
        return;
    }
    // loaded successfully
    console.log('duration in seconds: ' + this.whoosh.getDuration() + 'number of channels: ' + this.whoosh.getNumberOfChannels());

    // Play the sound with an onEnd callback
    if (self.state.allowPlay && this.whoosh) {
        this.whoosh.play((success) => {
            if (success) {

                console.log('successfully finished playing');
            } else {
                console.log('playback failed due to audio decoding errors');
            }
        });
    }

});

seek = (time) => {

    this.whoosh.setCurrentTime(time);
}

:bulb: Possible solution

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?