How to Build an Audio Player in React Native

Published on Aug 28, 2019

14 min read

EXPO

cover_image

Learning React Native development revolves around some common interface patterns that you should practice. One common UI built-in mobile application is an audio player.

In this tutorial, you are going to build a functioning interface for an audio player with common functionalities like

  • Load the audio file;
  • Play/pause the audio file;
  • Navigate to next track;
  • Navigate to the previous track.

Apart from building the user interface, you are also going to learn a lot about using the expo-av module. This module provides an API for any Expo application to consume for media playback. Also, this module contains APIs both for audio and video media, but here we are only going to look at the audio portion.

You will find the complete code for this tutorial at this GitHub repository.

What Are We Building?

🔗

The end result of this React Native tutorial is to have an audio player that can play tracks from remote audio files. For the demonstration, the app is going to use audio files related to a play written by William Shakespeare from Librivox. All these audio files are available under the public domain, so you do not have to worry about copyright issues.

Requirements

🔗

To follow this tutorial, please make sure you have the following installed on your local development environment and have access to the services mentioned below:

  • Nodejs (>=10.x.x) with npm/yarn installed.
  • expo-cli (>= 3.x.x), previously known as create-react-native-app.
  • Mac users must be running an iOS simulator.
  • Windows/Linux users must be running an Android emulator.

To know more about how to setup and run the simulator or the emulator on your local development environment visit React Native’s official documentation here.

Getting Started

🔗

To start, you first have to initialize a new React Native project using the expo-cli tool. The only requirement right now is to have expo-cli installed. Then, create a new project directory, navigate to it, and install the required dependency to add the functionality of playing an audio file inside the React Native app.

expo init music-player-expo
# navigate inside the app folder
cd music-player-expo
# install the following dependency
npm install expo-av

The dependency expo-av will help you use the Audio API and its promise-based asynchronous methods to play the audio files within the React Native app. The source of these audio files can be local or remote.

Once you have generated the app and installed the dependency, execute the command below to open the boilerplate application that comes with expo-cli.

expo start

The following screen will welcome you:

ss1

Since the app will be consuming a bunch of audio files from a remote resource, it is better if you create an array that will contain details related to each of the audio files and their resource in the form of a URI. Open App.js and add the following array before the App component.

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
3
4const audioBookPlaylist = [
5 {
6 title: 'Hamlet - Act I',
7 author: 'William Shakespeare',
8 source: 'Librivox',
9 uri:
10 'https://ia800204.us.archive.org/11/items/
11 hamlet_0911_librivox/hamlet_act1_shakespeare.
12 mp3',
13 imageSource:
14 'http://www.archive.org/download/
15 LibrivoxCdCoverArt8/hamlet_1104.jpg'
16 },
17 {
18 title: 'Hamlet - Act II',
19 author: 'William Shakespeare',
20 source: 'Librivox',
21 uri:
22 'https://ia600204.us.archive.org/11/items/
23 hamlet_0911_librivox/hamlet_act2_shakespeare.
24 mp3',
25 imageSource:
26 'http://www.archive.org/download/
27 LibrivoxCdCoverArt8/hamlet_1104.jpg'
28 },
29 {
30 title: 'Hamlet - Act III',
31 author: 'William Shakespeare',
32 source: 'Librivox',
33 uri:
34 'http://www.archive.org/download/
35 hamlet_0911_librivox/hamlet_act3_shakespeare.
36 mp3',
37 imageSource:
38 'http://www.archive.org/download/
39 LibrivoxCdCoverArt8/hamlet_1104.jpg'
40 },
41 {
42 title: 'Hamlet - Act IV',
43 author: 'William Shakespeare',
44 source: 'Librivox',
45 uri:
46 'https://ia800204.us.archive.org/11/items/hamlet_0911_librivox/hamlet_act4_shakespeare.mp3',
47 imageSource:
48 'http://www.archive.org/download/LibrivoxCdCoverArt8/hamlet_1104.jpg'
49 },
50 {
51 title: 'Hamlet - Act V',
52 author: 'William Shakespeare',
53 source: 'Librivox',
54 uri:
55 'https://ia600204.us.archive.org/11/items/hamlet_0911_librivox/hamlet_act5_shakespeare.mp3',
56 imageSource:
57 'http://www.archive.org/download/LibrivoxCdCoverArt8/hamlet_1104.jpg'
58 }
59];
60
61export default function App() {
62 return (
63 <View style={styles.container}>
64 <Text>Open up App.js to start working on your app!</Text>
65 </View>
66 );
67}
68
69const styles = StyleSheet.create({
70 container: {
71 flex: 1,
72 backgroundColor: '#fff',
73 alignItems: 'center',
74 justifyContent: 'center'
75 }
76});

