Repository: zmxv/react-native-sound

Issue #607: getDuration() returns negative values for network files - android only
Created: 2019-08-16T06:08:04Z
Actually I am trying to Load sound from the network. Its works in iOS. But not in android. I'm getting duration as -0.001 in it

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.10.9, 0.11.0
React Native:0.59.3
iOS: works fine on iOS
Android:7, 7.1, 8, 9
Windows: not implementing in windows

Does the problem occur on...

 Simulator
 Device

Duplicate issue #646: Sound sometimes playing on Android, sometimes not
Created: 2020-02-19T18:01:59Z
🪲 Description
Playing sounds work sometimes and sometimes not. Only affects Android. Same exact code works flawlessly on iOS.
🪲 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.
🪲 What is the expected behavior?
That it works as intended, i.e. that I can trust the documentation of this package.
🪲 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)
}
💡 Does the problem have a test case?
 
💡 Possible solution

💡 Is there a workaround?

💡 If the bug is confirmed, would you be willing to create a pull request?

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

 Simulator
 Device

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

Device: Xperia D6603 running Android 6.0.1

Closing comment (2022-08-02T22:48:19Z): 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):

Click on File menu and import your audio
Now click on File menu again and click export
After that you just need to export your audio with a constant bit rate (simple like that), take a look at the screenshot below.