【发布时间】:2023-03-10 03:05:01
【问题描述】:
我正在使用 swift 2 尝试以下代码,在 swift 1 中应该没问题。
class NewSoundViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
let audioURL = NSURL.fileURLWithPathComponents([
NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0],
"MyAudio.m4a"
])
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch {
print("Session errors.")
}
do {
let recordSettings: [String: AnyObject] = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
]
self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings)
self.audioRecorder.meteringEnabled = true
self.audioRecorder.prepareToRecord()
} catch let error as NSError{
print(error.description)
} catch {
print("Other errors")
}
super.init(coder: aDecoder)
}
编译错误
类型“AudioFormatID”不符合协议“AnyObject”`
在线AVFormatIDKey: kAudioFormatMPEG4AAC,。
如果我注释掉该行,我通过了构建,但出现了运行时错误
Error Domain=NSOSStatusErrorDomain Code=1718449215“操作无法完成。(OSStatus 错误 1718449215。)”
我也试过AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),,但出现运行时错误。 Xcode 似乎进入调试模式它红色突出显示self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings) 并表示
线程 1:EXC_BAD_ACCESS(code=1, address=0x0)
谁能帮帮我?
【问题讨论】: