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.
You need to make sure the path you give to
is exactly correct. The best way to do this, instead of hardcoding the reference to '/sdcard/', is to use
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