I'm trying to write in Swift 2.2 (Xcode 7) an iOS 9 application that can play audio. All my files are read from internet, so i choosed StreamingKit and can be played in background from multiple ViewController, so i created a singleton that store the audio player instance.
I successfully make the player playing audio in background and display info in NowPlayingInfoCenter, but i can't get duration to work i got this result
When i start playing my file i initialize the MPNowPlayingInfo with theses lines
var nowPlayingInfo : Dictionary<String, AnyObject> = [
MPNowPlayingInfoPropertyPlaybackRate : 1
]
if audioTitle != nil {
nowPlayingInfo[MPMediaItemPropertyTitle] = audioTitle
}
else {
nowPlayingInfo[MPMediaItemPropertyTitle] = NSLocalizedString("app_name", comment: "")
}
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo
And when buffering is done i set additionnals informations with theses lines
var nowPlayingInfo = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
//Time
nowPlayingInfo!["MPNowPlayingInfoPropertyElapsedPlaybackTime"] = NSNumber(double: self.audioPlayer.progress)
nowPlayingInfo!["MPMediaItemPropertyPlaybackDuration"] = NSNumber(double: self.audioPlayer.duration)
//Is playing
nowPlayingInfo!["MPNowPlayingInfoPropertyPlaybackRate"] = NSNumber(double: self.isPlaying ? 1.0 : 0.0)
//State
if isError {
nowPlayingInfo![MPMediaItemPropertyArtist] = NSLocalizedString("player_error", comment: "")
} else if isLoading {
nowPlayingInfo![MPMediaItemPropertyArtist] = NSLocalizedString("player_loading", comment: "")
} else {
nowPlayingInfo![MPMediaItemPropertyArtist] = NSString(string: self.audioArtist!)
}
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo
An example (retrieved from debugger) of value passed in my NSNumber constructor above : progress = 1.892426303854875 duration = 62.85865814546887
I also tried to pass formatted NSString like this answer with for example theses values below retrieved in debugger : progress = 0:02 duration = 1:03
The player duration and seek bar still like the screenshot
Anyone can help me please ? Thanks