Why there is not any reliable library to work with Sounds and Musics in React Native (New Arch)?
How do you require() a sound file in React Native? - Stack Overflow
Play audio
How to play sound in React Native?
Videos
» npm install react-native-sound-player
I know there are couple of amazing libraries for audio but they hasn’t supported new architecture.
I have tried react-native-sound but it has many limitations and bugs eventually did’t work for me
What worked for me was simply using the app name as root:
import myMP3File from '<appname>/assets/mymp3.mp3';
const Sound = require('react-native-sound');
Sound.setCategory('Playback');
// Do whatever you like with it.
Sound(myMP3File, () => console.log('soundfile loaded!'));
Edit:
We now use rn-fetch-blob and the following solution to access local files:
import RNFetchBlob from 'rn-fetch-blob';
const { fs } = RNFetchBlob;
filePathIos = `${fs.dirs.MainBundleDir}/yourFolder/yourMp3.mp3`;
filePathAndroid = fs.asset('yourFolder/yourMp3.mp3');
The corresponding path can then be used to copy the file using fs.cp().
You need to use react-native-asset to put files to your project and access them with require keyword.
Example for file with path ./assets/audio/some-audio.mp3:
- Add or modify
react-native.config.js
module.exports = {
project: {
ios: {},
android: {},
},
dependencies: {
},
assets: [
'./assets/audio'
],
};
- Run
npx react-native-asset - Use the file:
const someAudio = require('./assets/audio/some-audio.mp3')
» npm install react-native-sound
Hi people, I have lost half day try to find a good library ( that works ) for playing audio in react native from a url..with no result.
Anyone have suggestions? Thanks
This will preload the sound and when you press the play button it will play it.
export default class MovieList extends Component {
componentDidMount(){
this.hello = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
});
}
handlePress() {
this.hello.play((success) => {
if (!success) {
console.log('Sound did not play')
}
})
}
render() {
const { movie } = this.props
return (
<TouchableOpacity onPress={this.handlePress.bind(this)}>
<View>
<Text>Start</Text>
</View>
</TouchableOpacity>
)
}
}
If you are looking to play sound tracks from a list of sounds please check this gist for detailed code.
Thanks very much who has answer to this question, but i have resolve this with this simple one:
import React, { Component } from 'react'
import { Text, View, TouchableOpacity } from 'react-native'
import Sound from 'react-native-sound';
export default class MovieList extends Component {
sound = new Sound('motorcycle.mp3');
playSound = () => {
this.sound.play()
}
render() {
return (
<View>
<TouchableOpacity onPress={this.playSound}>
<View>
<Text>Start</Text>
</View>
</TouchableOpacity>
</View>
)
}
}