I've set up control center for my AVAudioPlayer
. Everything works great, except for the progress bar in control center, which continues counting up whenever the AVAudioPlayer
is paused. How can I fix this? Here's what I have so far:
func updateNowPlayingCenter (title: String, artist: String, currentTime: AnyObject, songLength: AnyObject, PlaybackRate: Double){
let image:UIImage = UIImage(named: title)!
let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
var songInfo: Dictionary <NSObject, AnyObject> = [
MPMediaItemPropertyTitle as NSObject: title as AnyObject,
MPMediaItemPropertyArtist as NSObject: artist as AnyObject,
MPMediaItemPropertyArtwork as NSObject: artwork,
MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: currentTime,
MPMediaItemPropertyPlaybackDuration as NSObject: songLength,
MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]
}
override func remoteControlReceived(with event: UIEvent?) {
if event?.type == UIEventType.remoteControl {
if event?.subtype == UIEventSubtype.remoteControlPlay {
audioPlayer.play()
} else if event?.subtype == UIEventSubtype.remoteControlPause {
audioPlayer.pause
} else if event?.subtype == UIEventSubtype.remoteControlNextTrack {
nextButtonTapped()
} else if event?.subtype == UIEventSubtype.remoteControlPreviousTrack {
previousButtonTapped()
}
}
}
and in the viewDidLoad:
updateNowPlayingCenter(title: titleText[thisSong], artist: authorText[thisSong], currentTime: audioPlayer.currentTime as AnyObject, songLength: audioPlayer.duration as AnyObject, PlaybackRate: 1.0)
I'm using Swift 3 and Xcode 8.