Audio playback lock screen control not displaying
我正在尝试在锁定屏幕上显示音频控件,但问题是音频控件在锁定屏幕上不显示任何内容。我已经启用了后台模式,并且音频在后台播放。
在应用委托类中,当我的应用启动时,我设置了我的音频会话
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
setupAudioSession() UIApplication.shared.beginReceivingRemoteControlEvents() return true } func setupAudioSession(){ do { } |
在我的主控制器中,我在播放音频后调用 setupLockScreen 函数。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func setupLockScreen(){
let commandCenter = MPRemoteCommandCenter.shared() commandCenter.playCommand.isEnabled = true commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in if self.player?.rate == 0.0 { self.player?.play() return .success } return .commandFailed } var nowPlayingInfo = [String : Any]() nowPlayingInfo[MPMediaItemPropertyTitle] =”My Song” nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = audioplayerItem.duration.seconds nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = audioplayerItem.asset.duration.seconds nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player?.rate MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo |
我阅读了很多文章,并查看了 Stack Overflow 中的所有问题,但没有运气。
问题是您的 nowPlayingInfo 没有显示在锁定屏幕上还是您添加的控件没有响应?
要让您的 nowPlayingInfo 出现在锁定屏幕上,您需要执行有关处理外部玩家事件通知的答案中列出的一些操作。一个是有一个不可混合的音频会话(你已经有了 AVAudioSessionCategoryPlayback),另一个是正在播放音频或最近才停止播放音频。你的应用真的在播放音频吗?
您拥有的背景音频是一种派生要求,因为 lockscreen=>background=>播放音频需要背景音频,所以我应该将其添加到另一个答案中。
如果问题是锁屏控件未启用/没有响应,那么请尝试添加 pauseCommand。 playCommand/pauseCommand 对似乎是接收锁屏/外部播放器命令的最低要求。
附言你的 MPNowPlayingInfoPropertyElapsedPlaybackTime 看起来不对。不应该是
1
|
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(audioplayerItem.currentTime)
|
- 谢谢你。”不可混合的音频会话”为我解决了这个问题。当我在应用程序委托中将其作为音频会话的选项时,控件没有出现。
这样做(这是swift 3):
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
override func viewDidLoad() {
super.viewDidLoad() UIApplication.shared.beginReceivingRemoteControlEvents() commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in } |
This answer found HERE
- 这和他做的有什么区别?另外,它只是重复另一个答案的代码;不要那样做。一个评论就足够了。
来源:https://www.codenong.com/49440503/