Can't play Firebase MP3 URL in React Native

359 Views Asked by At
  const audio = new Sound(
      'https://storage.googleapis.com/chat_bucket_aso/2ihzd4q47l73wmvcb_2.976.mp3',
      null,
      error => {
        setLoadingAudio(false);
        if (error) {
          console.log('failed to load the sound', error);
          setError(true);
          return;
        }
        setDuration(hp.recorderTimeFormat(audio.getDuration()));
        setPlayingDuration(audio.getDuration());
      },
    );

I am using React Native sound and this works in Android and get "The operation couldn’t be completed. (OSStatus error 1954115647.)" error in IOS.

2

There are 2 best solutions below

0
Jogy Felix On

If you're facing the OSStatus error -10875 while using Firebase storage with React Native Sound, it's possible that the URL you're using contains special characters that are causing the issue. One possible workaround is to modify the RNSound package by modifying code in RNSound.m file.

To do this, navigate to the RNSound package in your project's node_modules folder and open the RNSound.m file. Look for the following line of code:

fileNameUrl = [NSURL URLWithString:fileNameEscaped];

replace it with

fileNameUrl = [NSURL URLWithString:fileName];

This modification will remove the URL encoding from the file name and should allow React Native Sound to load the audio file from Firebase storage without encountering the OSStatus error.

0
Surya Shukla On

The solution that Jogy Felix gave is correct. I was so much in trouble with this for around 2 hrs and finally fixed it. All you got to do is go to RNSound.m in node_modules/react-native-sound/RNSound.m and find this code:

if ([fileNameEscaped hasPrefix:@"http"]) {
    fileNameUrl = [NSURL URLWithString:fileNameEscaped];
    NSData *data = [NSData dataWithContentsOfURL:fileNameUrl];
    player = [[AVAudioPlayer alloc] initWithData:data error:&error];
} else if ([fileNameEscaped hasPrefix:@"ipod-library://"]) {
    fileNameUrl = [NSURL URLWithString:fileNameEscaped];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileNameUrl
                                                    error:&error];
} else {
    fileNameUrl = [NSURL URLWithString:fileNameEscaped];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileNameUrl
                                                    error:&error];
}

and then replace fileNameUrl = [NSURL URLWithString:fileNameEscaped]; with fileNameUrl = [NSURL URLWithString:fileName];