我正在尝试正确设置已播放的播放时间.当调用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())
- }
- }