我正在尝试正确设置已播放的播放时间.当调用player.seek功能或暂停曲目时,不会更新Nowplayinginfocenter经过的时间.我现在使用setNowPlaying()初始化inffinenter,然后在寻求在信息中心更新它时调用setNowPlayingCurrentTime.
但是,当调用此时,经过的时间将重置为0.
任何建议都非常有用.
private func setNowPlaying(track: Track) { //set Now playing info center if NSClassFromString("MPNowPlayingInfoCenter") != nil { //artwork var url = NSURL(string: track.artworkUrl!) var data = NSData(contentsOfURL: url!) var image = UIImage(data: data!) var albumart = MPMediaItemArtwork(image: image) var songInfo: NSMutableDictionary = [ MPMediaItemPropertyTitle: track.title!,MPMediaItemPropertyArtwork: albumart,MPMediaItemPropertyArtist: track.userName!,MPMediaItemPropertyPlaybackDuration: track.duration!,MPNowPlayingInfoPropertyPlaybackRate: 0 ] MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo = songInfo as NSObject as! [NSObject : AnyObject] } if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,error: nil)) { println("Receiving remote control events") UIApplication.sharedApplication().beginReceivingRemoteControlEvents() } else { println("Audio Session error.") } } private func setNowPlayingCurrentTime(track: Track,time: Float64) { var songInfo: NSDictionary = MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo songInfo.mutablecopy().setValue(Double(time),forKey: MPNowPlayingInfoPropertyElapsedplaybackTime) println("test") println(songInfo.mutablecopy().valueForKey(MPNowPlayingInfoPropertyElapsedplaybackTime)) MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo = songInfo.mutablecopy() as! NSObject as! [NSObject : AnyObject] }
解决方法
我是通过以下方式完成的(Swift 2) – 关键是在播放/暂停时正确设置属性.
func play() { if self.player.currentItem != nil { player.play() //mpNowplaying info center MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyElapsedplaybackTime] = CMTimeGetSeconds(player.currentTime()) MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1 } else { loadTrackToPlayer() player.play() //mpNowplaying info center MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyElapsedplaybackTime] = CMTimeGetSeconds(player.currentTime()) MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1 } } func pause() { if self.player.currentItem != nil { player.pause() MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 0 MPNowPlayingInfoCenter.defaultCenter().NowPlayingInfo![MPNowPlayingInfoPropertyElapsedplaybackTime] = CMTimeGetSeconds(player.currentTime()) } }