M.A.Murali
M.A.Murali

Reputation: 10148

MediaPlayer issue between raw folder and sdcard on android

I am making an app for playing songs with a seek bar. If I play from the raw folder it works but if I play a song from the sdcard it shows a null pointer exception.

private MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(this, R.raw.t1); // it works

//  switch to sdcard
mediaPlayer.setDataSource("/sdcard/t1.mp3"); // null pointer exception.

I do not know what is the problem. Please help me.

Upvotes: 0

Views: 4362

Answers (1)

George
George

Reputation: 1327

You need to make sure the path you give to setDataSource() is exactly correct. The best way to do this, instead of hardcoding the reference to '/sdcard/', is to use android.os.Environment.getExternalStorageDirectory()

Try this, I think it will help you

MediaPlayer mediaPlayer = new MediaPlayer();
File path = android.os.Environment.getExternalStorageDirectory();
mediaPlayer.setDataSource(path + "/t1.mp3");

I hope this help you

Upvotes: 3

Related Questions