当我尝试使用 swift 播放音频文件时应用程序崩溃
Posted
技术标签:
【中文标题】当我尝试使用 swift 播放音频文件时应用程序崩溃【英文标题】:Application crashing when I try to play audio file using swift 【发布时间】:2015-10-08 01:30:57 【问题描述】:func playCorrectSound()
let soundURL: NSURL = NSBundle.mainBundle().URLForResource("correct", withExtension: "m4a")!
correctSound = try! AVAudioPlayer(contentsOfURL: soundURL)
correctSound.play()
func textFieldShouldReturn(textField: UITextField) -> Bool
if correctIncorrectField.text == answerField.text && correctIncorrectField.text != "" && answerField.text != ""
pointsNum = pointsNum + 10
points.text = "\(pointsNum)"
textField.text = ""
textField.hidden = true
correctIncorrect.hidden = false
correctIncorrect.textColor = UIColor.greenColor()
correctIncorrect.text = "Correct!"
pauseAndDoStuff()
playCorrectSound()
if correctIncorrectField.text != answerField.text && correctIncorrectField.text != "" && answerField.text != ""
pointsNum = pointsNum - 5
points.text = "\(pointsNum)"
textField.text = ""
textField.hidden = true
correctIncorrect.hidden = false
correctIncorrect.textColor = UIColor.redColor()
correctIncorrect.text = "Incorrect!"
pauseAndDoStuff()
textField.resignFirstResponder()
return true
由于某种原因,它崩溃了
let soundURL: NSURL = NSBundle.mainBundle().URLForResource("correct", withExtension: "m4a")!
一行和它唯一的说法:
致命错误:在展开 Optional 值时意外发现 nil。
【问题讨论】:
What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?的可能重复 【参考方案1】:确保您的项目中存在correct.m4a
文件
并且它包含在Copy Bundle Resources
Target -> Build Phases
中
【讨论】:
以上是关于当我尝试使用 swift 播放音频文件时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
没有看到 20 个 I/O 周期;下载音频文件时在 Swift 中发生放弃