zmxv / react-native-sound

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

Unable to play Playback sound in IOS in some case #624

Open Linoa65 opened 4 years ago

Linoa65 commented 4 years ago

:beetle: Description

Hi !

I have an issue with playing background sound on IOS. After hard testing, I think we are unable to play sound in background only if we have not already played a sound before. For my case, I receive VoIP notification, then I want to play a sound in Playback mode and no sound is played. My functions are called correctly with no error, but there is just no sound.

If just before my notification I open my app, play a sound, then send it to background, I'm able to play a sound in background.

:beetle: What is the observed behavior?

No sound played in background on IOS, in Playback mode, if it is "first" sound played since some minutes after sending app to background.

:beetle: What is the expected behavior?

Sound get play in all cases when app is in background.

:beetle: Please post your code:

Call of method :

soundService.playSound(this.sound, null, "Playback", "VoiceChat", 1);

Play sound method :

    /**
     * Play a sound from URL.
     * @param path: the path URL.
     * @param onEnd: called on the end of sound.
     * @param category: audio category.
     * @param mode: audio mode.
     * @param volume: the volume to set.
     */
    playSound(path: string, onEnd: Function, category: string, mode: string, volume: number) {
        if (this.sound) {
            this.stopSound();
        }

        // Set category as Playback by default
        if (category) {
            console.log(category);
            Sound.setCategory(category);
        } else {
            console.log("Playback");
            Sound.setCategory("Playback");
        }

        if (mode) {
            Sound.setMode(mode);
        } else {
            Sound.setMode("Default");
        }

        this.sound = new Sound(path, null, (err) => {
            // Display error if needed
            if (err) {
                crashService.recordError("SoundService", "Error playing sound.", err);
            }

            // Set volume if precised
            if (volume !== null && volume !== undefined) {
                this.sound.setVolume(volume);
            }

            // Then play the sound
            this.sound.play(() => {
                this.sound.release();

                // If callback passed, called at the end of playing
                if (onEnd) {
                    onEnd();
                }
            });
        });
    }

:bulb: Does the problem have a test case?

:bulb: **Possible solution**

:bulb: Is there a workaround?

:bulb: If the bug is confirmed, would you be willing to create a pull request?

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?

Paduado commented 4 years ago

I think I'm having a similar issue, I'm not able to play any sounds in some devices, but a soon as a play a video, all the sounds start playing as expected.

ucheNkadiCode commented 2 years ago

Check out the solution in https://github.com/zmxv/react-native-sound/issues/788, I layed out the steps of how I solved playing background and foreground audio over music streaming with the OP's help

It would also be good to set your app capability to have background sound Don't forget to actively set your Sound Module category to Playback and make sure Setactive(true) is called Just once after you set your category.

Having a setup method you call once would be helpful