[英](iOS MPMusicPlayerNotification) how to receive notification when app is in the background
I'm using [MPMusicPlayerController ipodMusicPlayer]
to play songs in my application 我正在使用[MPMusicPlayerController ipodMusicPlayer]
在我的应用程序中播放歌曲
so that the song experience doesn't get interrupted even if the app goes to the background. 这样,即使应用程序进入后台,歌曲体验也不会中断。
I've registered MPMusicPlayerNowPlayingItemDidChangeNotification
in my app delegate, 我在我的app委托中注册了MPMusicPlayerNowPlayingItemDidChangeNotification
,
so that whenever the music changes I can stop the player for a while, do something, and 因此,每当音乐改变时,我可以停止播放器一段时间,做一些事情,并且
then re-play it again. 然后重新播放它。
The problem is, I need to receive the notification even when the app is in the background 问题是,即使应用程序在后台,我也需要收到通知
and my app is the only application that's using the ipodMusicPlayer
. 我的应用程序是唯一使用ipodMusicPlayer
应用程序。 (that is, default music app isn't open). (即默认音乐应用程序未打开)。
I've been playing around with applicationDidEnterBackground
, but I just can't get it to work. 我一直在使用applicationDidEnterBackground
,但我无法让它工作。
How should I achieve this? 我该怎么做到这一点?
Thank You in advance 先感谢您
I found a solution for this. 我找到了解决方案。
Declare all your notification receivers in your app delegate, along with 在您的应用代理中声明所有通知接收器,以及
[[MPMusicPlayerController iPodMusicPlayer] beginGeneratingPlaybackNotifications];
Then, initialize a CLLocationManager object and start receiving location changes. 然后,初始化CLLocationManager对象并开始接收位置更改。
This way, your app will not be suspended even if it goes to the background, 这样,您的应用即使转到后台也不会被暂停,
and your appdelegate will keep receiving playbacknotifications. 并且您的appdelegate将继续接收回放通知。
However, if will only work with applications using GPS features. 但是,如果只适用于使用GPS功能的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.