[問題] 使用service 播放音樂問題

看板AndroidDev作者 (一日測 終身測)時間7年前 (2017/02/25 18:23), 編輯推噓4(406)
留言10則, 5人參與, 最新討論串1/1
大致列出比較可能出問題的地方 Manifest: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <service android:name=".MediaPlayService" android:enabled="true"/> MainActivity.java: Intent it = new Intent(MainActivity.this, MediaPlayService.class); startService(it); MediaPlayerService.java: public class MediaPlayService extends Service { private MediaPlayer player; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); player.stop(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Uri uri=Uri.fromFile( new File(Environment.getExternalStorageDirectory().getPath()+"/fate.mp3")); player=MediaPlayer.create(this,uri); player.start(); return super.onStartCommand(intent, flags, startId); } } Error Log: java.lang.RuntimeException: Unable to start service com.example.mrcat.lesson_052.MediaPlayService@a48b8eb with Intent { cmp=com.example.mrcat.lesson_052/.MediaPlayService }: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference 是指找不到嗎?路徑不對? 使用 startActivity(it) 路徑一樣可以直接撥放 (只是會跳去音樂播放器) 這次想在背景直接launch 照著書打也是失敗...(書的sample project 也一樣會crash) 音樂是放在內存的 (無SD card) Android 6.0.1 (Sony) 還請大神解惑 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.100.99 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1488018190.A.72F.html

02/25 21:10, , 1F
READ_EXTERNAL_STORAGE 好像需要要權限
02/25 21:10, 1F

02/25 21:23, , 2F
是指root嗎? 它也沒有跳出是否同意權限...
02/25 21:23, 2F

02/25 23:40, , 3F
跳出是要自己做的,你有做動態要權限?
02/25 23:40, 3F

02/25 23:41, , 4F
去App設定裡面的權限看那個權限有沒有開,沒有的話開起來試
02/25 23:41, 4F

02/26 02:23, , 5F
大大又救了我一次 需要去APP選項手動開啟權限
02/26 02:23, 5F

02/26 15:22, , 6F
android 6.0以上某些權限需要動態跟使用者要
02/26 15:22, 6F

02/26 15:23, , 7F
02/26 15:23, 7F

02/26 15:23, , 8F
基本上有出現在裡面的權限都要額外要
02/26 15:23, 8F

03/01 15:14, , 9F
6.0之後危險權限都被要求要動態確認
03/01 15:14, 9F

05/18 22:57, , 10F
Android 6.0以上存取外部裝置需要權限喔!
05/18 22:57, 10F
文章代碼(AID): #1OiLiESl (AndroidDev)