I am trying to correctly set the elapsed playback time. When the player.seek function is called or the pause is paused, the current time of nowplayinginfocenter is not updated. I init nowplayinginfocenter using setNowPlaying (), and then calls setNowPlayingCurrentTime when the track is requested to update it in the information center.
However, when it is called, elapsed time gets reset to 0.
Any advice would be very helpful.
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] }
source share