Android 使用后台服务播放音乐

Android 使用后台服务播放音乐,android,android-mediaplayer,background-service,Android,Android Mediaplayer,Background Service,我正在尝试使用后台服务播放音乐。首先,我在main活动中有一个切换按钮,用于播放和暂停音乐。我还创造了 BackgroundSoundService仅用于在所有活动中播放音乐,不在后台播放: public class BackgroundSoundService extends Service { private static final String TAG = "BackgroundSoundService"; MediaPlayer player; public IBinder onB

我正在尝试使用后台服务播放音乐。首先,我在
main活动
中有一个
切换按钮
,用于播放和暂停音乐。我还创造了
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()");
}
  }
main活动

@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);
            }
        }
    });

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

要防止在后台播放,请在活动暂停时覆盖,然后在那里暂停服务,并恢复您离开的位置,将当前位置保存在首选项中,然后在需要时检索。

要防止在后台播放,请在活动暂停时覆盖,然后暂停您在那里的服务,然后重新开始将当前位置保存在首选项中,然后在需要时检索它。我不确定您的
onPause()
onStop()
要做什么。但是,当您第一次使用
Context.startService()
启动服务时,将调用该服务的
onCreate()
方法,然后调用
onStartCommand()
,之后再次调用
startService()
时,仅调用
onStartCommand()
。因此,无论出于何种原因,如果您想在服务中播放声音并在同一服务中暂停,您都需要为服务提供一个
操作
,该操作指定您要执行的操作

因此,在您的活动中,当您想让服务播放声音时:

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);  
要暂停声音:

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
}

当你真的需要停止服务意味着
销毁服务时,调用
context.stopService()
,然后调用
ondestory()
方法,服务才真正被销毁。

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

因此,在您的活动中,当您想让服务播放声音时:

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);  
要暂停声音:

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
}

当您确实需要停止服务时,请调用
context.stopService()
,然后再调用
onDestroy())
方法被调用,服务真的被破坏了。

检查此代码用于在后台停止音乐。检查此代码用于在后台停止音乐。非常感谢它的工作,但音乐仍在后台播放。我将此代码放入
Intent myService=new Intent(MainActivity.this,BackgroundSoundService.class);getApplicationContext().startService(myService);
在我的
onDestroy()中)
在Main Activity中,音乐仍在播放background@S.Queroane我不确定你的问题到底是什么。你想在你的服务被破坏时停止音乐吗?我想在用户进入后台时停止音乐(例如,当用户单击“主页”按钮时)当在我的应用程序中返回时,音乐将从它离开的地方继续off@S.Queroane然后重写
onResume()
onPause()
活动的方法。当活动恢复时,请像之前一样恢复声音,当活动暂停时,请以相同的方式暂停声音。非常感谢它的工作,但音乐仍在后台播放。我将此代码
Intent myService=new Intent(main activity.this,BackgroundSoundService.class);getApplicationContext().startService(myService);
在my
onDestroy()
在MainActivity中播放音乐background@S.Queroane我不知道你到底有什么问题。你想在你的服务被破坏时停止音乐吗?我想在用户进入后台时停止音乐(例如,当用户单击“主页”按钮时)和在我的应用程序中返回时,音乐将从其离开的位置继续off@S.Queroane然后覆盖活动的
onResume()
onPause()
方法。当活动恢复时,像之前一样恢复声音,当活动暂停时,以同样的方式暂停声音。