作品链接:
http://www.jianshu.com/users/1e0f5e6f73f6/top_articles
1.导入第三方框架
#import <AVFoundation/AVFoundation.h>
2.声明录音对象
@property (nonatomic, strong) AVAudioRecorder *recorder;
3.#pragma mark - 懒加载代码
- (AVAudioRecorder *)recorder
{
if (_recorder == nil) {
// 1.创建沙盒路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接音频文件
NSString *filePath = [path stringByAppendingPathComponent:@"123.mp3"];
// 3.转化成url file://
NSURL *url = [NSURL fileURLWithPath:filePath];
// 4.设置录音的参数
NSDictionary *settingRecorder = @{
// 音质
AVEncoderAudioQualityKey : [NSNumber numberWithInteger:AVAudioQualityLow],
// 比特率
AVEncoderBitRateKey : [NSNumber numberWithInteger:16],
// 采样率
AVSampleRateKey : [NSNumber numberWithFloat:8000],
// 声道 视频2.1 录音 2.
AVNumberOfChannelsKey : [NSNumber numberWithInteger:2]
};
// 5.创建录音对象
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settingRecorder error:nil];
NSLog(@"%@,%@",filePath,path);
}
return _recorder;
}
3.#pragma mark - 点击事件
// 开始录音
- (IBAction)start {
[self.recorder record];
}
// 结束录音
- (IBAction)stop {
[self.recorder stop];
}