在 iOS10+ 中播放完成后,如何让播放器控件从锁屏中消失?
How to vanish player controls from lockscreen after the playback has finished in iOS10+?
在后台模式下播放音频时,播放器控件会出现在锁定屏幕上。音频停止时如何将其删除?如果尝试设置:
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
播放器仍在锁定屏幕上,但字段 artist/song 为空
UPD(我的音频会话代码):
在 AppDelegate 中:
func setupAudioSession() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
}
在播放器中 class:
private func clearRemotePlayerInfo() { // call after stop button pressed
try? AVAudioSession.sharedInstance().setActive(false)
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
}
TL;DR
Github 上的示例:https://github.com/JakubMazur/SO52243428
您不应该将 nil
分配给此 nowPlayingInfo
。
你应该做的是:
- 停止播放(没有必要,但最好清理您创建的内容)
- 将您的会话设置为不活动
- 清除
nowPlayingInfo
因此代码将如下所示:
self.player?.stop() // Where self.player is AVAudioPlayer
try? self.session?.setActive(false, with: .notifyOthersOnDeactivation) // Where self.session is AVAudioSession. You should do it with do-catch to good error catching.
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
它会像这样:
编辑:
我写了一个尽可能简单的例子来试试看。它在 Github https://github.com/JakubMazur/SO52243428 上可用。请随时检查它是否符合您的情况。
对我有用。
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "my video"
if let image = UIImage(named: "video image") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
let playerItem = self.player?.currentItem
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem?.currentTime().seconds
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem?.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.player?.rate
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
UIApplication.shared.beginReceivingRemoteControlEvents()
}
func closeVideo() {
self.player?.pause()
let sessionAV = AVAudioSession.sharedInstance()
try? sessionAV.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
UIApplication.shared.endReceivingRemoteControlEvents()
}
在后台模式下播放音频时,播放器控件会出现在锁定屏幕上。音频停止时如何将其删除?如果尝试设置:
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
播放器仍在锁定屏幕上,但字段 artist/song 为空
UPD(我的音频会话代码):
在 AppDelegate 中:
func setupAudioSession() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
}
在播放器中 class:
private func clearRemotePlayerInfo() { // call after stop button pressed
try? AVAudioSession.sharedInstance().setActive(false)
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
}
TL;DR
Github 上的示例:https://github.com/JakubMazur/SO52243428
您不应该将 nil
分配给此 nowPlayingInfo
。
你应该做的是:
- 停止播放(没有必要,但最好清理您创建的内容)
- 将您的会话设置为不活动
- 清除
nowPlayingInfo
因此代码将如下所示:
self.player?.stop() // Where self.player is AVAudioPlayer
try? self.session?.setActive(false, with: .notifyOthersOnDeactivation) // Where self.session is AVAudioSession. You should do it with do-catch to good error catching.
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
它会像这样:
编辑:
我写了一个尽可能简单的例子来试试看。它在 Github https://github.com/JakubMazur/SO52243428 上可用。请随时检查它是否符合您的情况。
对我有用。
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "my video"
if let image = UIImage(named: "video image") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
let playerItem = self.player?.currentItem
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem?.currentTime().seconds
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem?.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.player?.rate
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
UIApplication.shared.beginReceivingRemoteControlEvents()
}
func closeVideo() {
self.player?.pause()
let sessionAV = AVAudioSession.sharedInstance()
try? sessionAV.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
UIApplication.shared.endReceivingRemoteControlEvents()
}