【问题标题】:iOS: Audio Recording File FormatiOS:录音文件格式
【发布时间】:2012-09-08 06:39:51
【问题描述】:

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

那么,谁能帮我录制格式在窗口中播放且文件大小很小的音频。

我试过的代码如下:

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

【问题讨论】:

标签: iphone objective-c ios xcode audio-recording


【解决方案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];

【讨论】:

    【解决方案2】:

    我还尝试使用 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'处的文件就可以随意使用了。

    【讨论】:

    • setActive: withFlags: error: 在 iOS 6 之后被弃用。相反,我们可以使用 setActive: error:(不设置标志)。它对我来说很好。
    • 从 kAudioFormatMPEG4AAC 更改为 kAudioFormatMPEG4AAC,文件大小从 1.3MB 更改为 45kb
    • 有没有人用iOS10试过这个?如果有任何明显的水平,录音就会完全失真。
    【解决方案3】:

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

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

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

    【讨论】:

    • 再一次,这曾经可以正常工作 - 但如果有任何明显的水平,现在会在 iOS10 中失真。使用 iPad Pro
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多