Skip to main content

How to handle audio playback errors using MediaPlayer in Kotlin Android

How to handle audio playback errors using MediaPlayer in Kotlin Android.

Audio playback is a crucial component in many Android applications. However, sometimes errors can occur while playing audio files using the MediaPlayer class. These errors can be caused by various reasons like invalid file format, network issues, or insufficient permissions.

In this tutorial, we will learn how to handle audio playback errors using the MediaPlayer class in Kotlin Android. We will cover different scenarios where errors can occur and demonstrate how to handle them gracefully.


Before we begin, make sure you have the following prerequisites:

  • Android Studio installed on your machine
  • Basic knowledge of Kotlin and Android development

Step 1: Set up the project

First, let's set up a new Android project in Android Studio. Follow these steps:

  1. Open Android Studio and click on "Start a new Android Studio project" or choose "File" > "New" > "New Project".
  2. Enter the necessary details like the application name, package name, and location.
  3. Select the minimum SDK version and choose an empty activity template.
  4. Click on "Finish" to create the project.

Step 2: Add audio file to the project

Next, we need an audio file to test the MediaPlayer playback. Follow these steps to add an audio file to the project:

  1. Right-click on the "res" directory in the project structure and select "New" > "Directory".
  2. Name the directory as "raw" and click on "OK".
  3. Right-click on the "raw" directory and select "New" > "File".
  4. Choose an audio file from your local machine and click on "OK".

Step 3: Set up MediaPlayer

Now, let's set up the MediaPlayer in our Kotlin code. Add the following code inside the activity class:

private lateinit var mediaPlayer: MediaPlayer

private fun setupMediaPlayer() {
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file)
mediaPlayer.setOnPreparedListener { mp ->
mediaPlayer.setOnErrorListener { mp, what, extra ->

private fun handleError() {
// Handle the error gracefully

In the above code, we initialize the MediaPlayer by using the create() method, passing the audio file resource ID. We also set an OnPreparedListener to start the playback when the MediaPlayer is ready. Additionally, we set an OnErrorListener to handle any errors that occur during playback.

Step 4: Handle playback errors

Now, let's implement the handleError() function to handle playback errors. Depending on the error, you can perform different actions such as displaying an error message, logging the error, or retrying the playback.

Here's an example implementation:

private fun handleError() {
val error = mediaPlayer.error
if (error == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
// Unknown error occurred
// Display an error message to the user
} else if (error == MediaPlayer.MEDIA_ERROR_IO) {
// I/O error occurred
// Log the error for debugging purposes
} else if (error == MediaPlayer.MEDIA_ERROR_UNSUPPORTED) {
// Unsupported media format
// Retry playback with a different format or notify the user
} else if (error == MediaPlayer.MEDIA_ERROR_TIMED_OUT) {
// Connection timed out
// Retry playback or notify the user about the network issue

In the above code, we check the error code using the mediaPlayer.error property. Based on the error code, we perform the corresponding action. You can customize the error handling logic based on your application's requirements.

Step 5: Request necessary permissions

If your audio file is stored externally or requires specific permissions to access, make sure to request those permissions before starting the MediaPlayer. You can use the requestPermissions() method to handle permission requests.

Here's an example implementation:


private fun setupPermissions() {
if (ContextCompat.checkSelfPermission(
) != PackageManager.PERMISSION_GRANTED
) {
} else {

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
when (requestCode) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// Permission denied, handle accordingly

In the above code, we first check if the READ_EXTERNAL_STORAGE permission is granted. If not, we request the permission using requestPermissions(). In the onRequestPermissionsResult() callback, we handle the permission result and proceed with setting up the MediaPlayer if the permission is granted.

Step 6: Start playback

Finally, call the setupPermissions() method to initiate the audio playback. Make sure to call this method in an appropriate place, such as the onCreate() method of your activity.

Here's an example:

override fun onCreate(savedInstanceState: Bundle?) {


And that's it! You have now learned how to handle audio playback errors using MediaPlayer in Kotlin Android. You can further enhance the error handling logic based on your specific requirements.


In this tutorial, we covered how to handle audio playback errors using the MediaPlayer class in Kotlin Android. We learned how to set up the MediaPlayer, handle playback errors, and request necessary permissions. By implementing proper error handling, you can provide a better user experience and handle different error scenarios effectively.