首页 > 其他分享 >iOS 音频播放后触发锁屏音乐样式和触发灵动岛音乐播放默认样式需要设置MPNowPlayingInfoCenter

iOS 音频播放后触发锁屏音乐样式和触发灵动岛音乐播放默认样式需要设置MPNowPlayingInfoCenter

时间:2023-03-30 15:13:44浏览次数:42  
标签:触发 return MPRemoteCommandCenter 样式 self let shared 播放 image


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

 

标签:触发,return,MPRemoteCommandCenter,样式,self,let,shared,播放,image
From: https://www.cnblogs.com/ljcgood66/p/17272743.html

相关文章