我为AVAudioPlayer
设置了控制中心。一切都很好,除了控制中心的进度条,只要AVAudioPlayer
暂停,它就会继续计数。我怎样才能解决这个问题?这就是我到目前为止所拥有的:
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()
}
}
}
并在viewDidLoad中:
updateNowPlayingCenter(title: titleText[thisSong], artist: authorText[thisSong], currentTime: audioPlayer.currentTime as AnyObject, songLength: audioPlayer.duration as AnyObject, PlaybackRate: 1.0)
我使用的是Swift 3和Xcode 8。