React-native-sound: Http Headers on url playback

Created on 15 May 2018  路  1Comment  路  Source: zmxv/react-native-sound

Is there a way to set the http headers for url playback?

>All comments

@DanielWare I use RNFetchBlob to download audio file with headers to local cache directory, then play it. It looks like this:

RNFetchBlob
      .config({
        path: RNFetchBlob.fs.dirs.CacheDir + '/voiceMsgReceived.aac',
        fileCache: true,
        appendExt: 'aac',
      })
      .fetch('GET', `https://example.com/file/${uri}`, {
        // your custom headers here
        'x-access-token': this.state.token
      })
      .then((res) => {
        console.log('The file saved to ', res.path())
        // Beware that when using a file path as Image source on Android,
        // you must prepend "file://"" before the file path
        return (Platform.OS === 'android' ? 'file://' : '') + res.path()
      })
      .then((path) => {
        // These timeouts are a hacky workaround for some issues with react-native-sound.
        // See https://github.com/zmxv/react-native-sound/issues/89.
        setTimeout(() => {
          const sound = new Sound(path, '', (error) => {
            if (error) {
              console.log('failed to load the sound', error)
            }
          })

          chatStore.setPlayingStatus(true)
          setTimeout(() => {
            sound.play((success) => {
              chatStore.setPlayingStatus(false)
              if (success) {
                console.log('successfully finished playing')
              } else {
                console.log('playback failed due to audio decoding errors')
              }
            })
          }, 100)
        }, 100)
      })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulmelnikow picture paulmelnikow  路  18Comments

findzen picture findzen  路  31Comments

oxilor picture oxilor  路  11Comments

yaronlevi picture yaronlevi  路  15Comments

zakster12 picture zakster12  路  16Comments