Post not yet marked as solved
Post marked as unsolved with 5 replies, 7,763 views
I'm trying to use AVAudioEngine to record sounds from the microphone together with various sound effect files to a AVAudioFile.I create an AVAudioFile like this:let settings = self.engine.mainMixerNode.outputFormatForBus(0).settingstry self.audioFile = AVAudioFile(forWriting: self.audioURL, settings: settings, commonFormat: .PCMFormatFloat32, interleaved: false)I install a tap on the audio engine's mainMixerNode, where I write the buffer to the file:self.engine.mainMixerNode.installTapOnBus(0, bufferSize: 4096, format: self.engine.mainMixerNode.outputFormatForBus(0)) { (buffer, time) -> Void in do { try self.audioFile?.writeFromBuffer(buffer) } catch let error as NSError { NSLog("Error writing %@", error.localizedDescription) }}I'm using self.engine.mainMixerNode.outputFormatForBus(0).settingswhen creating the audio file since Apple states that "The buffer format MUST match the file's processing format which is why outputFormatForBus: was used when creating the AVAudioFile object above". In the documentation for installTapOnBus they also say this: " The tap and connection formats (if non-nil) on the specified bus should be identical"However, this gives me a very large, uncompressed audio file. I want to save the file as .m4a but don't understand where to specify the settings I want to use:[AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),AVSampleRateKey : NSNumber(double: 32000.0),AVNumberOfChannelsKey: NSNumber(int: 1),AVEncoderBitRatePerChannelKey: NSNumber(int: 16),AVEncoderAudioQualityKey: NSNumber(int: Int32(AVAudioQuality.High.rawValue))]If I pass in these settings instead when creating the audio file, the app crashes when I record.Any suggestions or ideas on how to solve this?