我正在使用服务来播放背景音乐。问题是当我完成活动时音乐继续播放。
这是来自 Main Activity 的代码,它启动服务
Intent svc=new Intent(HomeActivity.this, BackgroundSoundService.class);
startService(svc);
背景声音服务.java
public class BackgroundSoundService extends Service {
private static final String TAG = null;
public static MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("atMedia", "Backround Music playing");
player = MediaPlayer.create(this, R.raw.background);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}