录音简单使用
2016-07-22 16:44
141 查看
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVAudioPlayerDelegate> @property (nonatomic,strong) NSURL *recordedFile; @property (nonatomic,strong) AVAudioRecorder *recorder; @property (nonatomic,strong) AVAudioPlayer *play; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 临近状态检测 bool proximityState = [UIDevice currentDevice].proximityState; NSLog(@"------->%d",proximityState); UIDevice *device = [UIDevice currentDevice]; device.proximityMonitoringEnabled = YES; // 允许临近检测 if (device.proximityMonitoringEnabled == YES ) { // 临近消息触发 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:device]; } // 设置好文件的存储位置 NSFileManager *manager = [NSFileManager defaultManager]; NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [pathArray lastObject]; path = [path stringByAppendingPathComponent:@"RecordedFile"]; bool existsFlag = [manager fileExistsAtPath:path]; if (!existsFlag) { bool isSuccess = [manager createFileAtPath:path contents:nil attributes:nil]; if (isSuccess) { NSLog(@"创建成功"); }else{ NSLog(@"创建失败"); } } _recordedFile = [NSURL fileURLWithPath:path]; AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error; // 开启录音和播放功能 [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; if (session == nil) { NSLog(@"session is error ---->%@",error.description); }else{ [session setActive:true error:nil]; } } - (void)proximityChanged:(NSNotification *)notification{ // 如果此时手机靠近面部, 声音从听筒出来, 屏幕变暗 if ([UIDevice currentDevice].proximityState == YES) { // 接近耳朵 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; }else{ // 没有黑屏的话, 就直接开扬声器。 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; } } - (IBAction)recordAction:(id)sender { // 先设置播放和录音状态 [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; // 初始化录音 _recorder = [[AVAudioRecorder alloc]initWithURL:_recordedFile settings:nil error:nil]; // 准备录音 [_recorder prepareToRecord]; // 开始录音 [_recorder record]; NSLog(@"开始录音....."); } - (IBAction)stopAction:(id)sender { [_recorder stop]; _recorder = nil; NSLog(@"结束录音......"); NSError *error; _play = [[AVAudioPlayer alloc]initWithContentsOfURL:_recordedFile error:&error]; if (_play == nil) { NSLog(@"error is --->%@",error.description); } _play.delegate = self; //设置从扬声器播放 [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil]; } - (IBAction)playerAction:(id)sender { if ([_play isPlaying]) { [_play pause]; }else{ [_play play]; } } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ NSLog(@"播放完成"); } @end
相关文章推荐
- 使用AVAudioRecorder录音的时候模拟器可以录音但是真机不能录音的朋友来看看,这个方法可以解决你的问题哦
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- IOS AVAudioRecorder 录制音频
- iOS AVAudioPlayer AVAudioRecorder 录音和播放
- iOS 使用AVAudioRecorder实现音频的录制
- 录音的处理
- 音视频开发——iOS音频录制(六)
- AVAudioRecorder 录音
- AvaudioRecorder 录音
- smarty文件夹,第一个smarty实例总结
- 动态规划:NOI 2009 管道取珠
- 令人惊叹的浏览器插件
- 仙人球的残影
- 存储概述
- MySQL · 性能优化 · 条件下推到物化表
- tabbar和navigationbar
- ecshop数据库操作函数
- swipmenulistview+pulltorefresh整合
- JAVA学习笔记--线程请求Json数据,并解析绑定到listView
- Android自定义View自定义圆形Dialog