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

Sound sometimes playing on Android, sometimes not

Open AdamGerthel opened this issue 5 years ago • 17 comments

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

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

Are you using...

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

Which versions are you using?

  • React Native Sound: 0.11.0
  • React Native: 0.61.4
  • Android: Have tried a few different, 6.0.1 on physical device. Not sure about the emulators. Latest version I guess (brand new emulators from latest version of Android Studio).

Does the problem occur on...

  • [x] Simulator
  • [x] Device

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

  • Device: Xperia D6603 running Android 6.0.1

AdamGerthel avatar Feb 19 '20 18:02 AdamGerthel

+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().

CoSNaYe avatar Mar 02 '20 11:03 CoSNaYe

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

SurajMDurgad avatar Apr 13 '20 13:04 SurajMDurgad

Hello, Any updates on this issue?

julienjourde avatar Jun 10 '20 14:06 julienjourde

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

lelumees avatar Sep 01 '20 07:09 lelumees

@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

AdamGerthel avatar Sep 01 '20 07:09 AdamGerthel

Thanks @AdamGerthel, will take a look 💯

lelumees avatar Sep 01 '20 07:09 lelumees

+1 same issue

nguyenvanphuc2203 avatar Nov 09 '21 02:11 nguyenvanphuc2203

+1 same issue

anjandaffo avatar Jul 17 '22 10:07 anjandaffo

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

anjandaffo avatar Jul 17 '22 10:07 anjandaffo

@anjandaffo try to release() sound when component didmout

nguyenvanphuc2203 avatar Jul 17 '22 14:07 nguyenvanphuc2203

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

anjandaffo avatar Jul 18 '22 02:07 anjandaffo

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

nguyenvanphuc2203 avatar Jul 18 '22 03:07 nguyenvanphuc2203

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

anjandaffo avatar Jul 18 '22 03:07 anjandaffo

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

nguyenvanphuc2203 avatar Jul 18 '22 04:07 nguyenvanphuc2203

@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

anjandaffo avatar Jul 18 '22 04:07 anjandaffo

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

jens-tucholski avatar Jul 29 '22 09:07 jens-tucholski

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

Harukisatoh avatar Aug 02 '22 22:08 Harukisatoh