In the above snippet, imageSource is going to provide an album or an audiobook cover.

Define an initial state in the App Component

🔗

In this section, you are going to convert the functional App component that comes with the default Expo app into a class component. This conversion will be useful to define an initial state that will hold an object with properties like:

  • isPlaying to check whether the audio player is playing the audio file or not. This is going to be a boolean value.
  • playbackInstance to hold the instance of the current track being played.
  • volume the current volume of the audio for this media.
  • currentIndex to gather the index of which track is currently being played. This helps in navigating and playing the next and the previous track from the audioBookPlaylist array.
  • isBuffering holds a boolean value to check whether the current media is being buffered.

The initial state of the App component is going to look like the below snippet. Open App.js to add the state.

1export default class App extends React.Component {
2 state = {
3 isPlaying: false,
4 playbackInstance: null,
5 currentIndex: 0,
6 volume: 1.0,
7 isBuffering: false
8 };
9 render() {
10 return (
11 <View style={styles.container}>
12 <Text>Open up App.js to start working on your app!</Text>
13 </View>
14 );
15 }
16}

Building the UI: Audio Player Controls

🔗

In this section, let us build the UI components of how the basic audio player is going to look like. To start, please make sure that you are importing React Native elements like TouchableOpacity and Image from the core. Also, to add icons, let us import Ionicons from the library @expo/vector-icons.

This package comes with the Expo app, so you do not have to undergo the process of installing it as a separate module. This demo is going to use Ionicons from this package but feel free to use another icon library.

1import { StyleSheet, TouchableOpacity, View, Image } from 'react-native'
2import { Ionicons } from '@expo/vector-ico

The next step is to modify the render function inside App.js. Inside the container view, you are going to add an image that will display the cover of the audio book from the resource. Beneath this cover image, there will be three buttons that will let you control the audio files within the app.

1<View style={styles.container}>
2 <Image
3 style={styles.albumCover}
4 source={{
5 uri: 'http://www.archive.org/download/LibrivoxCdCoverArt8/hamlet_1104.jpg'
6 }}
7 />
8 <View style={styles.controls}>
9 <TouchableOpacity style={styles.control} onPress={() => alert('')}>
10 <Ionicons name="ios-skip-backward" size={48} color="#444" />
11 </TouchableOpacity>
12 <TouchableOpacity style={styles.control} onPress={() => alert('')}>
13 {this.state.isPlaying ? (
14 <Ionicons name="ios-pause" size={48} color="#444" />
15 ) : (
16 <Ionicons name="ios-play-circle" size={48} color="#444" />
17 )}
18 </TouchableOpacity>
19 <TouchableOpacity style={styles.control} onPress={() => alert('')}>
20 <Ionicons name="ios-skip-forward" size={48} color="#444" />
21 </TouchableOpacity>
22 </View>
23</View>

The conditional rendering implied on the second button states that whenever the boolean value of isPlaying is changed to true, the UI will display a pause button instead of a play button. Each button is accumulating an icon.

All of these buttons are going to be inside another view with a specific styling. You will notice the same thing in the above snippet. Outside the class component, using a StyleSheet object, let us add the styling.

1const styles = StyleSheet.create({
2 container: {
3 flex: 1,
4 backgroundColor: '#fff',
5 alignItems: 'center',
6 justifyContent: 'center'
7 },
8 albumCover: {
9 width: 250,
10 height: 250
11 },
12 controls: {
13 flexDirection: 'row'
14 },
15 control: {
16 margin: 20
17 }
18});

To provide styles to your React Native components, there are no classes or IDs in React Native like in web development. To create a new style object, you use the StyleSheet.create() method. When creating a new style object every time the component renders, StyleSheet creates style objects with IDs that are further used to reference instead of rendering the whole component again and again.

Execute the command expo start from a terminal window, if you haven't already, and you will get the following result.

Exploring the Audio API

🔗

To play a sound in an Expo application, you’re required to use and import the API for the Audio class from expo-av. So at the top of the App.js file and after other imports, you can add the following line.

1import { Audio } from 'expo-av';

