Skip to main content

How to pause and resume audio playback using MediaPlayer in Kotlin Android

How to pause and resume audio playback using MediaPlayer in Kotlin Android.

Here is a detailed step-by-step tutorial on how to pause and resume audio playback using MediaPlayer in Kotlin Android:

Step 1: Set up the project

Start by creating a new Android project in Kotlin. Open Android Studio, click on "Start a new Android Studio project," and follow the wizard to set up your project.

Step 2: Add the necessary permissions

In your project's AndroidManifest.xml file, add the following permissions to allow your app to access audio files:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 3: Add a button to play audio

In your app's activity_main.xml file, add a Button widget to play the audio. Here's an example:

<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Audio" />

Step 4: Initialize the MediaPlayer

In your MainActivity.kt file, import the necessary classes and create a MediaPlayer object. Here's an example:

import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

mediaPlayer = MediaPlayer()
}
}

Step 5: Set the audio file to play

In the onCreate() method, locate the audio file you want to play and set it to the MediaPlayer object using the setDataSource() method. Here's an example:

val audioFile = "path/to/audio/file.mp3"
mediaPlayer.setDataSource(audioFile)

Step 6: Prepare the MediaPlayer

After setting the audio file, call the prepare() method to prepare the MediaPlayer for playback. Here's an example:

mediaPlayer.prepare()

Step 7: Add a click listener to the play button

In the onCreate() method, get a reference to the playButton using findViewById() and add a click listener to it. Inside the click listener, call the start() method of the MediaPlayer to start playing the audio. Here's an example:

val playButton: Button = findViewById(R.id.playButton)
playButton.setOnClickListener {
mediaPlayer.start()
}

Step 8: Add a button to pause audio playback

In your activity_main.xml file, add another Button widget to pause the audio playback. Here's an example:

<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause Audio" />

Step 9: Pause audio playback

In the onCreate() method, get a reference to the pauseButton using findViewById() and add a click listener to it. Inside the click listener, call the pause() method of the MediaPlayer to pause the audio playback. Here's an example:

val pauseButton: Button = findViewById(R.id.pauseButton)
pauseButton.setOnClickListener {
mediaPlayer.pause()
}

Step 10: Add a button to resume audio playback

In your activity_main.xml file, add another Button widget to resume the audio playback. Here's an example:

<Button
android:id="@+id/resumeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Resume Audio" />

Step 11: Resume audio playback

In the onCreate() method, get a reference to the resumeButton using findViewById() and add a click listener to it. Inside the click listener, call the start() method of the MediaPlayer to resume the audio playback. Here's an example:

val resumeButton: Button = findViewById(R.id.resumeButton)
resumeButton.setOnClickListener {
mediaPlayer.start()
}

That's it! You have now implemented the pause and resume functionality for audio playback using MediaPlayer in Kotlin Android.