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

Unable to play Playback sound in IOS in some case

Open Linoa65 opened this issue 6 years ago • 2 comments

: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...

  • [x] iOS
  • [ ] Android
  • [ ] Windows

Are you using...

  • [x] React Native CLI
  • [ ] Expo
  • [ ] Other: (please specify)

Which versions are you using?

  • React Native Sound: 0.11.0
  • React Native: 0.60.5
  • iOS: 12.4.1

Does the problem occur on...

  • [ ] Simulator
  • [x] Device

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

  • Device: iPhone 7

Linoa65 avatar Oct 18 '19 14:10 Linoa65