我有这个功能:
func audioRecordingSettings() -> NSDictionary {
return [
AVFormatIDKey : kAudioFormatMPEG4AAC as NSNumber,
AVSampleRateKey : 16000.0 as NSNumber,
AVNumberOfChannelsKey : 1 as NSNumber,
AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue as NSNumber
]
}
然后我定义了另一个:
func startRecordingAudio() {
var error: NSError?
let audioRecordingURL = self.audioRecordingPath()
audioRecorder = AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings(), error: &error)
//Here the error comes.
}
它要求我在[NSObject: AnyObject]
之后添加audioRecordingSetthings()
,我不认为这是正确的解决方案。
因为当我在另一个startRecordingAudio()
中调用class
时,它会与Unexpectedly found nil
崩溃。
答案 0 :(得分:3)
您只需在audioRecordingSettings() as [NSObject : AnyObject]
:
AVAudioRecorderDelegate
即可
AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings() as [NSObject : AnyObject], error: &error)
此外,我会将您的方法更改为:
func audioRecordingSettings() -> [NSObject : AnyObject] {
return [
AVFormatIDKey : kAudioFormatMPEG4AAC,
AVSampleRateKey : 16000.0,
AVNumberOfChannelsKey : 1,
AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue
]
}
答案 1 :(得分:0)
您需要检查指定的"AVAudioRecorder"
初始值设定项:
init!(URL url: NSURL!, settings: [NSObject : AnyObject]!, error outError: NSErrorPointer)
因此,您需要将设置转换为[NSObject : AnyObject]
。