我正在尝试将循环音乐添加到我的应用中。模拟器运行,但没有音乐出现。
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var audioPlayer = AVAudioPlayer()
func startAudio() {
let filePath = Bundle.main.path(forResource: "backsong", ofType: "mp3")
let fileURL = NSURL.fileURL(withPath: filePath!)
do {
audioPlayer = try AVAudioPlayer.init(contentsOf: fileURL, fileTypeHint: AVFileType.mp3.rawValue)
audioPlayer.numberOfLoops = -1
audioPlayer.volume = 1
} catch {
self.present(UIAlertController.init(title: "Error", message: "Error Message", preferredStyle: .alert), animated: true, completion: nil)
}
audioPlayer.play()
}
}
答案 0 :(得分:0)
您从未调用该函数,因此函数内的代码行不会被调用。在viewDidLoad()中调用函数startAudio()
。