AVAudioRecorder记录AAC/m4a

Chr*_*ken 12 audio avaudiorecorder xamarin.ios ipad ios

我正在尝试使用AVAudioRecorder在设备上录制音频.该文件被传输到Web服务器,我想在Web浏览器中播放该结果文件.

我在设备上尝试了各种设置组合......似乎没有任何东西将文件编码为正确的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();  
     }
Run Code Online (Sandbox Code Playgroud)

编辑 - 将此代码放在行之前

recorder = AVAudioRecorder.ToUrl(url, settings, out error);
Run Code Online (Sandbox Code Playgroud)

一切正常.

NSError error;    
AVAudioSession audioSession = AVAudioSession.SharedInstance();
audioSession.SetCategory(AVAudioSession.CategoryRecord, out error);
audioSession.SetActive(true, out error);
Run Code Online (Sandbox Code Playgroud)

小智 32

对于硬件加速编码器,在任何给定时间只能激活一个会话.这意味着,对于AAC,它将在模拟器(使用软件编码器)中工作,但可能在设备上失败(使用硬件编码器),除非您的应用激活了AVAudioSession.

我还尝试使用AVAudioRecorder以AAC编码格式录制音频,最好是.m4a文件.我有一个时间的野兽这样做.我很快就能够将代码记录到核心音频文件(.caf)中的AAC,但我无法让AVAudioRecorder正确格式化.m4a.我正准备使用较低级别的Audio Units API在我的应用程序中重写录制代码,但我将其放在后面,并添加到代码中以处理共享音频会话.一旦设置完毕,我就回去尝试保存为.m4a并且它刚刚起作用.

这是一些示例代码:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                        [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                        [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                        nil];
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
[recorder prepareToRecord];

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];

[recorder record];
Run Code Online (Sandbox Code Playgroud)

然后结束我使用的录音:

[recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation;
[session setActive:NO withFlags:flags error:nil];
Run Code Online (Sandbox Code Playgroud)

然后可以随意使用'tmpFileUrl'中的文件.


tri*_*sse 9

iOS SDK显然很愚蠢.将格式ID键设置kAudioFormatMPEG4AAC为不足以实际编码文件; 在QuickTime中它会播放,但在其属性中你只会看到AAC,没有进一步的细节.将输出文件扩展名更改为m4a有助于实现技巧并使其应用正确的编码.