40

I have a very simple mediaplayer that play background. It calls file from the apk, but I want it to play from any directory like as music or sdcard.

Here is my code:

private MediaPlayer mpintro;

.
.

mpintro = MediaPlayer.create(this, R.raw.intro);
        mpintro.setLooping(true);
        mpintro.start();
starball
  • 20,030
  • 7
  • 43
  • 238
John simit
  • 1,305
  • 2
  • 11
  • 14
  • 2
    @Johnsimit use Default Exception e. – Zala Janaksinh May 23 '13 at 13:50
  • 1
    It works like this: ` mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3")); mpintro.setLooping(true); mpintro.start();` It did not work properly as string filepath... – John simit May 23 '13 at 15:29
  • @Johnsimit i have deployed my all mp3 files inside http://localhost/Android/music/vande.mp3 i used like hw u said Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "//localhost/Android/Music/vande.mp3")); mpintro.setLooping(true); mpintro.start();` but it is not working – Sandeep V Apr 16 '14 at 08:39

7 Answers7

65

It works like this:

mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
        mpintro.start();

It did not work properly as string filepath...

John simit
  • 1,305
  • 2
  • 11
  • 14
26
String filePath = Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();   
mediaPlayer.start()

and this play from raw folder.

int resID = myContext.getResources().getIdentifier(playSoundName,"raw",myContext.getPackageName());

            MediaPlayer mediaPlayer = MediaPlayer.create(myContext,resID);
            mediaPlayer.prepare();
            mediaPlayer.start();

mycontext=application.this. use.

Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
  • it says **surround with try/catch**, when I use filepath.. is it needed? – John simit May 23 '13 at 13:31
  • @John simit every time any code u right use the try catch blosk it's good habit and good programing technic. – Zala Janaksinh May 23 '13 at 13:36
  • `catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }` it gives me whole catch code, is it enough to use `Exception e` or should I use what suggested to me? – John simit May 23 '13 at 13:40
  • @Johnsimit simple exception. use only default exception. if helpful then click as correct mark.ok bye – Zala Janaksinh May 23 '13 at 13:42
4

Here is the code to set up a MediaPlayer to play off of the SD card:

String PATH_TO_FILE = "/sdcard/music.mp3";    
mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();   
mediaPlayer.start()

You can see the full example here. Let me know if you have any problems.

crocboy
  • 2,887
  • 2
  • 22
  • 25
3

Use the code below it worked for me.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.mp3");
mp.prepare();
mp.start();
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
2

I use this class for Audio play. If your audio location is raw folder.

Call method for play:

new AudioPlayer().play(mContext, getResources().getIdentifier(alphabetItemList.get(mPosition)
                        .getDetail().get(0).getAudio(),"raw", getPackageName()));

AudioPlayer.java class:

public class AudioPlayer {

    private MediaPlayer mMediaPlayer;

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    // mothod for raw folder (R.raw.fileName)
    public void play(Context context, int rid){
        stop();

        mMediaPlayer = MediaPlayer.create(context, rid);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        mMediaPlayer.start();
    }

    // mothod for other folder 
    public void play(Context context, String name) {
        stop();

        //mMediaPlayer = MediaPlayer.create(c, rid);
        mMediaPlayer = MediaPlayer.create(context, Uri.parse("android.resource://"+ context.getPackageName()+"/your_file/"+name+".mp3"));
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        mMediaPlayer.start();
    }

}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
1

2020 - NOV

This worked for me:

final File file = new File(getFilesDir(), "test.wav");//OR path to existing file
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(file));
mediaPlayer.start();
Rajeev Jayaswal
  • 1,423
  • 1
  • 20
  • 22
0

In Kotlin:

1) The file is in a resource folder :

var nowLesson =resources.getIdentifier("test.mp3", "raw", packageName)
             
mediaPlayer = MediaPlayer.create(applicationContext, nowLesson)
    
mediaPlayer.start()

2) The file is in a Path (file path)

val file = File(
        this.getDir("Music", MODE_PRIVATE),
        "/t.mp3"
    )
mediaPlayer = MediaPlayer.create(this,Uri.fromFile(file ))
                mediaPlayer.start()
Mori
  • 2,653
  • 18
  • 24