我正在学习 swift 1.0 的教程(偶然),它展示了如何播放声音。现在在 swift 2.0 中它不会工作请帮助。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var ButtonAudioURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("cow", ofType: "wav")!)
var ButtonAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
ButtonAudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playAudio(sender: AnyObject) {
ButtonAudioPlayer.play();
}
@IBAction func Stop(sender: AnyObject) {
}
最佳答案
使用 do-catch 语句通过运行一段代码来处理错误 ButtonAudioPlayer = try AVAudioPlayer(contentsOfURL: ButtonAudioURL)
//ButtonAudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error: nil)
do {
ButtonAudioPlayer = try AVAudioPlayer(contentsOfURL: ButtonAudioURL)
} catch let error as NSError {
print(error.localizedDescription)
}
关于ios - Swift 2.2 错误处理和音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966097/