Friday, July 23, 2010

Android MediaPlayer

The exercise play, pause and stop a mp3 file in /res/raw folder, using android.media.MediaPlayer.

Android MediaPlayer

First of all, copy a mp3 file in /res/raw folder.
(You have to create the folder "raw" under "res", and copy a mp3 file named "music.mp3" into it.)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/playpause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/quit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Quit!"
/>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



AndroidMediaPlayer.java

package com.exercise.AndroidMediaPlayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidMediaPlayer extends Activity {

MediaPlayer mediaPlayer;
Button buttonPlayPause, buttonQuit;
TextView textState;

private int stateMediaPlayer;
private final int stateMP_NotStarter = 0;
private final int stateMP_Playing = 1;
private final int stateMP_Pausing = 2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonPlayPause = (Button)findViewById(R.id.playpause);
    buttonQuit = (Button)findViewById(R.id.quit);
    textState = (TextView)findViewById(R.id.state);

    buttonPlayPause.setOnClickListener(buttonPlayPauseOnClickListener);
    buttonQuit.setOnClickListener(buttonQuitOnClickListener);

    initMediaPlayer();

}

private void initMediaPlayer()
{
 mediaPlayer = new  MediaPlayer();
    mediaPlayer = MediaPlayer.create(AndroidMediaPlayer.this, R.raw.music);
    stateMediaPlayer = stateMP_NotStarter;
    textState.setText("- IDLE -");
}

Button.OnClickListener buttonPlayPauseOnClickListener
 = new Button.OnClickListener(){

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch(stateMediaPlayer){
  case stateMP_NotStarter:
   mediaPlayer.start();
   buttonPlayPause.setText("Pause");
   textState.setText("- PLAYING -");
   stateMediaPlayer = stateMP_Playing;
   break;
  case stateMP_Playing:
   mediaPlayer.pause();
   buttonPlayPause.setText("Play");
   textState.setText("- PAUSING -");
   stateMediaPlayer = stateMP_Pausing;
   break;
  case stateMP_Pausing:
   mediaPlayer.start();
   buttonPlayPause.setText("Pause");
   textState.setText("- PLAYING -");
   stateMediaPlayer = stateMP_Playing;
   break;
  }

 }
};

Button.OnClickListener buttonQuitOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 mediaPlayer.stop();
 mediaPlayer.release();
 finish();
}
};
}


Download the files.

next: Play mp3 in SD Card, using Android's MediaPlayer

Updated@2016-06-18:
Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, with checking and requesting permission at runtime.

14 comments:

  1. Hello,
    I need help, please!
    I'am just 15 years, and I started programming on Android Saturday.
    I'm a beginner!

    That's why I ask you to help me:

    I'm doing an application, and I wish the volume (with setVolume ()),
    turns change SeekBar.
    But I can not return the volume when changing the SeekBar (OnSeekBarChange. or other ..)

    Can you help me?
    My email address is: igors@live.fr

    Thank you in advance
    Igor. Written in France. :P

    ReplyDelete
  2. thank you very much.
    This is really sweet!

    ReplyDelete
  3. Hello! Thanks alot for this

    I've two questions.
    I do have the 'res' folder, but not the 'raw' folder. So I made the 'raw' folder by hand and put inside an MP3.

    The thing is that there's an error apearing is this line:
    mediaPlayer = MediaPlayer.create(AndroidMediaPlayer.this, R.raw.music);"

    Exactly in: R.raw.music


    It would be great if you could reply me to my email: fastflipdistribution@gmail.com

    Regards

    ReplyDelete
  4. hello khet,

    yes, you have made the 'raw' folder by hand (in Eclipse Package Explorer) and put inside an MP3.

    ReplyDelete
  5. How do I do this with multiple songs and next and previous buttons? Thanks

    ReplyDelete
  6. Hi, i tried a few of audio playing using MediaPlayer including your tutorial but i still cant have any audio playing out from my emulator... It's very frustrating... Could you please help me on this? Thanks!

    ReplyDelete
  7. can't u help me to create button next or previous for playing music?
    thx..

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Can this AndroidMediaPlayer support .mp4 or .3gp format video file which stored in 'raw' folder?

    I have followed the steps to build this project but it didn't show any video even the sound when I press PLAY button? It just change the status to PLAYING.

    What platform I should use to build this project?

    ReplyDelete
  10. Hey there, wanted to thank you for the post. I was able to get this up and running, and the example was easier to follow than other tutorials and examples I looked for!

    ReplyDelete
  11. Hello.. I am Brijesh.. I want to know how can I play songs from my android phone memory and sd card?

    ReplyDelete