1婆跑、懶加載一個AVAudioRecorer類對象
2、設(shè)置保存的沙盒地址
3庭呜、將沙盒的地址轉(zhuǎn)換成NSURl對象
4滑进、用字典設(shè)置屬性
5、創(chuàng)建對象
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