喜 我做的应用程序,用于播放歌搜索栏。如果从原始文件夹中发挥它的工作原理,但如果我从SD卡它显示空指针异常播放歌曲。
hi i am doing app for playing song with seek bar. if play from raw folder it works but if i play song from sdcard it shows 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.
您需要确保你给的路径的setDataSource()
是完全正确的。这样做,而不是硬编码的参考/ SD卡/这一点,最好的办法,就是用 android.os.Environment.getExternalStorageDirectory()
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