我看过一些使用Objective-C实现此功能的帖子,但我无法通过Swift实现相同的功能。
具体而言,我无法弄清如何在下面实现addPeriodicTimeObserverForInterval
。
var player : AVAudioPlayer! = nil
@IBAction func playAudio(sender: AnyObject) {
playButton.selected = !(playButton.selected)
if playButton.selected {
let fileURL = NSURL(string: toPass)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
player.delegate = self
player.play()
startTime.text = "\(player.currentTime)"
endTime.text = NSString(format: "%.1f", player.duration)
} else {
player.stop()
}
任何协助将不胜感激。