英文:
Fatal error: 'try!' expression unexpectedly raised an error : Error Domain=com.apple.coreaudio.avfaudio Code=2003332927
问题
Using Xcode 14.3,尝试更新较旧的Swift应用程序,录制声音后,将其保存以播放所需的效果,但是一旦尝试播放它,应用程序立即崩溃。
致命错误:'try!'表达式意外引发错误:错误域=com.apple.coreaudio.avfaudio,代码=2003332927 "(null)" 用户信息={failed call=AudioConverterSetProperty(converter, kAudioConverterEncodeBitRate, sizeof(UInt32), &bitRate)}
在以下代码的第一行崩溃:
func saveVoiceCganger() {
do{
let newAudio:AVAudioFile = try! AVAudioFile(forWriting: getOutPutUrl(), settings: [
AVFormatIDKey: NSNumber(value:kAudioFormatAppleLossless),
AVEncoderAudioQualityKey : AVAudioQuality.low.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
])
let length = self.audioFile?.length
self.audioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: self.audioEngine.mainMixerNode.outputFormat(forBus: 0)) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
print(newAudio.length)
print("=====================")
print(length!)
print("**************************")
if (newAudio.length) < length! {//Let us know when to stop saving the file, otherwise saving infinitely
do{
//print(buffer)
try newAudio.write(from: buffer)
}catch _{
print("Problem Writing Buffer")
}
}else{
self.audioEngine.mainMixerNode.removeTap(onBus: 0)
}
}
}catch _{
print("Problem")
}
}
英文:
Using Xcode 14.3, trying to update an older swift app, once recording a voice, it is then saved to play the required effect, however, app crashes as soon as I try to play it.
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.coreaudio.avfaudio Code=2003332927 "(null)" UserInfo={failed call=AudioConverterSetProperty(converter, kAudioConverterEncodeBitRate, sizeof(UInt32), &bitRate)}
Crashes on the first line of this
func saveVoiceCganger() {
do{
let newAudio:AVAudioFile = try! AVAudioFile(forWriting: getOutPutUrl(), settings: [
AVFormatIDKey: NSNumber(value:kAudioFormatAppleLossless),
AVEncoderAudioQualityKey : AVAudioQuality.low.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
])
let length = self.audioFile?.length
self.audioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: self.audioEngine.mainMixerNode.outputFormat(forBus: 0)) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
print(newAudio.length)
print("=====================")
print(length!)
print("**************************")
if (newAudio.length) < length! {//Let us know when to stop saving the file, otherwise saving infinitely
do{
//print(buffer)
try newAudio.write(from: buffer)
}catch _{
print("Problem Writing Buffer")
}
}else{
self.audioEngine.mainMixerNode.removeTap(onBus: 0)
}
}
}catch _{
print("Problem")
}
}
答案1
得分: 1
请看以下翻译:
总体而言,将 ! 视为崩溃运算符,不要使用它。在这个上下文中,它意味着尝试调用一些可能引发异常的函数,如果引发异常则崩溃。
将 try! 更改为 try,然后在您的捕获块中记录错误(不要使用 catch _ 并忽略结果)。
在其他上下文中,! 是一个强制解包运算符,即“如果为 nil 则崩溃”运算符。它还可以用来创建“隐式解包可选值”,每当引用这种可选值并且它为 nil 时都会引发崩溃。
(! 运算符也可以用作逻辑 NOT。这种用法不会导致崩溃。)
英文:
In general, think of the ! as the crash operator, and don't use it. In this context, it means try calling some function that throws, and crash if it throws.
Change try! to try, and then in your catch, log the error. (don't use catch _ and ignore the result.)
In other contexts, ! is a force-unwrap, which is the "crash if nil` operator. It can also be used as to create "implicitly unwrapped optionals", which set up a crash any time you reference such an optional and it's nil.
(The ! operator can also be logical NOT. That use doesn't cause crashes.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论