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,//當前播放時間 ] } }