我正在使用
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,.
如果我注释掉该行,我通过了构建,但是遇到了运行时错误
错误域= NSOsstatusErrorDomain代码= 1718449215“操作无法完成.(Osstatus错误1718449215.)”
我还尝试了AVFormatIDKey:NSNumber(unsignedInt:kAudioFormatMPEG4AAC),并且遇到了运行时错误. Xcode似乎进入调试模式它红色突出显示self.audioRecorder =尝试AVAudioRecorder(URL:audioURL!,settings:recordSettings)并表示
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)
谁能帮帮我吗?