To customize the audio experience inside an iOS or an Android app, Expo provides an asynchronous method called setAudioModeAsync(). This method takes an options object as its only parameter. This object contains a list of key-value pairs that are required to enable and use the audio component.

Inside the App component, you are going to add a lifecycle method componentDidMount(). This method should be defined after the initial state. It will help you configure the Audio component from the expo-av module.

1async componentDidMount() {
2 try {
3 await Audio.setAudioModeAsync({
4 allowsRecordingIOS: false,
5 interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
6 playsInSilentModeIOS: true,
7 interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
8 shouldDuckAndroid: true,
9 staysActiveInBackground: true,
10 playThroughEarpieceAndroid: true
11 })
12
13 this.loadAudio()
14 } catch (e) {
15 console.log(e)
16 }
17 }

Let us take all the options that are being passed in the setAudioModeAsync method. These options will define how the audio player is going to behave.

The allowsRecordingIOS is a boolean which, when enabled, will allow recording in iOS devices. The playsInSilentModeIOS indicates whether the audiobook app should play while the device is in silent mode.

The interruptionModeIOS & interruptionModeAndroid is how the audio of the app will behave with the audio of other apps. For example, what if you receive a call while listening to the audio player? How will the audio from the audiobook app behave? The value of these two options sets that. Currently, the option for the iOS device is set to be interrupted by the audio of other apps, hence INTERRUPTION_MODE_IOS_DO_NOT_MIX.

However, in the case of Android, the value INTERRUPTION_MODE_ANDROID_DUCK_OTHERS indicates that the volume of the audio from other apps will be lowered while the audiobook app is running. This term, Duck is known as lowering the volume. To set this option for Android, you have to set the value of shouldDuckAndroid to true.

Lastly, the lifecycle method is going to trigger the loadAudio function, which you are going to see in action in the next section.

Loading the Audio File

🔗

After the lifecycle method componentDidMount() inside the App.js file, you are going to enter another asynchronous function called loadAudio(). This function will handle the loading of the audio file for the app's player.

1async loadAudio() {
2 const {currentIndex, isPlaying, volume} = this.state
3
4 try {
5 const playbackInstance = new Audio.Sound()
6 const source = {
7 uri: audioBookPlaylist[currentIndex].uri
8 }
9
10 const status = {
11 shouldPlay: isPlaying,
12 volume
13 }
14
15 playbackInstance.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate)
16 await playbackInstance.loadAsync(source, status, false)
17 this.setState({playbackInstance})
18 } catch (e) {
19 console.log(e)
20 }
21}
22
23onPlaybackStatusUpdate = status => {
24 this.setState({
25 isBuffering: status.isBuffering
26 })
27}

The new Audio.Sound() allows you to create an instance that will take the source of the audio file (which can be either from a local asset file or a remote API URI like in the current scenario). From the state property currentIndex the Audio instance created will find the index value in the array of audioBookPlaylist to read the source URI and play the audio file.

On the instance of Audio, a method called setOnPlaybackStatusUpdate is used. This method has a handler function being passed, which is known as onPlaybackStatusUpdate. This handler function is responsible for updating the UI whether the media is being currently buffered or being played. To track the state of buffering, isBuffering is used from the initial state property. Whenever the state of the Audio instance changes, this gets an update.

Lastly, the loadAsync function is called on the Audio instance, which takes in three parameters. This first parameter is the source of the audio file. The second parameter indicates the status of the object. This status object further uses the properties of shouldPlay and volume. The value of the property shouldPlay is indicated by isPlaying from the initial state object. The last boolean value passed in loadAsync indicates whether the audio player app should download the audio file before playing. In the current scenario, there is no requirement for that. Thus, it has been set to false.

Control Handlers

🔗

After the previous section, let us add three new methods which are going to control the state of the audio instance being played or paused. Also, changing to the next track or the previous track is going to be represented by different handler functions. Further, these handler functions are going to be used on onPress props of each button created in the UI section.

