#import 需要引入
//获取document目录的路径
- (NSString*) documentsPath {
if (! _documentsPath) {
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsPath = [searchPaths objectAtIndex: 0];
[_documentsPath retain];
}
return _documentsPath;
}
//(document目录的路径)
NSString *destinationString = [[self documentsPath]
stringByAppendingPathComponent:filenameField.text];
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
//初始化AVAudioRecorder
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];
//1 ID号
[recordSettings setObject:
[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
float sampleRate =
[pcmSettingsViewController.sampleRateField.text floatValue];
//2 采样率
[recordSettings setObject:
[NSNumber numberWithFloat:sampleRate] forKey: AVSampleRateKey];
//3 通道的数目
[recordSettings setObject:
[NSNumber numberWithInt:
(pcmSettingsViewController.stereoSwitch.on ? 2 : 1)]
forKey:AVNumberOfChannelsKey];
int bitDepth =
[pcmSettingsViewController.sampleDepthField.text intValue];
//4 采样位数 默认 16
[recordSettings setObject:
[NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey];
//5
[recordSettings setObject:
[NSNumber numberWithBool:
pcmSettingsViewController.bigEndianSwitch.on]
forKey:AVLinearPCMIsBigEndianKey];
//6 采样信号是整数还是浮点数
[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];
}
这两天也调了一下ios的录音,原文链接:http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1
这里ios的录音功能主要依靠AVFoundation.framework与CoreAudio.framework来实现
在工程内添加这两个framework
我这里给工程命名audio_text
在生成的audio_textViewController.h里的代码如下
- #import
- #import
- #import
-
- @interface audio_textViewController : UIViewController {
-
- IBOutlet UIButton *bthStart;
- IBOutlet UIButton *bthPlay;
- IBOutlet UITextField *freq;
- IBOutlet UITextField *value;
- IBOutlet UIActivityIndicatorView *actSpinner;
- BOOL toggle;
-
- //Variable setup for access in the class
- NSURL *recordedTmpFile;
- AVAudioRecorder *recorder;
- NSError *error;
- }
-
- @property (nonatomic,retain)IBOutlet UIActivityIndicatorView *actSpinner;
- @property (nonatomic,retain)IBOutlet UIButton *bthStart;
- @property (nonatomic,retain)IBOutlet UIButton *bthPlay;
-
- -(IBAction)start_button_pressed;
- -(IBAction)play_button_pressed;
- @end
audio_textViewController.m
最后在interface builder里面绘制好界面,如
设置下按键的属性
基本就ok了,可以开始录音了。
BUG解决
但是大家要注意一个貌似是ios5.0之后引入的bug,就是当你录制音频的时候启动时间往往会比较慢,大概需要3--5秒的时间吧,这种现象尤其在播放的时候立刻切换到录制的时候非常明显。
具体的原因我也不是很清楚,感觉应该是更底层的一些bug。目前我的解决方案是这样的。
1.在音频队列的初始化方法中加入代码
OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_PlayAndRecord;
error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, NULL, self);
UInt32 inputAvailable = 0;
UInt32 size = sizeof(inputAvailable);
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, NULL, self);
AudioSessionSetActive(true);
2.在录制声音开始的时候先把播放声音stop,加入
UInt32 category = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
这样做应该会让你的录制启动速度显著加快的。