[我正在使用AVAudio播放器,目前正在收到以下错误-致命错误:展开一个可选值时意外发现nil:文件
我查看了各种解决方案,问题是所需的文件不在副本分发包资源区域中。但是文件已添加到所需的目标位置,因此不确定解决方案。
import Foundation
import Capacitor
@objc(Buckfast)
public class Buckfast: CAPPlugin {
@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.success([
"value": value
])
var bombSoundEffect: AVAudioPlayer?
if let path = Bundle.main.path(forResource: "1", ofType: "wav") {
let url = URL(fileURLWithPath: path)
do {
bombSoundEffect = try AVAudioPlayer(contentsOf: url)
bombSoundEffect?.play()
} catch {
// couldn't load file :(
}
}
}
}
您可以尝试像if let
一样使用[]来解开包装>
if let path = Bundle.main.path(forResource: "1", ofType: "wav") {
}