【问题标题】:Swift 2.2 Error Handeling And AudioSwift 2.2 错误处理和音频
【发布时间】:2016-03-13 03:24:39
【问题描述】:

我正在关注 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) {
}

}

【问题讨论】:

    标签: ios swift error-handling


    【解决方案1】:

    使用 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)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 1970-01-01
      • 2017-08-02
      • 2011-07-09
      • 2019-10-23
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多