Skip to content

Dealing with the Android Media Player

April 5, 2010

Hey everyone,

This set of examples will deal with good practices (as I know that is) in dealing with Media Players in Android. There won’t be any large chunks of code snippets, but instead I’ll have several small examples and an explanation of what each do.

So the first example involves creating a Media Player instance from a given WAV or MP3 file. To add the WAV/MP3 file, simply create a new folder in the res folder of your project and label it raw. Then just add your WAV/MP3 file there. Now in your project you simply need to do something like:

// instantiate an instance of MediaPlayer
MediaPlayer mp_file = MediaPlayer.create(this, R.raw.your_song_file);

Pretty simple. Now, dealing with the media player file itself is pretty self-explanatory as well. Most likely you’ll want to call one of these methods:

// start playing the media file

// pause the media file

// stop the media file

but you can also set a number of other properties as well as add listeners to the media player, etc. (For a complete reference just visit the javadocs on MediaPlayer.) Now, you can also play around with the system’s AudioManager and let your application control the phone’s media volume (either by pressing the volume button on the phone or programmatically). For instance, if we want to retrieve the AudioManager and set its volume to the max volume, we can run this snippet of code:

AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);

// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_SHOW_UI);

// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_VIBRATE);

// etc

The flags control how you want to indicate to the user that you’ve adjusted the media player volume. The FLAG_PLAY_SOUND will play a system beep to indicate the volume, whereas FLAG_SHOW_UI will display a toast notifying the user that the media volume has been changed, and finally FLAG_VIBRATE will vibrate your phone. One can also use the AudioManager to alter the media volume programmatically like so:

mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);

And again you can change the parameters to alter what you want to do with the volume. Finally, once we are done with our media file, we want to avoid memory leaks in our application so we want to release all the memory accordingly like so:

protected void onStop() {
    // deallocate all memory
    if (mp_file != null) {
        if (mp_file.isPlaying()) {
        mp_file = null;

So basically when the Activity is stopped we stop playing the file, release it, and null it out to help out the garbage collect.

So yes, that’s the extent to which I’ve played around with the MediaPlayer. Let me know if there are questions (or if people know of other neat things to do with the MediaPlayer). Happy coding.

– jwei

20 Comments leave one →
  1. April 5, 2010 11:29 am

    Another nice tutorial! Keep them coming! 🙂

  2. Sumeet permalink
    June 30, 2010 9:00 pm

    ya..its a very interesting tutorial….

  3. July 3, 2010 10:39 am

    nice man good work
    keep it up
    m actually looking for someone who could tell me how to implement linearlayout in gallery
    If anyone knows that do leave a comment or mail me

  4. Antony permalink
    August 31, 2010 11:14 pm

    Hello, I want to play a number of sound files in raw directory one after another in android. How to do that?
    I want to make it a variable.
    eg:mp = MediaPlayer.create(geetaact.this, R.raw.first);
    //first should change by taking value from /raw folder.

  5. Surojit permalink
    October 3, 2010 7:24 pm

    Hi ,
    I would like to play an audio by using MediaPalyer, but not in speaker.
    There should be toggle button and user can do speaker on or off from there.

    Is there any way to do it ?

    Thanks !!!


    • October 5, 2010 12:32 am

      Hi Surojit,

      I’m a little confused – if it’s not being played through the speaker then where do you want it to be played through?

      Or do you want the audio to play silently or something?

      – jwei

      • Surojit permalink
        October 5, 2010 8:04 am

        Actually I want to play it in the earphone of the device. i.e. the same way that we usually do phone call ( listen to somebody talking over phone ).


  6. Maninder permalink
    December 21, 2010 4:32 am

    Is there any way to fetch the running instance of media player. I want to fetch the running instance of the media player and wanna the current file running as well as play/pause the song.

    • December 24, 2010 7:19 am

      Same problem. I need to get the running instance of media player so I could assign listeners to it. It’s like extending the media player with the app that I am going to create.

  7. January 3, 2011 9:31 am

    .stop() does not stop the sound. Thanks for the sample code though. This is the same thing I tried and what the documentation suggests will work.

  8. Frederic Rounds permalink
    April 24, 2011 8:36 pm

    I would like to play audio over a phone connection during a call so the called party could hear the recording.

  9. June 12, 2011 1:23 am

    How to play mms protocol file. i mean to say online radio which are based on mms protocol.
    Any ideas experts.

  10. kumar permalink
    July 23, 2012 4:13 am

    how to convert .wav file into .mid file

  11. aditya permalink
    January 10, 2013 9:50 pm

    I have implemented your code in my app and i am facing one problem i.e.when the app starts all buttons play, pause and stop works fine but when i restart the app i am unable to pause or stop the song.Any help will be appreciated

  12. bongani23 permalink
    May 22, 2013 4:45 am

    Hi I have implemented my code as below my challenge is l want one to stop as soon as l click on the button please help out:
    ImageButton ImageButton2 = (ImageButton) findViewById(;
    ImageButton2.setOnClickListener (new OnClickListener() {
    public void onClick(View v) {

    Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
    public void run() {

    startActivity(new Intent(getApplicationContext(), SoundTwo.class));

    • May 24, 2013 11:34 am

      Hi bongani23,

      What’s the error right now?

      – jwei

      • bongani23 permalink
        May 27, 2013 3:59 am

        Errors are on the logcat :

        05-27 14:02:05.982: E/MediaPlayer(13522): start called in state 0
        05-27 14:02:05.982: E/MediaPlayer(13522): error (-38, 0)
        05-27 14:02:05.990: E/MediaPlayer(13522): Error (-38,0)
        05-27 14:02:05.990: E/MediaPlayer(13522): mOnErrorListener is null. Failed to send MEDIA_ERROR message.
        05-27 14:02:05.990: E/MediaPlayer(13522): start called in state 0
        05-27 14:02:05.990: E/MediaPlayer(13522): error (-38, 0)
        05-27 14:02:05.990: E/MediaPlayer(13522): Error (-38,0)
        05-27 14:02:05.990: E/MediaPlayer(13522): mOnErrorListener is null. Failed to send MEDIA_ERROR message.
        05-27 14:02:05.990: E/MediaPlayer(13522): start called in state 0


  1. programatically change volume
  2. Supporting multiple devices on Android | Infinut

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: