React-native-sound: Sound sometimes playing on Android, sometimes not

Created on 19 Feb 2020  ·  6Comments  ·  Source: zmxv/react-native-sound

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

Most helpful comment

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

All 6 comments

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

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

Hello,
Any updates on this issue?

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

@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

Thanks @AdamGerthel, will take a look 💯

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onepiece-dz picture onepiece-dz  ·  15Comments

facuacostag picture facuacostag  ·  10Comments

aspitsyn picture aspitsyn  ·  15Comments

dereksweet picture dereksweet  ·  11Comments

nukc picture nukc  ·  11Comments