gpt4 book ai didi

ios - 在后台检测 MPMusicPlayerController 通知

转载 作者:IT王子 更新时间:2023-10-29 05:38:27 25 4
gpt4 key购买 nike

我有一个用 swift 编写的音乐应用程序,我正在使用 MPMusicPlayerController.systemMusicPlayer,也尝试过 applicationMusicPlayer。

如预期的那样,背景音乐播放良好。

当我的应用程序在后台运行时,我需要播放状态更改通知,以便我的应用程序可以确定要播放的下一首歌曲。

我的 viewDidLoad 方法中有以下内容。如果我的应用程序在前台,我的方法“playbackChanged”将以所有正确的状态被调用。

 NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "playbackChanged",
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification,
object: MPMusicPlayerController.systemMusicPlayer()
)

如果我的应用程序在后台,我什么也得不到。我记得在 iOS 7 和使用 iPodMusicPlayer(现已在 iOS 8 中弃用)时,我会在后台正确收到这些通知。

任何想法出了什么问题?

最佳答案

在 MusicPlayer 上调用 beginGeneratingPlaybackNotifications() 似乎对我有用。

 MPMusicPlayerController.systemMusicPlayer().beginGeneratingPlaybackNotifications()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "playbackChanged",
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification,
object: MPMusicPlayerController.systemMusicPlayer()
)

更新:不幸的是,MPMusicPlayerNotifications 无法在后台可靠地工作。

关于ios - 在后台检测 MPMusicPlayerController 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26371233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com