使用后台服务播放音乐

play music with background service

我正在尝试使用后台服务播放音乐。首先,我在 MainActivity 中有一个 toggle button 用于播放和暂停音乐。我也创造了 BackgroundSoundService 仅用于在所有活动中播放音乐而不是在后台播放:

public class BackgroundSoundService extends Service {

private static final String TAG = "BackgroundSoundService";
MediaPlayer player;

public IBinder onBind(Intent arg0) {
    Log.i(TAG, "onBind()" );
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    player = MediaPlayer.create(this, R.raw.vaporv2);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);
    Log.i(TAG, "onCreate() , service started...");

}
public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return Service.START_STICKY;
}

public IBinder onUnBind(Intent arg0) {
    Log.i(TAG, "onUnBind()");
    return null;
}
public void onStop() {
    Log.i(TAG, "onStop()");
}
public void onPause() {
    if(player!=null && player.isPlaying()){
        player.pause();
    }
    Log.i(TAG, "onPause()");
}
@Override
public void onDestroy() {
    player.stop();
    player.release();
    Log.i(TAG, "onCreate() , service stopped...");
}

@Override
public void onLowMemory() {
    Log.i(TAG, "onLowMemory()");
}
  }

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     MusicButton = (ToggleButton) findViewById(R.id.toggleButton);

    MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (MusicButton.isChecked()) {
                //mediaPlayer.pause();
                Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
                startService(myService);
            } else {
                //mediaPlayer.start();
                Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
                stopService(myService);
            }
        }
    });

我的问题发生在我暂停音乐并再次播放时。当我想让它从停止的地方继续播放时,音乐会从头开始。我的第二个问题是我不想在后台播放音乐,我想在应用程序处于后台时停止音乐。

为了防止在后台播放 Activity 暂停然后暂停你在那里服务并从你离开的地方继续在首选项中保存当前位置然后在需要时检索它

我不确定您的 onPause()onStop() 是什么意思。但是,当您第一次使用 Context.startService() 启动服务时,将调用服务的 onCreate() 方法,然后调用 onStartCommand() ,稍后当您再次调用 startService() 时,只会调用onStartCommand() 被调用。因此,无论出于何种原因,如果您想在服务中播放声音并在同一服务中暂停声音,您需要为服务提供一个 Action 来指定您要执行的操作。

所以在你的 activity 当你想告诉服务播放声音时:

String action = "PLAY";
Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
myService.setAction(action);
startService(myService);

并暂停声音:

String action = "PAUSE";
Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
myService.setAction(action);
startService(myService);  

并在您服务的 onStartCommand() 方法中:

if (intent.getAction().equals("PLAY")) {
    // resume the sound
}
if (intent.getAction().equals("PAUSE")) {
    // pause the sound
}

当你真的需要停止服务时Destroy服务,调用context.stopService()然后才调用onDestroy()方法并且服务被真正销毁。