13

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

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

我试过的代码如下:

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

3 回答 3

16

我还尝试使用 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”处的文件。

于 2012-10-02T22:00:52.037 回答
8

这是我对高质量和小文件大小的设置(平衡):

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

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

于 2013-07-01T13:27:50.220 回答
1
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];
于 2012-09-15T11:48:28.313 回答