作品鏈接:
http://www.reibang.com/users/1e0f5e6f73f6/top_articles
1.導(dǎo)入第三方框架
#import <AVFoundation/AVFoundation.h>
2.聲明錄音對象
@property (nonatomic, strong) AVAudioRecorder *recorder;
3.#pragma mark - 懶加載代碼
- (AVAudioRecorder *)recorder
{
if (_recorder == nil) {
// 1.創(chuàng)建沙盒路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接音頻文件
NSString *filePath = [path stringByAppendingPathComponent:@"123.mp3"];
// 3.轉(zhuǎn)化成url file://
NSURL *url = [NSURL fileURLWithPath:filePath];
// 4.設(shè)置錄音的參數(shù)
NSDictionary *settingRecorder = @{
// 音質(zhì)
AVEncoderAudioQualityKey : [NSNumber numberWithInteger:AVAudioQualityLow],
// 比特率
AVEncoderBitRateKey : [NSNumber numberWithInteger:16],
// 采樣率
AVSampleRateKey : [NSNumber numberWithFloat:8000],
// 聲道 視頻2.1 錄音 2.
AVNumberOfChannelsKey : [NSNumber numberWithInteger:2]
};
// 5.創(chuàng)建錄音對象
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settingRecorder error:nil];
NSLog(@"%@,%@",filePath,path);
}
return _recorder;
}
3.#pragma mark - 點擊事件
// 開始錄音
- (IBAction)start {
[self.recorder record];
}
// 結(jié)束錄音
- (IBAction)stop {
[self.recorder stop];
}