Play Audio in Android using Jetpack Compose
In Android, a MediaPlayer is used to play media files like audio and video files in the application. The MediaPlayer Class uses the MediaPlayer API for performing various functions like creating a MediaPlayer, playing, pausing, starting, and stopping the media. In this article, we will show you how you could play an Audio File on Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Create a raw folder and add a music file (.mp3) to it
Navigate to the res folder and create a raw folder if does not exist. Add or copy a local audio file into this folder. In this project, the audio file name is “audio.mp3” and so the resource names to call it inside the application becomes R.raw.audio. To create a raw folder, follow this article Resource Raw Folder in Android Studio.
Step 3: Import drawable (Images) for Start and Pause Icon Buttons
Import two vector assets of your choice for displaying the start and pause button. In this project, we imported play and pause buttons as “ic_play” and “ic_pause” in the drawable folder. So we can call them in the application by the addresses R.drawable.ic_play and R.drawable.ic_pause. To import vector assets in your Android project, follow this article How to Add Vector Assets in Android Studio?
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.w3wiki.jcaudio import android.media.MediaPlayer import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Calling the composable function // to display element and its contents MainContent() } } } // Creating a composable // function to display Top Bar @Composable fun MainContent() { Scaffold( topBar = { TopAppBar(title = { Text( "GFG | Audio Player" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, content = { MyContent() } ) } // Creating a composable function to // create two icon buttons namely play and pause // Calling this function as content in the above function @Composable fun MyContent(){ // Fetching the local context val mContext = LocalContext.current // Declaring and Initializing // the MediaPlayer to play "audio.mp3" val mMediaPlayer = MediaPlayer.create(mContext, R.raw.audio) Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) { Row { // IconButton for Start Action IconButton(onClick = { mMediaPlayer.start() }) { Icon(painter = painterResource(id = R.drawable.ic_play), contentDescription = "" , Modifier.size( 100 .dp)) } // IconButton for Pause Action IconButton(onClick = { mMediaPlayer.pause() }) { Icon(painter = painterResource(id = R.drawable.ic_pause), contentDescription = "" , Modifier.size( 100 .dp)) } } } } // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true ) @Composable fun DefaultPreview() { MainContent() } |
Output:
In the below recording of the sample application, you can see that we are able to play the audio.mp3 file and play or pause them using the buttons.
Contact Us