import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "GoingHome", withExtension: "mp3")!
player = try? AVAudioPlayer(contentsOf: url)
player?.numberOfLoops = -1 //循环次数
player?.enableRate = true //允许倍速播放
player?.rate = 1.25 //播放倍速
}
@IBAction func play(_ sender: Any) {
player?.play()
}
@IBAction func pause(_ sender: Any) {
player?.pause()
}
@IBAction func stop(_ sender: Any) {
player?.stop()
}
@IBAction func seek(_ sender: UISlider) {
let value = sender.value
player?.currentTime = Double(value) * (player?.duration ?? 0)
}
}