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 f开发者_开发问答older 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.
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
精彩评论