简体   繁体   English

(iOS MPMusicPlayerNotification)如何在应用程序处于后台时接收通知

[英](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.

相关问题 当APP在iOS中收到外部通知时检测背景模式 - Detect BACKGROUND mode when APP receive external notification in iOS 应用被杀死时,有没有办法接收通知? 那就是当应用程序无法在iOS的前台或后台运行时 - Is there a way to receive notification when app is killed? That is when app is not working in foreground or background in iOS 应用程序在后台时的 iOS 通知 - iOS Notification when App is in Background 当应用被杀死而不在IOS的后台运行时如何获取通知有效载荷 - How to get notification payload when app is killed not running in background in IOS 后台iOS应用程序是否收到显示器将要睡眠的通知? - Does a background iOS app receive notification that the display is going to sleep? 在 ios 中接收推送通知时后台执行不起作用 - background execution is not working when receive push notification in ios iOS应用程序在后台时的GCM推送通知 - GCM push notification when iOS app is in the background iOS 应用程序在后台时无法接收 FCM 推送通知 - Unable to receive FCM push notification when iOS application is in background 当应用被杀死时,如何在ios中单击通知时接收数据((react-native-fcm模块在iOS中获取推送通知) - How to receive data on click of notification in ios, when app is killed ,( react-native-fcm module to get push notification in IOS) 应用程序在后台时跳过或禁用iOS通知 - Skip or Disable iOS Notification When App Is In Background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM