2015-07-28 16 views
6

Sto tentando di impostare correttamente il tempo di riproduzione trascorso. Quando viene chiamata la funzione player.seek o la traccia viene messa in pausa, il tempo trascorso per l'avvio dell'infocenter non viene aggiornato. Avvio nowplayinginfocenter con setNowPlaying() e quindi richiamo setNowPlayingCurrentTime quando si cerca di aggiornarlo nel centro informazioni.MPNowPlayingInfoPropertyElapsedPlaybackTime non impostato correttamente

Tuttavia, quando questo è chiamato il tempo trascorso viene resettato a 0.

Qualsiasi consiglio sarebbe molto utile per favore.

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] 
} 

risposta

3

L'ho fatto nel modo seguente (Swift 2): il tasto era correttamente impostato sugli attributi su riproduzione/pausa.

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

Swift 3

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

Si avrebbe bisogno di sparare a questa pausa, saltare, cambi di velocità, ecc

Esempio di pausa.

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

Non è necessario aggiornare il movimento dello scrubber, la schermata di blocco calcola i tempi per voi fino a quando si è impostato riprodurre durata nel nowPlayingInfo.

Problemi correlati