MPNowPlayingInfoPropertyElapsedPlaybackTime未正确设置

时间:2015-07-28 10:05:57

标签: ios swift

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

2 个答案:

答案 0 :(得分:4)

我是通过以下方式完成的(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())

        }
  }

答案 1 :(得分:2)

Swift 3

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

你需要在暂停,跳过,速度变化等情况下触发它。

暂停的例子。

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

您不需要更新滑块的移动,只要您在nowPlayingInfo中设置了回放持续时间,锁定屏幕就会为您计算时间。