#import <AVFoundation/AVFoundation.h> 需要引入
- (NSString*) documentsPath {
if
(! _documentsPath) {
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsPath = [searchPaths objectAtIndex: 0];
[_documentsPath retain];
}
return
_documentsPath;
}
NSString *destinationString = [[self documentsPath]
stringByAppendingPathComponent:filenameField.text];
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
NSError *recorderSetupError = nil;
AVAudioRecorder audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL
settings:recordSettings error:&recorderSetupError];
[recordSettings release];
第二个参数 settings是一个容纳键值对的NSDictionary有四种一般的键
1:一般的音频设置
2:线性PCM设置
3:编码器设置
4:采样率转换设置
NSMutableDictionary 需要加入五个设置值(线性PCM)
NSMutableDictionary *recordSettings =
[[NSMutableDictionary alloc] initWithCapacity:10];
[recordSettings setObject:
[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
float
sampleRate =
[pcmSettingsViewController.sampleRateField.text floatValue];
[recordSettings setObject:
[NSNumber numberWithFloat:sampleRate] forKey: AVSampleRateKey];
[recordSettings setObject:
[NSNumber numberWithInt:
(pcmSettingsViewController.stereoSwitch.on ? 2 : 1)]
forKey:AVNumberOfChannelsKey];
int
bitDepth =
[pcmSettingsViewController.sampleDepthField.text intValue];
[recordSettings setObject:
[NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:
[NSNumber numberWithBool:
pcmSettingsViewController.bigEndianSwitch.on]
forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:
[NSNumber numberWithBool:
pcmSettingsViewController.floatingSamplesSwitch.on]
forKey:AVLinearPCMIsFloatKey];
AVAudioRecorder成功创建后,使用他非常直接.它的三个基本方法如下
-(
void
) startRecording {
[audioRecorder record];
}
-(
void
) pauseRecording {
[audioRecorder pause];
recordPauseButton.selected = NO;
}
-(
void
) stopRecording {
[audioRecorder stop];