React-native-sound: Sometimes some audios don't reproduce

Created on 7 Feb 2018  路  1Comment  路  Source: zmxv/react-native-sound

I'm several audios on FlatList and I'm trying to play one by one. Until in some moment, it stops and does not work anymore.

I'm debugging and the audio looks like load normally, just sound doesn't come out.

My code:

 async playSound (url) {
    await this.loadSound(url)
    this.player.play((success) => {
      if (!success) {
        this.player.reset()        
      }
      this.player.release()
    })
  }

  loadSound (url) {
    return new Promise((resolve, reject) => {
      Sound.setCategory('Playback', true)
      this.player = new Sound(url, undefined, (error) => {
        if (error) {
          reject(error)
        }
        resolve()
      })
    })
  }

Did anybody have this problem and can help me?

To complement, my poc's interface
image

Most helpful comment

I'm only had calling .release() when the audio has ended
so, I'm also just calling when pausing like .pause.release() and works

My actual code, maybe would it help others:

import Sound from 'react-native-sound'

class AudioManager {
  constructor () {
    this.player = null
    this.currentAudio = null
  }

  async play (url, onFinishCallback) {
    if (this.currentAudio !== url) {
      await this.loadSound(url)
      this.currentAudio = url
    }
    this.resume(onFinishCallback)
  }

  pause () {
    if (this.player === null) return
    this.player.pause()
  }

  resume (onFinishCallback) {
    this.player.play((success) => {
      if (!success) {
        this.player.reset()
      }
      this.release()
      onFinishCallback()
    })
  }

  loadSound (url) {
    this.release()

    Sound.setCategory('Playback', true)
    return new Promise((resolve, reject) => {
      this.player = new Sound(url, undefined, (error) => {
        if (error) {
          reject(error)
        }
        resolve()
      })
    })
  }

  release () {
    if (this.player === null) return
    this.player.pause().release()
    this.currentAudio = null
  }
}

export default new AudioManager()

>All comments

I'm only had calling .release() when the audio has ended
so, I'm also just calling when pausing like .pause.release() and works

My actual code, maybe would it help others:

import Sound from 'react-native-sound'

class AudioManager {
  constructor () {
    this.player = null
    this.currentAudio = null
  }

  async play (url, onFinishCallback) {
    if (this.currentAudio !== url) {
      await this.loadSound(url)
      this.currentAudio = url
    }
    this.resume(onFinishCallback)
  }

  pause () {
    if (this.player === null) return
    this.player.pause()
  }

  resume (onFinishCallback) {
    this.player.play((success) => {
      if (!success) {
        this.player.reset()
      }
      this.release()
      onFinishCallback()
    })
  }

  loadSound (url) {
    this.release()

    Sound.setCategory('Playback', true)
    return new Promise((resolve, reject) => {
      this.player = new Sound(url, undefined, (error) => {
        if (error) {
          reject(error)
        }
        resolve()
      })
    })
  }

  release () {
    if (this.player === null) return
    this.player.pause().release()
    this.currentAudio = null
  }
}

export default new AudioManager()
Was this page helpful?
0 / 5 - 0 ratings