我有以下代码,我总是在“ try btnSound”上得到一个断点。不知道我在哪里做错了。声音文件位于主文件夹中。
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var outputLbl: UILabel!
var btnSound: AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "btn", ofType: "wav")
do {
try btnSound = AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))
btnSound.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
}
@IBAction func numberPressed(btn: UIButton!){
btnSound.play()
}
}
答案 0 :(得分:0)
好。谢谢大家我只是用过:
var audioPlayer = AVAudioPlayer()
let source = Bundle.main.path(forResource: "btn", ofType: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: source!))
audioPlayer.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
我将“路径”重命名为“声音”,并且开始工作。不知道是什么问题。