我正在尝试使用 AVAudioRecorder 在设备上录制音频。该文件被传输到网络服务器,我想在网络浏览器中播放生成的文件..
我在设备上尝试了各种设置组合...似乎没有将文件编码为正确的 AAC 格式。
QuickTime 说它不知道如何播放这个文件。
示例代码
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();
}
编辑- 通过将此代码放在该行之前
recorder = AVAudioRecorder.ToUrl(url, settings, out error);
一切正常。
NSError error;
AVAudioSession audioSession = AVAudioSession.SharedInstance();
audioSession.SetCategory(AVAudioSession.CategoryRecord, out error);
audioSession.SetActive(true, out error);