我有一个服务,它接收一个音频文件并用媒体播放器播放这就是我打电话给我的服务
private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", path);
startService(music);
}
这是我的服务
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;
}
}
永远不会执行服务,从未制作过toast,媒体播放器也不会播放该路径。谢谢我提前帮助
如果我在我的清单中声明它
<service android:name=".MusicService" android:enabled="true"></service>
我收到强制关闭错误
答案 0 :(得分:8)
答案 1 :(得分:2)
你需要公开你的课程。 即。
public class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;