iOS:录音文件格式 [英] iOS: Audio Recording File Format

查看:252
本文介绍了iOS:录音文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施录音。它适用于 caf & wave 格式。但问题是文件太大。

I am implementing the Audio recording. It works fine with caf & wave format. But the problem is file size is too big.

所以,任何人都可以帮助我录制音频,其格式也在窗口和放大器中播放文件大小很低。

So, can anyone help me for record audio with format which also played in window & file size is little low.

我尝试的代码如下:

 dirPaths = NSSearchPathForDirectoriesInDomains(
                                                    NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
     NSString *soundFilePath = [docsDir
                                stringByAppendingPathComponent:@"sound.wave"];
     NSLog(@"%@",soundFilePath);
     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

     NSDictionary *recordSettings = [NSDictionary 
                         dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                     AVEncoderAudioQualityKey,
                                     [NSNumber numberWithInt:16], 
                                     AVEncoderBitRateKey,
                                     [NSNumber numberWithInt: 2], 
                                     AVNumberOfChannelsKey,
                                     [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                     [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                     nil];

     NSError *error = nil;

     audioRecorder = [[AVAudioRecorder alloc]
                      initWithURL:soundFileURL
                      settings:recordSettings
                      error:&error];

     if (error)
     {
         NSLog(@"error: %@", [error localizedDescription]);

     } else {
        [audioRecorder prepareToRecord];
     }


推荐答案

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

I also was attempting to use AVAudioRecorder to record audio in an AAC encoded format preferably in a .m4a file. I was quickly able to get the code to record to AAC inside of a core audio file (.caf), but I couldn't get AVAudioRecorder to properly format a .m4a. I was just about to rewrite the recording code in my application using the lower level Audio Units API but instead I put that on the back burner and added in the code to handle the shared audio session. Once that was set up, I went back and tried to save as an .m4a and it just worked.

以下是一些示例代码:

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];

然后结束我使用的录音:

Then to end the recording I used:

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

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

Then the file at 'tmpFileUrl' can be used as you please.

这篇关于iOS:录音文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