Monday, July 26, 2010

Play mp3 in SD Card, using Android's MediaPlayer

In the exercise "Android MediaPlayer", the application play mp3 file in /res/raw folder. In this exercise, it will be modified to play a mp3 file, named music.mp3, in SD Card.

First of all, copy a mp3 file (music.mp3) to SD Card. Refer to the article "Create SD Card in Android Emulator and copy files into, in Eclipse, Emulator and DDMS" to copy mp3 file to SD Card.

main.xm have no change, refer to the exercise "Android MediaPlayer".

AndroidMediaPlayer.java
package com.exercise.AndroidMediaPlayer;

import java.io.IOException;

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

public class AndroidMediaPlayer extends Activity {
 
 MediaPlayer mediaPlayer;
 Button buttonPlayPause, buttonQuit;
 TextView textState;
 
 private int stateMediaPlayer;
 private final int stateMP_Error = 0;
 private final int stateMP_NotStarter = 1;
 private final int stateMP_Playing = 2;
 private final int stateMP_Pausing = 3;
 
   /** 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()
   {
    String PATH_TO_FILE = "/sdcard/music.mp3";    
    mediaPlayer = new  MediaPlayer();
    
    try {
   mediaPlayer.setDataSource(PATH_TO_FILE);
   mediaPlayer.prepare();
   Toast.makeText(this, PATH_TO_FILE, Toast.LENGTH_LONG).show();
   stateMediaPlayer = stateMP_NotStarter;
         textState.setText("- IDLE -");
  } catch (IllegalArgumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
   stateMediaPlayer = stateMP_Error;
         textState.setText("- ERROR!!! -");
  } catch (IllegalStateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
   stateMediaPlayer = stateMP_Error;
         textState.setText("- ERROR!!! -");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
   stateMediaPlayer = stateMP_Error;
         textState.setText("- ERROR!!! -");
  }
   }
  
   Button.OnClickListener buttonPlayPauseOnClickListener
    = new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(stateMediaPlayer){
    case stateMP_Error:
     break;
    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.

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

9 comments:

HILMAN's said...

how to play more than one file music in the sd card?

Erik said...

hello HILMAN's,

I think you have to implement your own ui to handle the operation.

Chirag Gabani said...

you can use media content provider

take all supported media from that table and show in list view or whatever in form for eg.. grid view

put set on click listener and pass that path to this activity(in which media player is there)using intent

get that path using bundle and save in string variable and pass this to media player .....


i hope u will understand...

Keerthi said...

can you please give me a sample code for playing more than one file (present in a folder) from the sdcard?

Keerthi said...

can you please give the code for playing more than one file, which are present in a folder in android sdcard?

deepa said...

how to play a .wav file from sdcard in android using MediaPlayer.

vidhin said...

Hi , this tutorial really useful for me ... I have a doubt I need to trim a audio file..
for example I have a audio file with 5 MIn.. I want to trim the file 2min to 4min... and save to sdcard...

thanks for advance....

Vijay singh said...

mediaplayer.prepare(); arising a Exception .. What should i do ???

Erik said...

please read the updated post Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, with checking and requesting permission at runtime..