Already I have created an iPhone application to record. It will record in .caf file.
But I want to record in .m4a format.
Please help me to do this.
Here is the working SWIFT code to record m4a Audio files. Bear in mind that hitting the right format parameters that produce usable audio files in iOS is really painful to find. I found that this combination works, after much trial and error. I hope it saves you time, enjoy!
let recordSettings: [String : AnyObject] = [AVSampleRateKey : NSNumber(float: Float(16000)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Low.rawValue))]
func initializeAudioSession(){
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
settings: recordSettings)
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
} catch let error as NSError{
print("ERROR Initializing the AudioRecorder - "+error.description)
}
}
func recordSpeechM4A(){
if !audioRecorder.recording {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true)
audioRecorder.record()
print("RECORDING")
} catch {
}
}
}
func directoryURL() -> NSURL { //filename helper method
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
filepath = urls[0]
let documentDirectory = urls[0] as NSURL
print("STORAGE DIR: "+documentDirectory.description)
//print("---filepath: "+(filepath?.description)!)
let soundURL = documentDirectory.URLByAppendingPathComponent("recordedAudio.m4a") //.m4a
print("SAVING FILE: "+soundURL.description)
return soundURL
}