所有活动播放相同的背景音乐
The same background music playing in all activities
我在所有活动中都使用服务来播放背景音乐,而且效果很好。问题是音乐会继续播放,即使我的应用程序处于后台(当用户使用主页按钮或后退按钮退出时)。我该如何解决?
服务classBackgroundSoundService
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.slow_shock);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return START_NOT_STICKY;
}
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() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
开始于
Intent svc = new Intent(this, BackgroundSoundService.class);
startService(svc);
Android 清单:
<service android:enabled="true" android:name=".BackgroundSoundService" />
发生这种情况是因为服务仍然在 activity 中。要在多个 activity 绑定到服务时终止服务,您需要解除对所有服务的绑定,如 documentation 中所述:
The service lifecycle—from when it's created to when it's
destroyed—can follow either of these two paths:
A started service
The service is created when another component calls startService().
The service then runs indefinitely and must stop itself by calling
stopSelf(). Another component can also stop the service by calling
stopService(). When the service is stopped, the system destroys it.
A bound service
The service is created when another component (a client) calls
bindService(). The client then communicates with the service through
an IBinder interface. The client can close the connection by calling
unbindService(). Multiple clients can bind to the same service and
when all of them unbind, the system destroys the service. The service
does not need to stop itself.
These two paths are not entirely separate. You can bind to a service
that is already started with startService(). For example, you can
start a background music service by calling startService() with an
Intent that identifies the music to play. Later, possibly when the
user wants to exercise some control over the player or get information
about the current song, an activity can bind to the service by calling
bindService(). In cases such as this, stopService() or stopSelf()
doesn't actually stop the service until all of the clients unbind.
然后调用Context.stopService()
停止它:
context.stopService(new Intent(context, BackgroundSoundService.class));
我在所有活动中都使用服务来播放背景音乐,而且效果很好。问题是音乐会继续播放,即使我的应用程序处于后台(当用户使用主页按钮或后退按钮退出时)。我该如何解决?
服务classBackgroundSoundService
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.slow_shock);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return START_NOT_STICKY;
}
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() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
开始于
Intent svc = new Intent(this, BackgroundSoundService.class);
startService(svc);
Android 清单:
<service android:enabled="true" android:name=".BackgroundSoundService" />
发生这种情况是因为服务仍然在 activity 中。要在多个 activity 绑定到服务时终止服务,您需要解除对所有服务的绑定,如 documentation 中所述:
The service lifecycle—from when it's created to when it's destroyed—can follow either of these two paths:
A started service
The service is created when another component calls startService(). The service then runs indefinitely and must stop itself by calling stopSelf(). Another component can also stop the service by calling stopService(). When the service is stopped, the system destroys it.
A bound service
The service is created when another component (a client) calls bindService(). The client then communicates with the service through an IBinder interface. The client can close the connection by calling unbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. The service does not need to stop itself.
These two paths are not entirely separate. You can bind to a service that is already started with startService(). For example, you can start a background music service by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases such as this, stopService() or stopSelf() doesn't actually stop the service until all of the clients unbind.
然后调用Context.stopService()
停止它:
context.stopService(new Intent(context, BackgroundSoundService.class));