2012-09-15 124 views
12

我正在实施音频录制。它可以很好地与caf & wave格式。但问题是文件大小太大。iOS:音频录制文件格式

那么,任何人都可以帮助我记录音频格式,也发挥在窗口&文件大小是低的。

我尝试的代码如下:

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

重复http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-format? – nneonneo

+0

以m4a(MPEG-4 AAC)格式录制。有关详细信息,请参阅http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-format。 – nneonneo

回答

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”的文件可以作为您请。

+0

'setActive:withFlags:error:'在iOS 6之后弃用。相反,我们可以使用'setActive:error:'(不设置标志)。这对我来说可以。 – Vineeth

+0

老兄!保存了我的一天:)我为她的第一个生日为我的宝宝制作了一个应用程序,所以她感谢你<3 –

+0

从kAudioFormatMPEG4AAC更改为kAudioFormatMPEG4AAC,将文件大小从1.3MB更改为45kb – Thiru

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

这里是我的高品质和文件尺寸较小(资产负债)的设置:

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

这给你一个接近CD品质的单声道AAC轨道。您应该附加.m4a文件,以便随处读取。所有其他参数设置为设备默认值,这是大多数情况下您想要实现的。

+0

再一次,这用于正常工作 - 但如果有任何可观的级别,现在使用iOS10进行扭曲。使用iPad Pro –