Sooraj
Sooraj

Reputation: 10567

React-native playing audio from s3 URL using react-native-sound

I'm new to react-native and trying to create an audio playing app.

I used react-native-sound to achieve the same. In the documentation, it specifies that I can play the file from a network. But I could not find any docs for the same.

Right now I'm uploading audio files from my ROR backend and adding the file to a local folder inside react. I'm changing that to aws s3.

And the audio file is started like -

var whoosh = new Sound(this.state.soundFile, Sound.MAIN_BUNDLE, (error) => {

where this.state.soundFile is a local file name (string) inside the specified folder.

Is this possible?

Upvotes: 6

Views: 15516

Answers (2)

Shivo'ham
Shivo'ham

Reputation: 3062

To play the sound from remote URL

import Sound from 'react-native-sound';

var sound1 = new Sound('https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/pew2.aac', '',
  (error, sound) => {
    if (error) {
      alert('error' + error.message);
      return;
    }
    sound1.play(() => {
      sound1.release();
    });
  });

Upvotes: 0

Isilher
Isilher

Reputation: 331

You can play remote sounds with react-native-sound by specifying a url and not setting the bundle:

import React, { Component } from 'react'
import { Button } from 'react-native'
import Sound from 'react-native-sound'

class RemoteSound extends Component {
  playTrack = () => {
    const track = new Sound('https://www.soundjay.com/button/button-1.mp3', null, (e) => {
      if (e) {
        console.log('error loading track:', e)
      } else {
        track.play()
      }
    })
  }

  render() {
    return <Button title="play me" onPress={this.playTrack} />
  }
}

export default RemoteSound

Upvotes: 13

Related Questions