View Course Path

How to Play, Pause, and Stop an mp3 Audio File in your Android app using MediaPlayer

What will you learn?

  1. Using MediaPlayer to create an audio object. If needed, you can read more about MediaPlayer and its documentation before moving forward.
  2. Adding files to your project’s resources.
  3. Use basic controls like play, pause, and stop. 

You can play any Audio File, as long as Android supports it. Whether it is an MP3 file or WAV file.


Building the interface of our MediaPlayer Android App

In this application, we require three buttons. Each of them will have a different purpose. The three buttons will be to play the sound, pause it, and stop it.

For our ease of use, we will add the linear layout first and drop the three buttons inside it. For this purpose, the Linear Layout is best for us.

If you are wondering which layout is best in Android and when to use them, refresh your memory by rereading it.

Once you highlight the button, by clicking on it, the properties panel will come up. Here you have to edit the IDs, the text, and the onClick() function.

  1. Edit the IDs of those buttons to btnPlay, btnPause, and btnStop. It’s always better to be uniform while naming the elements of your Application.
  2. Edit the text by giving it the simple name of Play, Pause, and Stop.
  3. Edit the onClick function to playSong, pauseSong, and stopSong.

That’s it for the design part.

Here’s an image of the interface along with the XML code.

The completed code of the interface of our MediaPlayer file

You will need to edit the package name to that of what you created. In this example, my package name reads “com.aasemjs.audioplayer.MainActivity”

Adding the demo sound file to your android audio app

Once we are done with our interface, we have to add the audio file to our project.

You have to add the files to the raw folder. You can find it here app > res > raw. If there is no raw folder, then create it by right-clicking on res > new > directory. Give the directory the name raw, and nothing else.

Inside this directory, all your audio files will be present. You can not simply drag and drop files here. You have to copy your source file, then right-click on the raw directory and click paste. The following popup will appear.

You have to make sure that the new name you are giving contains all small alphabets. The only valid characters for the name in any file will be [a-z; 0-9; _].

If another popup comes asking you to verify the file type, then select the option text. As long as you have given the correct file type in the name, Android will compile it as the file type you specified, mp3 in this case.

Why are special characters not allowed in Android File Names?

Special characters are not allowed, except the underscore symbol. This is because each file inside the folder is translated into java field name inside R.java class:

drawable/icon.png -> R.drawable.icon

And since special characters are not allowed in Java names, they can’t be used. As for the capital letters, theories suggest that it is made so because of Linux and Windows. Linux treats icon.png and Icon.png as two different files, and Windows treats it as one file. So to avoid any compile-time error, we aren’t allowed to use capital letters either in file names.

Add at least two audio files to your project, once you are done with that, we can start writing the code in MainActivity.java file.

Coding the functionality of our Android AudioPlayer app

As with all apps, we begin our coding in Java. First, we need to instantiate the MediaPlayer class. This will create an object of the class:

MediaPlayer mp;

What is happening here is that we have created an object mp of the class MediaPlayer. This means that all the properties of the class MediaPlayer are stored in this object.

Once you instantiate the MediaPlayer class, the package android.media.MediaPlayer will get automatically added to your code. If it doesn’t, then do it anywhere near the other imports like this:

import android.media.MediaPlayer;

Adding the audio file to this object

We want this code to run as soon as the app launches. So we’ll place it in the onCreate function:

mp = MediaPlayer.create(this, R.raw.abc);

Just make sure that instead of ‘abc,’ you mention the name you had given to your file. You don’t need to add ‘.mp3’ or ‘.wav’ or whatever filetype you are using.

Playing the sound in your Android App

Now we have to define the function we are calling when we hit the play button.

MediaPlayer has an inbuilt function called start( ). We’ll use that to our advantage.

public void playSong(View v){
    mp.start();
}

Now, if everything is in order, then the audio file would start playing.

Pausing the sound in our Android AudioPlayer app

If instead of some sound effect, you added a full-length song, then you have to add a way to pause the song.

Fortunately, MediaPlayer has an inbuilt function called pause( ), which we will run when we hit the pause button.

public void pauseSong(View v) {
    mp.pause();
}

Now, if everything is in order, then the audio file would pause. Hitting up the Play button will start the file from where it was paused.

Stopping the sound

Stopping the file will completely stop the file, and you won’t be able to rerun it.

MediaPlayer has an inbuilt function called stop( ).

public void stopSong(View v) {
    mp.stop();
}

Now when you hit the Play button again, nothing happens. This is because the MediaPlayer object has been cleared. There is no file present in the object anymore. So in the same function, add another line of code.

mp = MediaPlayer.create(this, R.raw.abcd);

This time, add the second file name to avoid confusion.

Run your app to test it. If you have forgotten how to set up your virtual device, then refresh your memory by going through the post again.

Note: The files are stored in the app, so if you add audio files of a size larger than 100mb, then your app size would be larger than 100mb.

Check out some more of our tutorials on Android.

Use Intent in Android Studio to start a new activity

Simple Calculator in Android Studio with source code

Weight Conversion app using Math Operators in Android Studio

Android Studio – Which Layout to use?

How to add video in Android Studio?

The completed code of the Java file looks like this:

Don’t forget to change your package name at the beginning of the code!

 

Related courses for this will be up soon!

5 thoughts on “How to Play, Pause, and Stop an mp3 Audio File in your Android app using MediaPlayer

  1. Hi, This is very helpful but how about if I wanna put 5 songs into raw folder. But I don’t wanna use listview. Just use 5 play buttons that associated in xml file. Is it possible to implement it ?

    1. You want to create 5 unique buttons and each button corresponds to a different song?

      You will need to create 5 different functions and associate each function to a different song.

      It is possible to implement it. Go for it!

    1. Hello Aaron,

      You can’t play multiple sound files at the same time. The speaker is a resource reserved for one file at a time.

      However, if you are able to play it finding a loophole in the system, the stop() function will stop playing all files that are in the buffer altogether.

      Hope this was of help!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top