【问题标题】:Stop Android Service when user presses the home button当用户按下主页按钮时停止 Android 服务
【发布时间】:2016-08-17 11:32:10
【问题描述】:

我有一个音乐服务,可以在应用程序的后台播放音乐。我希望音乐在应用程序的所有活动中继续播放,但在应用程序在后台运行时停止(即当用户转到另一个应用程序或按下主页按钮而不从正在运行的应用程序中删除应用程序时)

这是我的 MusicService 代码:

public class MusicService extends Service {
public static MediaPlayer player;

public IBinder onBind(Intent arg0) {
    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {


    player= MediaPlayer.create(this,R.raw.music1);
    player.start();
    player.setLooping(true);

    return super.onStartCommand(intent,flags,startId);

}

}

这是我清单中与音乐服务相关的部分:

<service android:name=".MusicService" android:stopWithTask="true" />

编辑:如果有人知道如何在没有服务的情况下播放背景音乐,那也没关系,只要在应用打开和按下主页按钮时关闭的整个过程中播放音乐。

【问题讨论】:

    标签: android background android-service audio-player android-music-player


    【解决方案1】:

    基本上你必须定义女巫活动是你的退出点,然后像这样编辑你的 onStartCommand

    private boolean playing; // use this var to determine if the service is playing
    public int onStartCommand(Intent intent, int flags, int startId) {
    
      String action = intent.getAction();
      if(action == ACTION_PLAY) {
        // No each time your start an activity start the service with ACTION_PLAY but in ACTION_PLAY process check if the player if not already runing
          if(!playing) {
             player= MediaPlayer.create(this,R.raw.music1);
             player.start();
             player.setLooping(true);
             // here you set playing to true
             playing = true;
         }
     } else if(action.equals(ACTION_STOP) {
            // Set playing to false
            playing = false;
           // This is just an exemple : Now here increase a delay little bit so that the player will not stop automaticaly after leaving activity
            new Handler().postDelayed(new Runnable(){
                   @override
                   public void run() {
                    // No before stoping the play service
                    // check playing if playing dont go further 
                     if(playing) return;
                    if(player!=null && player.isPlaying()) {
                        player.stop();
                        player.release();
                        player.reset(); // To avoid mediaPlayer has went away with unhandled error warning
                        player = null;
                        // And stop the service 
                        stopSelf();
                     }    
                  }
          },2500);
     }
    return START_STICKY;}
    

    现在如果你想开始玩,使用

    Intent intent = new Intent(context,YourService.class);
    intent.setAction(YourService.ACTION_PLAY);
    

    为了停止

    Intent intent = new Intent(context,YourService.class);
    intent.setAction(YourService.ACTION_STOP);
    

    别忘了定义两个动作内容字段

    如果您不想定义退出点,您可以定义一个布尔值来确定您的服务正在使用中,因此不会被延迟处理程序停止 现在每次活动启动时,使用操作 ACTION_PLAY 启动服务 一旦它停止使用 ACTION_STOP 启动服务,这将确保每个活动都能够启动和停止播放器 也不要忘记调整延迟
    希望对你有帮助

    【讨论】:

    • 但是如果用户在任何随机活动中退出怎么办?那时我该如何处理停止音乐?
    【解决方案2】:

    我认为这个问题的最佳解决方案是停止每个ActivityonPause() 中的音​​乐并开始每个ActivityonResume() 中的音​​乐。您将遇到的问题是,当您的应用程序从一个Activity 切换到另一个时,您的音乐会卡顿。为了解决这个问题,您应该将Runnable(停止音乐)发布到onPause() 中的Handler,但发布它以使其不会立即运行。它应该延迟大约 200 毫秒。在onResume() 中,取消Runnable 使其不运行。这将防止口吃,但在用户按下 HOME 按钮 200 毫秒后停止音乐。


    另一种选择是不使用Service,而只是将MediaPlayer 实例保留在Application 类中。你仍然想在onPause()onResume() 中停止和开始音乐,但是你可以通过调用Application 类的一些方法来更直接地做到这一点。您需要创建一个自定义 Application 类,该类 extends Application 并将其添加到您的清单中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      相关资源
      最近更新 更多