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,//当前播放时间 ] } }

 

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