- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发这个记录器项目,这段代码是用 swift 2.0 编写的,它给出了这个问题!我有类似的标题帖子,但与我遇到的问题无关
导入UIKit导入AVFoundation
类 PlaySoundViewController:UIViewController {
var audioPlayer: AVAudioPlayer!
var receivedAudio: AudioRecorded!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
do{
let audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl) --> ***The error happens here***
audioPlayer.enableRate = true
}catch let ErrorType {
NSLog("Could not create AVAudioPlayer: \(ErrorType)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playFastSound(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.play()
audioPlayer.rate = 2.0
}
@IBAction func playSlowSound(sender: AnyObject)
{
audioPlayer.stop()
audioPlayer.play()
audioPlayer.rate = 0.5
}
@IBAction func stopPlaying(sender: AnyObject) {
audioPlayer.stop()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
最佳答案
您的问题可能是在分配期间使用 try!
。
如HWS puts it -
There's one more thing to discuss, which is what to do if you know a call simply can't fail, for whatever reason. Now, clearly this is a decision you need to make on a case-by-case basic, but if you know there's absolutely no way a method call might fail, or if it did fail then your code was so fundamentally broken that you might as well crash, you can use try! to signal this to Swift.
所以这可能不是您想要的。对于错误处理,您通常会使用 try
(不带 !
)。
do {
let audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl)
// Do stuff with audio player
}
catch let error {
NSLog("Could not create AVAudioPlayer: \(error)")
}
现在这并不能解释为什么没有创建音频播放器。最有可能的是 URL 无效,它指向的文件不存在,或者它的编码方式是 AVAudioPlayer 无法读取的。通常您需要标准文件格式,例如 mp3、mp4 等。
关于ios - 线程 1 : exc_bad_instruction (code=exc_i386_invop subcode=0x0) error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760844/
我是一名优秀的程序员,十分优秀!