Skip to content

Sound not playing on the older iOS versions#889

Description

@KumaraSwamyR94

馃 Description
When running the app (compiled with XCode 26), sound files do not play on devices using an iOS version older than 26 (tested on iPhoneX - 16.7.10). Playback works correctly on iOS 26+ devices and on all tested Android devices.

馃 What have you tried?
I've already tried to setting the category to Playback, but didn't work.
Sound.setCategory('Playback', true);

馃 Please post your code:

import Sound from 'react-native-sound';

export enum SoundType {
    ERROR = 'error.mp3',
    LOGIN_SUCCESS = 'login_success.mp3',
    LEAVING_SUCCESS = 'leaving_success.mp3',
    LATE_SUCCESS = 'late_success.mp3',
    RETURNING_SUCCESS = 'returning_success.mp3',
}

// Global cache to prevent reloading sounds on every hook mount
const sounds = new Map<string, Sound>();

// Enable playback in silent mode
Sound.setCategory('Playback', true);

// Preload function
const preloadSounds = () => {
    // Preload all sounds
    Object.values(SoundType).forEach(fileName => {
        if (!sounds.has(fileName)) {
            const sound = new Sound(fileName, Sound.MAIN_BUNDLE, error => {
                if (error) {
                    console.error(`Failed to load sound: ${fileName}`, error);
                    return;
                }
                sound.setVolume(1);
                sounds.set(fileName, sound);
            });
        }
    });
};

export const useSound = () => {
    const play = (soundType: SoundType) => {
        const sound = sounds.get(soundType);
        if (sound && sound.isLoaded()) {
            sound.play((success) => {
                if (!success) {
                    sound.reset();
                }
            });
        } else {
            // Fallback: try loading if missing
            const newSound = new Sound(soundType, Sound.MAIN_BUNDLE, error => {
                if (error) {
                    console.error(`Failed to play sound: ${soundType}`, error);
                    return;
                }
                newSound.setVolume(1);
                sounds.set(soundType, newSound);
                newSound.setCurrentTime(0);
                newSound.play();
            });
        }
    };

    const playError = () => {
        play(SoundType.ERROR);
    };

    const playSuccess = (type: 'login' | 'leaving' | 'late' | 'returning') => {
        switch (type) {
            case 'login':
                play(SoundType.LOGIN_SUCCESS);
                break;
            case 'leaving':
                play(SoundType.LEAVING_SUCCESS);
                break;
            case 'late':
                play(SoundType.LATE_SUCCESS);
                break;
            case 'returning':
                play(SoundType.RETURNING_SUCCESS);
                break;
        }
    };

    // Cleanup all the sounds on unmount
    React.useEffect(() => {

        // Initial preload
        preloadSounds();

        return () => {
            sounds.forEach(sound => sound.release());
            sounds.clear();
        };
    }, []);

    return {
        play,
        playError,
        playSuccess,
    };
};

馃挕 Possible solution

Is your issue with...

  • iOS
  • Android
  • Windows

Are you using...

  • React Native CLI (e.g. react-native run-android)
  • Expo
  • Other: (please specify)

Which versions are you using?

  • React Native Sound: "^0.13.0"
  • React Native: "0.82.0"
  • iOS: 16.7.10
  • Android:
  • Windows:

Does the problem occur on...

  • Simulator
  • Device

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

  • Device: iPhone XS, iPad 5th Gen

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions