在Spotify和Google Music等后台播放音乐时如何取消通知 [英] How to make notification dismissible when playing music in background like in Spotify and Google Music

查看:482
本文介绍了在Spotify和Google Music等后台播放音乐时如何取消通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spotify和Google Music允许用户在后台播放音乐,也可以取消媒体通知. 将这些应用程序之一移至后台,然后暂停播放时,便可以关闭媒体通知.但是,如果您不关闭通知,那么它会长时间处于暂停状态,系统将不会终止音频服务",因为该通知始终可见,并会立即对播放/暂停做出反应.

Spotify and Google Music allows a user to play music in background allowing also the media notification to be dismissible. When you move one of these apps to background, then pause the playback, you are able to dismiss the media notification. However if you don't dismiss the notification, so it is left in paused state for long time, the System won't kill the "audio service" as the notification is always visible and immediately react to play/pause.

据我所知,不可能使用前台服务来关闭通知,我们需要调用stopForeground(false),这将使系统终止该服务.

As far as I know it is not possible using foreground services as in order to dismiss the notification, we'd need to call stopForeground(false), which would let system to kill the service.

这是我们现在在应用程序中的操作方式:

Here is how we do it right now, in our app:

开始播放时,我们使用以下代码将MusicService作为前台服务运行:

When playback is started, we run MusicService as a foreground service with following code:

startForeground(mediaNotification)

然后,当用户离开应用程序时,他可以通过媒体通知控制播放.由于该通知与MusicService紧密相关,因此用户只有在我们致电以下信息后才能将其关闭:

Then, when user leave the app, he is able to control the playback via media notification. As the notification is tightly coupled with the MusicService, user won't be able to dismiss it until we call:

stopForeground(false)

这就是为什么我们在用户按下暂停按钮时调用此方法的原因. 但是,这构成了服务背景,因此系统会在短时间内将其杀死.

That's why we call this method when user press pause button. However this makes a Service background, so system will kill it in short period of time.

Spotify和Google音乐如何解决?实现它的方法是什么?

How Spotify and Google Music could workaround it? What is the way to achieve it?

推荐答案

结合Pouya的答案和一些独立研究,我有一个代码段,其行为与使用OOTB MediaPlayer的Spotify等应用程序非常接近.可能有一个更优雅的解决方案,但这是一个快速而肮脏的版本-我要发布的代码太多了,因此有一个概述:

Using a combination of Pouya's answer and some independent research, I have a code snippet that behaves fairly close to apps like Spotify using the OOTB MediaPlayer. There is probably a more elegant solution, but this is the quick and dirty version - I have too much code scattered about to post, so have an overview instead:

当媒体播放器暂停时,我运行两段代码.首先,更新通知栏后,我使用以下命令将其从前台服务降级为后台服务:

When the mediaplayer is paused, I run two pieces of code. First, after updating the notification bar I demote it from a foreground service to a background one with:

public void demoteNotifyBar() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            stopForeground(STOP_FOREGROUND_DETACH);
        else
            stopForeground(false);
    }

然后我启动一个新计时器,该计时器每秒重新制作一次通知栏:

and then I start a new timer, that remakes the notification bar once a second:

// Put this in the onCreate method
pauseRefresh = new Runnable() {
            @Override
            public void run() {
                // Check if media is playing to prevent race conditions
                if (!isPlaying()) {
                    startForeground(FOREGROUND_SERVICE, notification);
                    demoteNotifyBar();
                    handler.postDelayed(pauseRefresh, 1000);
                }
            }
        };

// Call this wherever you're handling the pause button
pauseRefresh.run();

要取消自动刷新,您需要在重新制作通知栏并将媒体从暂停切换到播放之前调用此代码(是的,您可以忽略此选项来切换音频,但是请阅读以下内容以了解原因)这是必需的):

To cancel the auto-refresh, you'll need to call this bit of code before you remake the notification bar and toggle the media from pause to playing (yes, you can ignore this for toggling audio, but read below for why this is needed):

handler.removeCallbacks(pauseRefresh);

您还需要侦听具有删除意图的滑动关闭,以防止在用户关闭通知时重新生成通知.只需使用相同的代码来取消用户单击播放时使用的计时器.

You will also need to listen for the swipe dismiss with a delete intent, to prevent the notification from remaking itself when the user dismisses it; simply use the same code to cancel the timer that is used when the user clicks play.

这篇关于在Spotify和Google Music等后台播放音乐时如何取消通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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