zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.78k stars 749 forks source link

Sound sometimes playing on Android, sometimes not #646

Open AdamGerthel opened 4 years ago

AdamGerthel commented 4 years ago

:beetle: Description

Playing sounds work sometimes and sometimes not. Only affects Android. Same exact code works flawlessly on iOS.

:beetle: What is the observed behavior?

Playing sounds work sometimes and sometimes not. new Sound callback returns no errors, and play() callback returns true. But still, the sounds are only played sometimes. There seems to be no correlation between which file succeeds to play and which doesn't.

:beetle: What is the expected behavior?

That it works as intended, i.e. that I can trust the documentation of this package.

:beetle: Please post your code:

// Below is a simplified example of what I'm doing.

const audioFiles = {
  example: require('./audio/example.mp3')
}

class Track {
  constructor(id) {
    this._id = id
    this._sound = null
    this.isLoaded = false
  }

  load = () => new Promise((resolve, reject) => {
    this._sound = new Sound(audioFiles[this._id], (error) => {
      if (error) {
        reject(error)
      } else {
        this.isLoaded = true
        resolve()
      }
    })
  })

  play = () => new Promise(async (resolve, reject) => {
    if (this.isLoaded === false) {
      try {
        await this.load()
      } catch (error) {
        reject(error)
      }
    }

    this._sound.play((success) => {
      if (!success) {
        reject('Play failed')
      } else {
        resolve()
      }
    })
  })
}

const track = new Track('example')

try {
  track.play()
} catch (error) {
  console.log(error)
}

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

CoSNaYe commented 4 years ago

+1 here. I found it seems like the player's state is abnormal. For example, if I call the play() quite fast before the previous is finished. Then there's a high chance that it has the problem. And after the problem emerges, all following attempts to play the audio fail. I need to restart the app.

BTW, I found the issue can be significantly solved (but not 100%) by calling .release() at appropriate timing. I guess there's race condition causing an abnormal state of Android Media Player. But I don't know the root cause yet.

I'll deeply appreciate If someone who is familiar with the project can help to check the source codes relates to the release().

SurajMDurgad commented 4 years ago

I'm facing the same issue. Some sounds randomly stop playing. After one stops, consecutive all sounds stop playing.

julienjourde commented 4 years ago

Hello, Any updates on this issue?

lelumees commented 3 years ago

Same here, happening on Android only and on Lenovo tabM10. Feels like a race condition problem since it is hard to reproduce, but happens often enough to be unreliable in production.

First idea to try out - the docs specify that it is wise to replay the sound in the sound.stop() callback, e.g:

// Stop the sound and rewind to the beginning
whoosh.stop(() => {
  // Note: If you want to play a sound after stopping and rewinding it,
  // it is important to call play() in a callback.
  whoosh.play();
});

Will try it out soon, but even so it feels like the library is getting outdated, with the latest release happening a year ago and the maintainers claiming it to be in alpha/beta stage. So I guess it is time to fork and customise or find a new library. πŸ™ˆ

@AdamGerthel did you manage to figure out a fix for this? πŸ™

AdamGerthel commented 3 years ago

@rasmuslelumees I ended up using Expo-AV instead (can be used in the "bare" workflow if you have Expo unimodules installed). It hasn't been without issues either but it's been more stable than the other packages.

Here's my implementation: https://github.com/expo/expo/issues/1873#issuecomment-592214795

lelumees commented 3 years ago

Thanks @AdamGerthel, will take a look πŸ’―

nguyenvanphuc2203 commented 2 years ago

+1 same issue

anjandaffo commented 2 years ago

+1 same issue

anjandaffo commented 2 years ago

Any one have solution for this? I am facing same issue and our app is live so it impacts to user base.

nguyenvanphuc2203 commented 1 year ago

@anjandaffo try to release() sound when component didmout

anjandaffo commented 1 year ago

@nguyenvanphuc2203 in component did mount we don't have audio object to release it. There is no method to release all. In library i checked it is creating pool of media player. I am releasing the audio from inside the callback of play method and unmount of components.

nguyenvanphuc2203 commented 1 year ago

this._sound.release() @anjandaffo , such as sample in this issue

anjandaffo commented 1 year ago

@nguyenvanphuc2203 in component mount this._sound is null. Did you mean component unmount? In component unmount I am calling the release function

nguyenvanphuc2203 commented 1 year ago

@anjandaffo oh yes, in componentUnmount you need release sound, sorry i forget

anjandaffo commented 1 year ago

@nguyenvanphuc2203 we are doing the same. On component unmount and play completion callback we are calling function of release. After the audio not play in callback of load getting success true and and play method instant giving callback

jens-tucholski commented 1 year ago

I had the same problem. The solution that works for me is to call the method reset() directly after the sound finnished playing.

Solution: export const doPlaySound = (soundFile: string) =>{ const playSound = new Sound(soundFile, Sound.MAIN_BUNDLE, (error: string) => { if(error){ console.log('failed to load the sound', error); return; } playSound.play((success: boolean) => { if(success) { playSound.reset(); return; }else{ console.log('playback failed due to audio decoding errors'); return; } }); }); }

sry i don't know how i can post the code here more readable

Harukisatoh commented 1 year ago

Hey guys, I was having the same issue as described in this thread. But I managed to solve the problem.

So, for future readers, this issue seems related to #607. Apparently, audios can display different durations on different devices (and this seems to be caused by the bit rate of the audio, don't ask me what that means, I don't know), and this inconsistency can cause some of the functions of the library to not work properly.

The solution for that is related in this comment, but I'm going to describe it here too.

You're going to need to format your audio and make the bit rate of it constant. To do that you need an audio editor (I've used Audacity, it's free). And for Audacity you need to (Not sure how that would work in another editor):

  1. Click on File menu and import your audio
  2. Now click on File menu again and click export
  3. After that you just need to export your audio with a constant bit rate (simple like that), take a look at the screenshot below.
Screen Shot 2022-08-02 at 19 45 22