0

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.

newenglander
  • 2,019
  • 24
  • 55
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

1 Answers1

3

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

George
  • 1,327
  • 11
  • 25