Create a Music Player using React-Native
Last Updated :
03 Jul, 2025
React Native is a powerful framework that allows you to build mobile applications using JavaScript and React. In this tutorial, we are going to build a Music Player using React-Native. Here we will load the music files stored in our device and then use the play and pause buttons to control the music. We can also see the progress of the music played in duration.
To give you a better idea of what we’re going to create, let’s watch a demo video.
Demo Video
Prerequisites & Technologies Used:
Approach to create Music Player App
- In this app, we will have a single page or screen.
- At the start of app, using useEffect hook, we will call a function to load the music files.
- Then we will save the device music file URIs and File names in a array.
- In the component, array will be mapped and displayed in a column with file names.
- On the left of each tile, there will be play/pause button. On clicking it, the file will be loaded.
- After loading, another useEffect will fire on change of sound variable. It will store the progress of duration of music played. We will display the progress.
- After completion of music, sound will be unloaded.
Step-by-Step Implementation
Step 1: Create a React Native Project
Now, create a project with the following command.
npx create-expo-app app-name --template
Note: Replace the app-name with your app name for example : react-native-demo-app
Next, you might be asked to choose a template. Select one based on your preference as shown in the image below. I am selecting the blank template because it will generate a minimal app that is as clean as an empty canvas in JavaScript.
It completes the project creation and displays a message: "Your Project is ready!" as shown in the image below.
Now go into your project folder, i.e., react-native-demo
cd app-name
Project Structure:
Step 2: Run Application
Start the server by using the following command.
npx expo start
Then, the application will display a QR code.
For the Android users:
- For the Android Emulator, press " a" as mentioned in the image below.
- For the Physical Device, download the " Expo Go " app from the Play Store. Open the app, and you will see a button labeled " Scan QR Code. " Click that button and scan the QR code; it will automatically build the Android app on your device.
For iOS users, simply scan the QR code using the Camera app .
- If you're using a web browser, it will provide a local host link that you can use as mentioned in the image below.
Step 3: Update Dependencies
The updated dependencies in package.json file will look like:
"dependencies": {
"expo": "53.0.13",
"expo-status-bar": "~2.2.3",
"react": "18.2.0",
"react-native": "0.79.0",
"expo-media-library": "~17.1.7",
"expo-av": "~15.1.6",
"@expo/vector-icons": "^14.1.0"
},
Step 4: Start Coding
Example: In this example we are following the above-explained approach.
App.js
//App.js
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import * as MediaLibrary from 'expo-media-library';
import { useEffect, useState } from 'react';
import { Audio } from 'expo-av';
import Ionicons from '@expo/vector-icons/Ionicons';
export default function App() {
const [musicFiles, setMusicFiles] = useState([])
const [playing, setPlaying] = useState(-1)
const [sound, setSound] = useState(null);
const [progressDuration, setProgressDuration] = useState(0)
const fetchMusicFiles = async () => {
const permission = await MediaLibrary.requestPermissionsAsync(
);
const media = await MediaLibrary.getAssetsAsync({
mediaType: MediaLibrary.MediaType.audio,
});
setMusicFiles(media.assets)
}
const playMusic = async (fileUri) => {
const { sound } = await Audio.Sound.createAsync({
uri: fileUri,
});
setSound(sound);
await sound.playAsync();
}
const pauseMusic = async () => {
await sound.pauseAsync();
}
useEffect(() => {
if (!sound) {
return;
}
sound.setOnPlaybackStatusUpdate(
async (status) => {
if (status.didJustFinish) {
setPlaying(-1)
await sound.unloadAsync();
console.log("finished")
setSound(null);
}
else {
setProgressDuration(status.positionMillis / 1000)
}
}
);
}, [sound])
useEffect(() => {
fetchMusicFiles()
}
, [])
return (
<View style={styles.container}>
<StatusBar style="auto" />
<Text style={styles.heading}>
Welcome to GeeksforGeeks
</Text>
<View style={styles.list}>
{musicFiles.map((file, index) => {
return (
<View key={index}>
<TouchableOpacity onPress={
playing !== index ?
() => {
playMusic(file.uri)
setPlaying(index)
} :
() => {
pauseMusic()
setPlaying(-1)
}
} style={styles.playButton}>
<View style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
}}>
<Ionicons
name={playing !== index ?
"play" :
"pause"}
size={30}
color="white" >
<Text
style={styles.fileName}>
{file.filename}
</Text>
</Ionicons>
</View>
{/* progress duration if index == currentIndex*/}
<View style={styles.row}>
{playing === index ?
<Text style={styles.fileName}>
{progressDuration} / {file.duration}
</Text> :
<></>
}
</View>
</TouchableOpacity>
</View>
)
})}
</View>
</View>
);
}
const styles = StyleSheet.create({
row: {
flexDirection: "row",
justifyContent: "space-evenly",
},
container: {
backgroundColor: "#fff",
height: "100%",
marginTop: 50,
},
heading: {
color: "green",
fontSize: 30,
textAlign: "center",
fontWeight: "bold",
},
list: {
marginTop: 20,
flex: 1,
flexDirection: "column",
},
fileName: {
fontSize: 18,
color: "white",
fontWeight: 'bold',
},
playButton: {
backgroundColor: 'gray',
borderRadius: 50,
padding: 10,
margin: 10,
},
});
Output:
Similar Reads
Create a Video Player App using React-Native React-Native is an open-source JavaScript framework used to broaden cross-platform packages i.e., you may write code in React-Native and publish it as an Android or IOS app. In this article, we will build a Video Player app with the usage of React-Native. The app will run on both Android and IOS.Pre
4 min read
Create a 2048 Game using React-Native In this article, we are going to implement a 2048 Game using React Native. The 2048 game is a popular sliding puzzle game that involves combining tiles with the same number to reach the tile with the number 2048. Players can move the tiles in four directions: up, down, left, or right.PrerequisiteRea
7 min read
Create a Portfolio App using React-Native In this article, we are going to Create a portfolio app using React Native. The portfolio app is a simple application that is a collection of a person's qualifications, achievements, work samples, and other relevant materials. It is used to demonstrate one's abilities and suitability for a particula
5 min read
Create a Rock Paper Scissors Game using React-Native Rock, Paper, Scissors is a timeless game that has entertained people of all ages for generations. In this article, we will walk you through the process of creating a Rock Paper Scissors mobile game using React Native. You'll learn how to build a simple yet engaging game that can be played on both An
10 min read
Create a Crossword Puzzle Game using React-Native A Crossword Puzzle is a traditional word power game. The user needs to guess words based on the hints provided. So here, we will create a basic crossword puzzle app in React Native. The crossword puzzle app contains four buttons, namely Generate, Verify, Reset, and Solve. The Generate button will cr
7 min read
Create a Dashboard App using React-Native A dashboard app using react native is a software application designed to provide a consolidated and visual representation of important information, data, or features in a single, easily accessible interface. Dashboards are commonly used in various industries to help users monitor, analyze, and manag
7 min read