1

Is there any way to reproduce a sound that is on http without using native Android/IOS components?

I found this module react-native-sound, but it uses native parts and I wanted to know if there is a way to avoid being able to use them.

Paul
  • 3,644
  • 9
  • 47
  • 113

2 Answers2

1

If you are using expo then it has support for audio, you can see in the documentation here: https://docs.expo.io/versions/v32.0.0/sdk/audio

However if you are not using expo, and made your app with react-native init then you will need to use a dependency that has native modules or write your own native module as processing audio on the javascript thread will block your app and make it unusable.

Andrew
  • 26,706
  • 9
  • 85
  • 101
  • I do not use expo, I'm creating a module where an audio should be played. But I'm having some problems, do you have self experience with this? – Paul Jan 23 '19 at 11:21
1

if you are using expo you can do something like that

const soundObject = new Expo.Audio.Sound();

playSound = async () => {
    await soundObject.loadAsync({uri:'the uri of your audio'});
    soundObject.playAsync();
}

now it should start playing your mp3 , for further details find expo documentation here https://docs.expo.io/versions/latest/sdk/audio

Kazi Hasan Ali
  • 311
  • 4
  • 9
  • I'm trying to do as you say, even if to make this module I have to download the whole package of expo, I do not like it. I was thinking of using react-native-sound, but I could not get it to work properly. This is the link: https://snack.expo.io/BJkUZnrXN I have to make the sound repeat in loop. If you click on the album, the image changes, but the audio does not have to end or start again from the beginning, it must continue from where it was. – Paul Jan 23 '19 at 09:19
  • It happens many times, that are created, of the same audio track two currents, which mix and do not understand anything. How do you advise me to take action? Can a singleton be created in this case? – Paul Jan 23 '19 at 09:19
  • Are you using Expo or react native only ? if you are using expo then you dont have to install expo again and if you are using react native then react-native-sound is a good choice.but the link you have given is using expo to play the sound if you want to use pause and play function use soundObject.pauseAsync(); soundObject.playAsync(); – Kazi Hasan Ali Jan 23 '19 at 09:27
  • work with single instance of the sound Class then it will not mix with multiple sound – Kazi Hasan Ali Jan 23 '19 at 09:30
  • I do not use expo, I only use https://snack.expo.io/, to do some tests when I can. What I am creating is simply a module and then share it for those who want to use it. ;) I do not know if you use instagram, in instagram there is a similar component and I wanted to recreate it to share it for those who needed it. Take a look here: https://snack.expo.io/ByHV8nB74 What do you suggest me? If you want to try to explain better what I have to do. – Paul Jan 23 '19 at 09:47
  • in the Album.js i can see that there you have imported {Audio} from expo – Kazi Hasan Ali Jan 23 '19 at 09:52
  • If you try it happens some strange things, at least to me. As soon as I connect it works and hears audio, then it happens that the line falls down and I resume it, so you hear another line that mixes with the first line. I think it's due you have thread. Then if I click on the second button to play the second track "adele", it will not be played. – Paul Jan 23 '19 at 09:58
  • When you are playing the second track you need to stop the first one – Kazi Hasan Ali Jan 23 '19 at 10:03
  • I know, but I'm not succeeding I'm doing something wrong. Could you give us a look? – Paul Jan 23 '19 at 11:19
  • if playsound is you are using for playing sound then try that `playSound = async preview_url => { if(this.state.isPlaying == true) { this.stopSound() await soundObject.loadAsync({ uri: preview_url }); //soundObject.setIsLoopingAsync(true); soundObject.playAsync(); this.setState({isPlaying : true}); } else { await soundObject.loadAsync({ uri: preview_url }); //soundObject.setIsLoopingAsync(true); soundObject.playAsync(); this.setState({isPlaying : true}); } };` – Kazi Hasan Ali Jan 23 '19 at 12:21