Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPNowPlayingInfoPropertyElapsedPlaybackTime not setting correctly

Tags:

ios

swift

I'm trying to correctly set the elapsed play time. When the player.seek function is called or the track is paused, the nowplayinginfocenter elapsed time is not updated. I init nowplayinginfocenter with setNowPlaying() and then call setNowPlayingCurrentTime when the track is seeked to update it in the info center.

However, when this is called the elapsed time gets reset to 0.

Any advice would be very useful please.

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]
}
like image 726
Phil Hudson Avatar asked Jul 28 '15 10:07

Phil Hudson


2 Answers

I did it the following way (Swift 2) - the key was correctly setting attributes on play/pause.

    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())

        }
  }
like image 68
Phil Hudson Avatar answered Nov 15 '22 14:11

Phil Hudson


Swift 3

    if player.currentItem != nil {
        MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
        MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1
    }

You would need to fire this at pause, skip, speed changes, etc.

Example of pause.

    if player.currentItem != nil {
        MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
        MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 0
    }

You do not need to update the movement of the scrubber, the lock screen will calculate times for you as long as you have set play back duration in nowPlayingInfo.

like image 33
skymook Avatar answered Nov 15 '22 15:11

skymook