IOS 录音AVAudioRecorder

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容