我是快速开发和构建适用于Apple Watch的简单应用程序的新手。点击按钮时,我想播放短促的声音。这可能吗?
据我了解,最好的方法是使用AVFoundation和AVAudioPlayer。在最近的几个发行版中似乎对此进行了很多更新,我发现了相互矛盾的建议。根据一些教程,我将这个简单的测试放在一起,但是我得到了一个“线程1:致命错误:在解开可选值时意外发现为零” ,这是我的代码:>
import WatchKit
import Foundation
import AVFoundation
var dogSound = AVAudioPlayer()
class InterfaceController: WKInterfaceController {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func playTapped() {
let path = Bundle.main.path(forResource: "Dog", ofType: ".mp3")!
let url = URL(fileURLWithPath: path)
do {
dogSound = try AVAudioPlayer(contentsOf: url)
dogSound.play()
} catch {
//couldn't load file :(
}
}
}
我的音频文件是 WatchKit扩展中 Assets.xcassets 文件夹中名为 Dog 的 mp3 已将 AVFoundation.framework 添加到库链接二进制文件
我在做什么错?有没有正确的方法教程来实现?非常感谢!