2015-07-28 155 views
6

我在嘗試正確設置播放時間。當player.seek函數被調用或音軌暫停時,nowplayinginfocenter經過的時間不會更新。我現在使用setNowPlaying()來初始化信息中心,然後在信息中心查找軌道以更新它時調用setNowPlayingCurrentTime。MPNowPlayingInfoPropertyElapsedPlaybackTime設置不正確

但是,如果這就是所謂的經過時間被重置爲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] 
} 

回答

3

我這樣做了以下方法(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()) 

     } 
    } 
2

斯威夫特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播放時間。