1、懒加载一个AVAudioRecorer类对象
2、设置保存的沙盒地址
3、将沙盒的地址转换成NSURl对象
4、用字典设置属性
5、创建对象
6、代码如下
//// ViewController.m// 录音//// Created by 韩燕辉 on 16/9/7.// Copyright © 2016年 hyh. All rights reserved.//#import "ViewController.h"#import@interface ViewController ()
@property(nonatomic,strong)AVAudioRecorder *recorder;
@property (weak, nonatomic) IBOutlet UILabel *pathUILable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/**
* 懒加载avaudiorecorder对象
*
* @return AVAudioRecorer
*/
-(AVAudioRecorder *)recorder
{
if (_recorder == nil) {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"];
self.pathUILable.text = filePath;
NSLog(@"%@",filePath);
NSURL *url = [NSURL URLWithString:filePath];
NSDictionary *settingRecorder = @{AVFormatIDKey:@(kAudioFormatLinearPCM),AVSampleRateKey : @(8000)};
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settingRecorder error:nil];
}
return _recorder;
}
/**
* 开始录音
*
* @param sender void
*/
- (IBAction)recorder:(id)sender {
[self.recorder record];
}
- (IBAction)stopRecorder:(id)sender {
[self.recorder stop];
}
@end
网友评论