【问题标题】:play music with background service使用后台服务播放音乐
【发布时间】:2017-10-19 20:33:30
【问题描述】:

我正在尝试使用后台服务播放音乐。首先,我在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);
            }
        }
    });

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

【问题讨论】:

  • 查看link,了解在后台停止音乐。

标签: android android-mediaplayer background-service


【解决方案1】:

我不确定您的 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()方法并且服务真的被销毁了。

【讨论】:

  • 非常感谢它的工作,但音乐仍在后台播放,我将此代码 Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); getApplicationContext().startService(myService); 放在我的 onDestroy() 中的 mainactivity
  • 请在后台播放音乐
  • @S.Queroane 我不确定你的问题到底是什么。您希望在您的服务被销毁时停止播放音乐吗?
  • 我想在用户进入后台时停止音乐(例如,当用户点击主页按钮时)以及在我的应用程序中返回时音乐从中断处继续
  • @S.Queroane 然后覆盖您的活动的onResume() 和onPause() 方法。当活动恢复时,像以前一样恢复声音,当活动暂停时,以同样的方式暂停声音。
【解决方案2】:

为了防止在 Activity 暂停时在后台覆盖播放,然后暂停您的服务并恢复您离开的位置,将当前位置保存在首选项中,然后在需要时检索它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多