[英]Play/Pause button not updating in lockscreen always it will show pause button in Swift4 ios
我有一個可以從 iOS 命令中心和鎖定屏幕播放的音頻播放器。 當我在我的應用程序中切換播放/暫停按鈕時,它應該通過更新 nowPlayingInfo (MPNowPlayingInfoCenter) 來更新命令中心 (MPRemoteCommandCenter) 中的播放/暫停按鈕。 但它沒有更新。
但是當我嘗試從鎖屏播放/暫停在我的應用程序切換按鈕中控制它的更新時,這工作正常。但類似地,當我從我的應用程序播放/暫停按鈕內部進行控制時。 它沒有更新鎖屏中的播放/暫停按鈕。總是顯示暫停按鈕。
我設置 Playbackrate 1.0 播放和 0.0 暫停。但它仍然沒有更新..
有些人可以給我任何建議或以下代碼中缺少的任何內容。
func setupLockScreenDisplay() {
var nowPlayingInfo = [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = self.currentSongName
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = self.currentAlbum == nil ? self.totalDurationTime : playerItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = NSNumber(value: MPNowPlayingInfoMediaType.audio.rawValue)
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.currentAlbum == nil ? self.storyPlayer.currentTime : CMTimeGetSeconds(player.currentTime())
// Set the metadata
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
MPNowPlayingInfoCenter.default().playbackState = .playing
}
下面的遠程命令代碼工作正常..我可以從鎖屏控制
func setupRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { event in
self.play()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { event in
self.pause()
return .success
}
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget { event in
self.nextPlay()
return .success
}
commandCenter.previousTrackCommand.isEnabled = true
commandCenter.previousTrackCommand.addTarget { event in
self.lastPlay()
return .success
}
commandCenter.togglePlayPauseCommand.isEnabled = true
}
這里是內部播放按鈕的代碼
func play() {
if self.currentAlbum == nil {
self.storyPlayer.play()
} else {
self.player.play()
}
self.isPlaying = true
NotificationCenter.default.post(name: Notification.Name(rawValue: kPlayerManagerChangePlayingStateRsp), object: nil)
setupLockScreenDisplay()
setupRemoteCommandCenter()
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
}
暫停按鈕內的代碼
func pause() {
if self.currentAlbum == nil {
self.storyPlayer.pause()
} else {
self.player.pause()
}
self.isPlaying = false
NotificationCenter.default.post(name: Notification.Name(rawValue: kPlayerManagerChangePlayingStateRsp), object: nil)
setupLockScreenDisplay()
setupRemoteCommandCenter()
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 0
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
}
在您的視圖中添加此代碼確實加載了 func 或應用程序委托
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
}
catch {
print(error)
}
以及您的后台模式功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.