Contents
React Native Play Music / Sound
This is an Example to Play Music / Sound in React Native App for Android and iOS. In the current time, every app developer or the app owner wants to make a different mark of its app on the user and to do that they have many options like making the UI very different or to put a rememberable thing which makes their app different from other.
While talking about the attractive thing I mean anything which makes your app rememberable. If you have noticed the sound of WhatsApp while sending a message or the different ringtone of Hangout the yu can understand what I mean. Both apps have their own identity with there sound and other than this app you also have many other apps that are known for some small sounds they make.
Here we are going to see how can you play music or sound in your React Native App. We will cover how to make sound from the local project directory and from the remote URL.
To Play Sound in React Native
To play Sound / Music in React Native app we are going to use Sound
component provided by react-native-sound
which supports playing sound clips on iOS, Android, and Windows. react-native-sound
does not support streaming.
You can run 3 different types of sounds using this library which are listed below:
- aac
- mp3
- wav
To play the sound from remote URL
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();
});
});
From the local project directory
var sound1 = new Sound(require('./pew2.aac'),
(error, sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound1.play(() => {
sound1.release();
});
});
To stop the sound
sound4.stop(() => {
console.log('Stop');
});
I think this is enough to know about the library, now let’s move towards the example. In this example, we are going to make a screen with a list that will have 6 types of different sound options. I have covered both the ways, play local sound and remote sound.
Now let’s get started with the example.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli
command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use Sound
component we have to install react-native-sound
dependency. To install the dependency open the terminal and jump into your project
cd ProjectName
Now install the dependency
npm install react-native-sound --save
CocoaPods Installation
After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods. In this example, we need to install the pods for react-native-sound
.
Please use the following command to install CocoaPods
cd ios && pod install && cd ..
Code to Play Music / Sound in React Native App
Now Open App.js in any code editor and replace the code with the following code
App.js
// Play Music / Sound in React Native App for Android and iOS
// https://aboutreact.com/react-native-play-music-sound/
// import React in our code
import React, {useEffect} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
ScrollView,
} from 'react-native';
// import Sound Component
import Sound from 'react-native-sound';
const App = () => {
let sound1, sound2, sound3, sound4, sound5, sound6;
useEffect(() => {
Sound.setCategory('Playback', true); // true = mixWithOthers
return () => {
if (sound1) sound1.release();
if (sound2) sound2.release();
if (sound3) sound3.release();
if (sound4) sound4.release();
if (sound5) sound5.release();
if (sound6) sound6.release();
};
}, []);
//List of the dummy sound track
const audioList = [
{
title: 'Play mp3 sound from Local',
isRequire: true,
url: require('./resource/advertising.mp3'),
},
{
title: 'Play mp3 sound from remote URL',
url:
'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/advertising.mp3',
},
{
title: 'Play aac sound from Local',
isRequire: true,
url: require('./resource/pew2.aac'),
},
{
title: 'Play aac sound from remote URL',
url:
'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/pew2.aac',
},
{
title: 'Play wav sound from Local',
isRequire: true,
url: require('./resource/frog.wav'),
},
{
title: 'Play wav sound from remote URL',
url:
'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/frog.wav',
},
];
const playSound = (item, index) => {
if (index == 0) {
sound1 = new Sound(item.url, (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound1.play(() => {
sound1.release();
});
});
} else if (index == 1) {
sound2 = new Sound(item.url, '', (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound2.play(() => {
sound2.release();
});
});
} else if (index == 2) {
sound3 = new Sound(item.url, (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound3.play(() => {
sound3.release();
});
});
} else if (index == 3) {
sound4 = new Sound(item.url, '', (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound4.play(() => {
sound4.release();
});
});
} else if (index == 4) {
sound5 = new Sound(item.url, (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound5.play(() => {
sound5.release();
});
});
} else if (index == 5) {
sound6 = new Sound(item.url, '', (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
sound6.play(() => {
sound6.release();
});
});
}
};
const stopSound = (_item, index) => {
if (index == 0 && sound1) {
sound1.stop(() => {
console.log('Stop');
});
} else if (index == 1 && sound2) {
sound2.stop(() => {
console.log('Stop');
});
} else if (index == 2 && sound3) {
sound3.stop(() => {
console.log('Stop');
});
} else if (index == 3 && sound4) {
sound4.stop(() => {
console.log('Stop');
});
} else if (index == 4 && sound5) {
sound5.stop(() => {
console.log('Stop');
});
} else if (index == 5 && sound6) {
sound6.stop(() => {
console.log('Stop');
});
}
};
const ItemView = (item, index) => {
return (
<View style={styles.feature} key={index}>
<Text style={styles.textStyle}>{item.title}</Text>
<TouchableOpacity onPress={() => playSound(item, index)}>
<Text style={styles.buttonPlay}>Play</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => stopSound(item, index)}>
<Text style={styles.buttonStop}>Stop</Text>
</TouchableOpacity>
</View>
);
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text style={styles.titleText}>
Play Music / Sound in React Native App for Android and iOS
</Text>
<ScrollView style={{flex: 1}}>
{audioList.map(ItemView)}
</ScrollView>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
textStyle: {
flex: 1,
padding: 5,
},
buttonPlay: {
fontSize: 16,
color: 'white',
backgroundColor: 'rgba(00,80,00,1)',
borderWidth: 1,
borderColor: 'rgba(80,80,80,0.5)',
overflow: 'hidden',
paddingHorizontal: 15,
paddingVertical: 7,
},
buttonStop: {
fontSize: 16,
color: 'white',
backgroundColor: 'rgba(80,00,00,1)',
borderWidth: 1,
borderColor: 'rgba(80,80,80,0.5)',
overflow: 'hidden',
paddingHorizontal: 15,
paddingVertical: 7,
},
feature: {
flexDirection: 'row',
padding: 5,
marginTop: 7,
alignSelf: 'stretch',
alignItems: 'center',
borderTopWidth: 1,
borderTopColor: 'rgb(180,180,180)',
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running (macOS only)
react-native run-ios
Output Screenshots
This is how you can Play Music / Sound in React Native App for Android and iOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