当我们录制音频时,如何将录音设置更改为16Khz和16位?

时间:2013-03-11 10:36:45

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

我有如下设置。

我想在录制音频时将录音设置更改为16Khz和16位。

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"sound.wav"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

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

NSError *error = nil;

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

if (error)
{

} else
{
    [audioRecorder prepareToRecord];
}

如何设置这些设置?

编辑问题

感谢您的回复,我尝试了这些方法,但它对我不起作用,因为我的客户端正在将录制的语音(录制的语音,我以字节格式发送)发送到ASR引擎(自动语音识别)。我没有得到相同的响应(我得到的响应音频说“引号”)我发送的内容。客户说您没有以16KHz和16位采样率录制语音,这就是您获得该响应的原因。但我告诉他我发送给他的服务器的字节,他给了.wav文件,它正在播放完美。但是,如果他发送给ASR引擎的同一个,ASR引擎不接受录制的语音我发送的内容(他说ASR不会接受,因为你没有录制16KHz和16比特采样率的音频)。客户给出了以下回复。 (但是,我尝试了你提出的所有方法)

Filename:   sv_SE_356985580762248932.wav
Folder: E:\developApp\TestappName\Mortionsn_dev\2nd-iteration\test_wfiles
File Type:  44100Hz, 16-bit, Stereo
Uncompressed Size:  1.63 MB (1,713,696 bytes)
File Format:    Windows PCM
Windows PCM
Size on Disk:   1.63 MB (1,717,892 bytes)
Last Written (local):   3/11/2013  00:21:00.000
Length: 0:09.714
428,424 samples

使用以下答案编辑问题第二次:

稍后通过提供建议我将设置代码更改为:

NSMutableDictionary *recordSettings = [NSMutableDictionary dictionary];


[recordSettings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];

[recordSettings setValue: [NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];//8000.0

[recordSettings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

[recordSettings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

[recordSettings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];

[recordSettings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

[recordSettings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];

2 个答案:

答案 0 :(得分:3)

试试这个, 常规音频设置是,

AVFormatIDKey,
AVSampleRateKey,
AVNumberOfChannelsKey.

对于录音机

AVEncoderAudioQualityKey;
AVEncoderBitRateKey;
AVEncoderBitRatePerChannelKey;
AVEncoderBitDepthHintKey;

确保您已包含常规设置和录像机设置。

并将您的AVSampleRateKey更改为16000.0

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:                 
                                [NSNumber numberWithInt:kAudioFormatLinearPCM],
                                AVFormatIDKey
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:16000.0],
                                AVSampleRateKey,
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                nil];

答案 1 :(得分:2)

您现有的设置是44.1kHz和16位(假设上面已经有效),您需要更改的唯一一行是:

[NSNumber numberWithFloat:44100.0] 

要:

[NSNumber numberWithFloat:16000.0]