I am trying to record audio on the Device using AVAudioRecorder. That file is transmitted to a web server and I want to play that resulting file in a web browser..
I have tried various combinations of settings on the device…nothing seems to encode the file into the correct AAC format.
QuickTime says that it doesn’t know how to play this file.
Sample code
private void InitializeRecordingSession() {
string fileName = string.Format("{0}.m4a", Guid.NewGuid());
string tmpdir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/audio";
if (!Directory.Exists(tmpdir))
Directory.CreateDirectory(tmpdir);
audioFilePath = Path.Combine(tmpdir, fileName);
Console.WriteLine("Audio File Path: " + audioFilePath);
var url = NSUrl.FromFilename(audioFilePath);
var settings = new AVAudioRecorderSettings() {
AudioFormat = AudioFormatType.MPEG4AAC,
SampleRate = 44100,
NumberChannels = 1,
AudioQuality = AVAudioQuality.High,
};
recorder = AVAudioRecorder.ToUrl(url, settings, out error);
//Set Recorder to Prepare To Record
recorder.PrepareToRecord();
}
Edit-By putting this code in before the line
recorder = AVAudioRecorder.ToUrl(url, settings, out error);
Everything worked.
NSError error;
AVAudioSession audioSession = AVAudioSession.SharedInstance();
audioSession.SetCategory(AVAudioSession.CategoryRecord, out error);
audioSession.SetActive(true, out error);
The iOS SDK is apparently stupid. Setting the format ID key to
kAudioFormatMPEG4AACis not enough for it to actually encode the file; in QuickTime it will play but in its properties you’ll just seeAACand no further details. Changing the output file extension tom4ashould help do the trick and make it apply the right encoding.