React-native-sound: Sound can not stop

Created on 10 Apr 2018  路  6Comments  路  Source: zmxv/react-native-sound

when I call stop function, it try to replay sound again until finish

import Sound from 'react-native-sound'

class Ringtone {
    constructor(filename) {
        // See notes below about preloading sounds within initialization code below.
        this.whoosh = new Sound(filename, Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error)
                return
            }

            // Loop indefinitely until stop() is called
            this.whoosh.setNumberOfLoops(-1);

            console.log('duration in seconds: ' + this.whoosh.getDuration() + 'number of channels: ' + this.whoosh.getNumberOfChannels());
        });
    }

    stop() {
        if(this.whoosh) {
            this.whoosh.stop()
            this.whoosh.setCurrentTime(0.0)
        }
    }

    play() {
        // Play the sound with an onEnd callback
        this.whoosh.play((success) => {
            if (success) {
                console.log('successfully finished playing');
            } else {
                console.log('playback failed due to audio decoding errors');
            }
        });
    }
}

export const ringtone = new Ringtone('incallmanager_ringtone.mp3')
export const ringback = new Ringtone('incallmanager_ringback.mp3')

Most helpful comment

@EskelCz
I used setVolume(0.0) instead

stop() {
        if(this.whoosh) {
            // Get the current playback point in seconds
            this.whoosh.getCurrentTime((seconds) => {
                console.log(this.filename)
                console.log('at ' + seconds)
            })

            this.whoosh.stop()
            this.whoosh.setVolume(0.0)
        }
    }

All 6 comments

Duplicated

@smnodame have you solved it? I'm having the same issue.

@EskelCz
I used setVolume(0.0) instead

stop() {
        if(this.whoosh) {
            // Get the current playback point in seconds
            this.whoosh.getCurrentTime((seconds) => {
                console.log(this.filename)
                console.log('at ' + seconds)
            })

            this.whoosh.stop()
            this.whoosh.setVolume(0.0)
        }
    }

Duplicated

image

@smnodame I'm having the same issue and this.whoosh.stop() this.whoosh.setVolume(0.0) also not worked

I don't think there's a stopAll() function. You need to keep track of the instances you create. I use a single global variable. With some logic I can make sure that only one sound is being played at a time.

When you create a new sound object, assign it to a global variable.
Before you create a new sound object, stop the sound on global instance
Add a null check on the global variable for the first click

var whoosh = new Sound(/storage/sdcard0/Android/data/com.myapp/files/Download/${name}.mp3, '', (error) => {
if (error) {
alert('failed to load the sound', error);
return;
}
if (global.sound) global.sound.stop();
global.sound = whoosh;
whoosh.play( success => { console.log('Playing') } );
}
People think global variables bad in JS. If you misuse yes. But this case it really is a global object and there's nothing wrong using it. It's not a misuse.

https://stackoverflow.com/questions/49620303/react-native-sound-stop-all-sounds

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viperfx picture viperfx  路  4Comments

VladimirJarabica picture VladimirJarabica  路  3Comments

andrekovac picture andrekovac  路  5Comments

jacargentina picture jacargentina  路  4Comments

poc7667 picture poc7667  路  4Comments