1handlePlayPause = async () => {
2 const { isPlaying, playbackInstance } = this.state;
3 isPlaying
4 ? await playbackInstance.pauseAsync()
5 : await playbackInstance.playAsync();
6
7 this.setState({
8 isPlaying: !isPlaying
9 });
10};
11
12handlePreviousTrack = async () => {
13 let { playbackInstance, currentIndex } = this.state;
14 if (playbackInstance) {
15 await playbackInstance.unloadAsync();
16 currentIndex < audioBookPlaylist.length - 1
17 ? (currentIndex -= 1)
18 : (currentIndex = 0);
19 this.setState({
20 currentIndex
21 });
22 this.loadAudio();
23 }
24};
25
26handleNextTrack = async () => {
27 let { playbackInstance, currentIndex } = this.state;
28 if (playbackInstance) {
29 await playbackInstance.unloadAsync();
30 currentIndex < audioBookPlaylist.length - 1
31 ? (currentIndex += 1)
32 : (currentIndex = 0);
33 this.setState({
34 currentIndex
35 });
36 this.loadAudio();
37 }
38};

The handlePlayPause checks the value of isPlaying to decide whether to play an audio file from the resource it is currently loaded or not. This decision is made using a conditional operator, and then the state is updated accordingly. The playBackInstance is holding the same value from the previous section when an audio file is loaded.

The next handler function handlePreviousTrack is used to skip back to the previous audio track in the playlist. It first clears the current track being played using unloadAsync from the Audio API, using the property value of currentIndex from the state. Similarly, the handler function handleNextTrack clears the current track and then using the currentIndex navigates to the next track.

Completing the Player UI

🔗

The last piece of the puzzle in this audio player app is to display the information of the audio file which is being played. This information is already provided in the mock API array audioBookPlaylist. Create a new function called renderFileInfo before the render function with the following JSX to display. Also, update the StyleSheet object.

1renderFileInfo() {
2 const { playbackInstance, currentIndex } = this.state
3 return playbackInstance ? (
4 <View style={styles.trackInfo}>
5 <Text style={[styles.trackInfoText, styles.largeText]}>
6 {audioBookPlaylist[currentIndex].title}
7 </Text>
8 <Text style={[styles.trackInfoText, styles.smallText]}>
9 {audioBookPlaylist[currentIndex].author}
10 </Text>
11 <Text style={[styles.trackInfoText, styles.smallText]}>
12 {audioBookPlaylist[currentIndex].source}
13 </Text>
14 </View>
15 ) : null
16 }
17
18// update the Stylesheet object
19const styles = StyleSheet.create({
20 container: {
21 flex: 1,
22 backgroundColor: '#fff',
23 alignItems: 'center',
24 justifyContent: 'center'
25 },
26 albumCover: {
27 width: 250,
28 height: 250
29 },
30 trackInfo: {
31 padding: 40,
32 backgroundColor: '#fff'
33 },
34 trackInfoText: {
35 textAlign: 'center',
36 flexWrap: 'wrap',
37 color: '#550088'
38 },
39 largeText: {
40 fontSize: 22
41 },
42 smallText: {
43 fontSize: 16
44 },
45 control: {
46 margin: 20
47 },
48 controls: {
49 flexDirection: 'row'
50 }
51})
52

Next, use this function inside the render method of the App component below the view that holds all the control buttons. Also, update the control buttons to use appropriate handler functions from the previous section. Here is the complete code of the render function.

1render() {
2 return (
3 <View style={styles.container}>
4 <Image
5 style={styles.albumCover}
6 source={{ uri: 'http://www.archive.org/download/LibrivoxCdCoverArt8/hamlet_1104.jpg' }}
7 />
8 <View style={styles.controls}>
9 <TouchableOpacity style={styles.control} onPress={this.handlePreviousTrack}>
10 <Ionicons name='ios-skip-backward' size={48} color='#444' />
11 </TouchableOpacity>
12 <TouchableOpacity style={styles.control} onPress={this.handlePlayPause}>
13 {this.state.isPlaying ? (
14 <Ionicons name='ios-pause' size={48} color='#444' />
15 ) : (
16 <Ionicons name='ios-play-circle' size={48} color='#444' />
17 )}
18 </TouchableOpacity>
19 <TouchableOpacity style={styles.control} onPress={this.handleNextTrack}>
20 <Ionicons name='ios-skip-forward' size={48} color='#444' />
21 </TouchableOpacity>
22 </View>
23 {this.renderFileInfo()}
24 </View>
25 )
26 }

Now, run the application, and you will get the following result.

Conclusion

🔗

You have reached the end of this tutorial. We hope you enjoyed it and learned how to integrate the expo-av library to use an Audio class to create functionality in your cross-platform applications and build an audio player. An important thing to retain from this demo application is how to use available methods like loadAsync(), and unloadAsync().

The resources used in order to create this tutorial can be found below:

Originally published at Jscrambler


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.


Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.