[問題] 使用service 播放音樂問題
大致列出比較可能出問題的地方
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
02/25 21:10, 1F
→
02/25 21:23, , 2F
02/25 21:23, 2F
→
02/25 23:40, , 3F
02/25 23:40, 3F
→
02/25 23:41, , 4F
02/25 23:41, 4F
→
02/26 02:23, , 5F
02/26 02:23, 5F
推
02/26 15:22, , 6F
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
03/01 15:14, 9F
推
05/18 22:57, , 10F
05/18 22:57, 10F
AndroidDev 近期熱門文章
PTT數位生活區 即時熱門文章