Swift2 AVAudioRecorder

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?

+5
source share
3 answers

I also tried AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC)

Well, that is the right thing. You basically ask two questions here; compiler error that you have already solved. You now have a runtime error, but that is a completely different matter.

As for the runtime error, this is probably just an example of a test attempt on a simulator. I ran your code on the device (after fixing the corresponding line so that it compiles), and that's fine.

EDIT In a comment, you found that you tested this on a device running iOS 8.3. This is problem! You need to test the device, and it must be a device running iOS 9. Then you will find that your code works without failures.

+3
source

I had the same problem with the settings. AVFormatIDKey was not received with the error mentioned in the original message.

In Swift 2, recording options must be explicitly entered.

These are my recording settings that work. If I just skipped AVFormatIDKey, the microphone only worked for a split second.

 let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)), AVFormatIDKey : NSNumber(int: Int32(kAudioFormatAppleLossless)), AVNumberOfChannelsKey : NSNumber(int: 1), AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue)), AVEncoderBitRateKey : NSNumber(int: Int32(320000))] 

You do not need to upgrade your device to iOS 9. I built and ran this for iOS 8.4

+2
source

I had a similar problem, and it turned out that when I upgraded from swift 1.2 to 2.0, the signature of the "settings" was changed to optional [String: AnyObject]

 //Swift 1.2 AVAudioRecorder(URL: audioURL!, settings: nil) 

If I go through an empty dictionary, it is no longer a failure for me and works as before.

 //Swift 2.0 AVAudioRecorder(URL: audioURL!, settings: [:]) 
0
source

All Articles