我有一个Service,它接收音频文件并使用MediaPlayer播放。这是我调用Service的方式:
这是我的
private void playAudio(String url) throws Exception {
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", path);
startService(music);
}
这是我的
Service
类:class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
@Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle e = intent.getExtras();
musicFile= e.getString("paths");
try {
mediaPlayer.prepare();
mediaPlayer.setDataSource(musicFile);
} catch (IllegalArgumentException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IllegalStateException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IOException i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Service
没有被执行,Toast
没有显示,MediaPlayer
没有播放。
我在清单文件中声明了它:
<service android:name=".MusicService" android:enabled="true"></service>
我遇到一个强制关闭的错误,我的日志中显示了 IllegalAccessException
:
java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
java.lang.IllegalAccessException: access to class not allowed