AVAudioPlayer 没有声音
No sound from AVAudioPlayer
我正在尝试将循环音乐添加到我的应用程序中。模拟器运行,但是没有音乐出来。
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()
}
}
您从未调用过该函数,因此未调用函数中的代码行。在viewDidLoad()中调用函数startAudio()
。
我正在尝试将循环音乐添加到我的应用程序中。模拟器运行,但是没有音乐出来。
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()
}
}
您从未调用过该函数,因此未调用函数中的代码行。在viewDidLoad()中调用函数startAudio()
。