iOS 音頻播放後觸發鎖屏音樂樣式和觸發靈動島音樂播放默認樣式需要設置MPNowPlayingInfoCenter


import MediaPlayer


func setNowPlayingInfo() {
// .開啓遠程交互,只有開啓這個才能進行遠程操控 UIApplication.shared.beginReceivingRemoteControlEvents() // 獲取鎖屏中心 MPRemoteCommandCenter.shared() // 監聽遠程交互方法 MPRemoteCommandCenter.shared().playCommand.addTarget { [weak self] event in self?.playMusicMPRemote() return .success } MPRemoteCommandCenter.shared().pauseCommand.addTarget { [weak self] event in self?.playMusicMPRemote() return .success } MPRemoteCommandCenter.shared().nextTrackCommand.addTarget {event in return .success } MPRemoteCommandCenter.shared().previousTrackCommand.addTarget {event in return .success } // 廢棄 // let albumArt = MPMediaItemArtwork(image: backImageView.image!) let image = backImageView.image let albumArt = MPMediaItemArtwork.init(boundsSize: CGSize(width: (image?.size.width)!, height: (image?.size.height)!), requestHandler: { (size) -> UIImage in return image! }) DispatchQueue.global().asyncAfter(deadline: .now() + 1) { [weak self] in let Duration = Int(self?.audioPlayer.duration ?? 0 - (self?.audioPlayer.progress)!) let PlaybackTime = Int((self?.audioPlayer.progress)!) MPNowPlayingInfoCenter.default().nowPlayingInfo = [ MPMediaItemPropertyTitle: self?.dataModel.title ?? "", // 設置歌曲名 MPMediaItemPropertyArtist: "冥想", // 設置歌手名 MPMediaItemPropertyArtwork: albumArt, //設置封面的圖片 MPMediaItemPropertyPlaybackDuration: Duration, //剩餘時長 MPNowPlayingInfoPropertyPlaybackRate:1.0, //速率 MPNowPlayingInfoPropertyElapsedPlaybackTime: PlaybackTime,//當前播放時間 ] } }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章