I am trying to use the following code with swift 2, which should be accurate in 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) }
I got a compilation error
AudioFormatID type does not conform to AnyObject protocol
in line AVFormatIDKey: kAudioFormatMPEG4AAC,
If I comment out a line, I passed the assembly but got a runtime error
Domain Error = NSOSStatusErrorDomain Code = 1718449215 "Operation could not be completed (OSStatus error 1718449215.)"
I also tried AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC), and got a runtime error. Xcode seemed to go into debug mode. The red color is highlighted by self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings) and said
Subject 1: EXC_BAD_ACCESS (code = 1, address = 0x0)
Can anybody help me?
source share