Play Music / Sound in React Native App for Android and iOS

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. This both apps have their own identity with there sound and other than this app you also have many other apps which 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 support 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

From the local project directory

To stop the sound

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 which 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

Now install the dependency

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

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

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
react-native run-ios (macOS only).

Image

This is how you can Play Music / Sound in React Native App for Android and iOS. If you have any doubt 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. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.