ios – MPNowPlayingInfoPropertyElapsedPlaybackTime未正确设置

前端之家收集整理的这篇文章主要介绍了ios – MPNowPlayingInfoPropertyElapsedPlaybackTime未正确设置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试正确设置已播放的播放时间.当调用player.seek功能或暂停曲目时,不会更新nowplayinginfocenter经过的时间.我现在使用setNowPlaying()初始化inffinenter,然后在寻求在信息中心更新它时调用setNowPlayingCurrentTime.

但是,当调用此时,经过的时间将重置为0.

任何建议都非常有用.

  1. private func setNowPlaying(track: Track) {
  2. //set now playing info center
  3. if NSClassFromString("MPNowPlayingInfoCenter") != nil {
  4. //artwork
  5. var url = NSURL(string: track.artworkUrl!)
  6. var data = NSData(contentsOfURL: url!)
  7. var image = UIImage(data: data!)
  8. var albumArt = MPMediaItemArtwork(image: image)
  9.  
  10.  
  11. var songInfo: NSMutableDictionary = [
  12. MPMediaItemPropertyTitle: track.title!,MPMediaItemPropertyArtwork: albumArt,MPMediaItemPropertyArtist: track.userName!,MPMediaItemPropertyPlaybackDuration: track.duration!,MPNowPlayingInfoPropertyPlaybackRate: 0
  13. ]
  14. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as NSObject as! [NSObject : AnyObject]
  15. }
  16. if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,error: nil)) {
  17. println("Receiving remote control events")
  18. UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
  19. } else {
  20. println("Audio Session error.")
  21. }
  22.  
  23. }
  24.  
  25. private func setNowPlayingCurrentTime(track: Track,time: Float64) {
  26.  
  27. var songInfo: NSDictionary = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
  28. songInfo.mutableCopy().setValue(Double(time),forKey: MPNowPlayingInfoPropertyElapsedPlaybackTime)
  29. println("test")
  30. println(songInfo.mutableCopy().valueForKey(MPNowPlayingInfoPropertyElapsedPlaybackTime))
  31. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo.mutableCopy() as! NSObject as! [NSObject : AnyObject]
  32. }

解决方法

我是通过以下方式完成的(Swift 2) – 关键是在播放/暂停时正确设置属性.
  1. func play() {
  2.  
  3. if self.player.currentItem != nil {
  4. player.play()
  5. //mpnowplaying info center
  6.  
  7. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
  8. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1
  9.  
  10. } else {
  11. loadTrackToPlayer()
  12. player.play()
  13. //mpnowplaying info center
  14. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
  15. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1
  16. }
  17. }
  18.  
  19. func pause() {
  20. if self.player.currentItem != nil {
  21. player.pause()
  22. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 0
  23. MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
  24.  
  25. }
  26. }

猜你在找的iOS相关文章