使用后台服务播放音乐 [英] play music with background service

查看:113
本文介绍了使用后台服务播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过后台服务播放音乐.首先,我在 MainActivity 中有一个 toggle按钮,用于播放和暂停音乐.我也创造了 BackgroundSoundService 仅用于在所有活动中播放音乐,而不能在后台播放:

I'm trying to play music with a background service. Firstly, I have a toggle button in MainActivity for playing and pausing music. I also created BackgroundSoundService just for playing music in all activities and not to play in the background:

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

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

My problem happens when I pause the music and play it again. The music starts from the beginning when I want it to continue from where it left off. My second problem is that I don't want to play music in the background, I want stop music when the app is in background.

推荐答案

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

I'm not sure what your onPause() and onStop() are meant to do. However when you start a service for the first time using Context.startService() the onCreate() method of the service is called and then onStartCommand() is called and later when you call startService() again only the onStartCommand() is called. So for whatever reason if you want to play the sound in a service and pause that in the very same service, you need to provide the service an Action that specifies the action you want to do.

因此,在您要告诉服务播放声音的活动中:

So in your activity when you want to tell the service to play the sound:

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()方法被调用,服务实际上被破坏了.

and when you really need to stop the service meaning Destroy the service, call context.stopService() and only then onDestroy() method is called and the service is really destroyed.

这篇关于使用后台服务播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