2012-08-28 147 views
10

我试图使用AVAudioRecorder在设备上录制音频。该文件传输到网络服务器,我想在网页浏览器播放结果文件..AVAudioRecorder记录AAC/m4a

我已经尝试设备上的各种组合设置...没有什么似乎编码文件到正确的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(); 
    } 

编辑 -By把这个代码行

recorder = AVAudioRecorder.ToUrl(url, settings, out error); 

一切工作之前。

NSError error;  
AVAudioSession audioSession = AVAudioSession.SharedInstance(); 
audioSession.SetCategory(AVAudioSession.CategoryRecord, out error); 
audioSession.SetActive(true, out error); 
+0

如果你已经找到了解决方案,这是我面临同样的问题,我不知道。 – justinkoh

回答

6

iOS SDK显然很愚蠢。将格式ID密钥设置为kAudioFormatMPEG4AAC对于实际编码文件是不够的;在QuickTime中,它会播放,但在它的属性中,你只会看到AAC,没有进一步的细节。将输出文件扩展名更改为m4a应该有助于解决问题并使其适用正确的编码。

+0

我很确定我尝试过,并没有奏效。但我会再试一次。 –

+0

这仍然不起作用“这不是quicktime明白的文件” –

+0

恐怕这种方法也不适用于我。 – justinkoh

30

对于硬件加速编码器,在任何给定时间只能有一个会话处于活动状态。这意味着,对于AAC,除非您的应用程序激活AVAudioSession,否则它将在模拟器(使用软件编码器的地方)中工作,但可能会在设备上(使用硬件编码器的地方)失败。

我也试图使用AVAudioRecorder以AAC编码格式录制音频,最好在.m4a文件中录制。我有一次这样做的野兽。我很快就能够将代码记录到核心音频文件(.caf)的AAC中,但我无法使AVAudioRecorder正确格式化.m4a。我正要使用较低级别的音频单元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]; 

然后结束我所用的记录:

[recorder stop]; 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation; 
[session setActive:NO withFlags:flags error:nil]; 

然后在“tmpFileUrl”的文件可以作为您请。

+1

这应该是无数次upvoted! – mahboudz

+0

令人惊叹的答案。谢谢,这节省了我很多时间! –

3

有关AVAudioSession的提示非常有帮助!

由于setActive:withFlags:error:已在iOS的6.0 变得过时停止音频会话的代码应调整至:

AVAudioSession *session = [AVAudioSession sharedInstance]; 
int flags = AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; 
[session setActive:NO withOptions:flags error:nil]; 
相关问题