首先保证代码没有拼写错误,该有的权限都申请了,然后就是初始化MediPlayer的时候,
可以发现getExternalStorageDirectory已经弃用了,但是不影响代码的正常运行。我们加了一个Log语句打印打开文件对象的路径,运行程序看看Android Monitor的内容,如图所示:
看不清的话下面是复制的内容:
11-02 17:38:05.639 22754-22754/com.example.asus.playaudiotest D/MainActivity: initMediaPlayer:/storage/emulated/0/music.mp3
11-02 17:38:05.734 22754-22754/com.example.asus.playaudiotest D/WindowManager: Add to mViews: DecorView@1f33e8d[MainActivity],pkg= com.example.asus.playaudiotest
11-02 17:38:05.780 22754-22754/com.example.asus.playaudiotest V/OplusZoomWindowDecorViewHelper: setLastReportedMergedConfiguration mZoomDisplayHeight: 2400 getDecorView.DecorView@1f33e8d[MainActivity]
显然我们只要保证手机的/storage/emulated/0/music.mp3有这个文件就ok。
于是再次运行程序就可以播放音乐了。