作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在按下按钮时播放简单的声音。 xcode 强制我使用“!”对于我的音频文件的 NSURL,我按照其他教程学习如何快速播放声音,但由于某种原因我不断收到此错误,为什么!?代码:
import UIKit
导入 AVFoundation
类 View Controller :UIViewController {
var player = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("buttonSound", ofType: "mp3")
let fileURL = NSURL(fileURLWithPath: path!)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.prepareToPlay()
}
@IBAction func button21(sender: AnyObject) {
player.play()
}
当我添加“!”时,应用程序将加载而不会崩溃,当我按下按钮时,应用程序崩溃并且日志输出“ fatal error :在展开可选值时意外发现 nil”
最佳答案
很高兴看到您的问题得到解决。只是添加到对话中:
我和你有同样的问题,我听从了这里其他人的建议,但它并没有解决我的问题(unexpectedly found nil while unwrapping an Optional value
)。我正在使用 Xcode 6.1,为我解决的问题是删除文件并再次添加,除了这次,我没有将文件拖入,而是通过右键单击项目文件夹并选择“将文件添加到”来完成your_app's_name”选项。
关于ios - 选项类型 'String?' 的 AVFoundation 值未解包,应用程序崩溃,不会播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26877558/
我是一名优秀的程序员,十分优秀!