xcode iOS:录音文件格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12435683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:30:12  来源:igfitidea点击:

iOS: Audio Recording File Format

iphoneobjective-ciosxcodeaudio-recording

提问by user1673099

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

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

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

因此,任何人都可以帮助我录制格式也在窗口中播放的音频,并且文件大小很小。

The code I tried is below:

我试过的代码如下:

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

回答by Scott Miller

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.

我还尝试使用 AVAudioRecorder 以 AAC 编码格式录制音频,最好是在 .m4a 文件中。我很快就能够将代码录制到核心音频文件 (.caf) 内的 AAC,但我无法让 AVAudioRecorder 正确格式化 .m4a。我正准备使用较低级别的 Audio Units API 在我的应用程序中重写录音代码,但我把它放在了后面,并添加到代码中来处理共享音频会话。设置完成后,我返回并尝试另存为 .m4a 文件,结果正常。

Here is some example code:

下面是一些示例代码:

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

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

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

回答by Matjan

Here are my settings for high quality and small file size (on balance):

以下是我对高质量和小文件大小的设置(总体而言):

NSDictionary *recordSettings = @{AVEncoderAudioQualityKey: @(AVAudioQualityMedium),
                                 AVFormatIDKey: @(kAudioFormatMPEG4AAC),
                                 AVEncoderBitRateKey: @(128000),
                                 AVNumberOfChannelsKey: @(1),
                                 AVSampleRateKey: @(44100)};

This gives you a close to cd quality mono AAC track. You should append the file with .m4a to be readable everywhere. All other parameters get set to device defaults, which is what you want to happen in most cases.

这为您提供了接近 cd 质量的单声道 AAC 音轨。您应该使用 .m4a 附加文件,以便在任何地方都可读。所有其他参数都设置为设备默认值,这在大多数情况下是您希望发生的。

回答by Jerry Thomsan

NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithCapacity:0];

[settings setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; 
[settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

//Encoder

[settings setValue :[NSNumber numberWithInt:12000] forKey:AVEncoderBitRateKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitDepthHintKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitRatePerChannelKey];
[settings setValue :AVAudioQualityMin           forKey:AVEncoderAudioQualityKey];