录音简单使用
来源:互联网 发布:java高级工程师面试题 编辑:程序博客网 时间:2024/05/13 13:12
#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
0 0
- 录音简单使用
- 使用MediaPlayer实现简单的录音
- Android recording 录音功能 简单使用小实例
- 通话录音和CoreTelephony框架的简单使用
- iOS - 简单录音:
- Android 简单录音程序
- iOS录音简单实现
- ubuntu 下简单录音
- S40使用J2ME录音
- 使用AVAudioRecorder 录音
- 使用AVAudioRecorder 录音
- 使用waveIn进行录音
- 使用mciSendCommand实现录音
- 使用waveIn进行录音
- 使用MediaRecorder保存录音
- 录音的基本使用
- 录音的使用
- 使用蓝牙mic录音
- 如何快速掌握一门技术
- 四大组件之BroadCastReceiver(无序广播和有序广播)
- 国内一线互联网公司内部面试题库
- 单词长度-week8-C语言习题集
- 127. Word Ladder
- 录音简单使用
- 使用Sax生成xml文件
- Count of Smaller Numbers After Self
- 七步从AngularJS菜鸟到专家(6):服务
- 实验项目2-13:两个有序序列的中位数
- Android自定义View自定义圆形Dialog
- MySQL优化之一--硬件和操作系统
- JAVA学习笔记--线程请求Json数据,并解析绑定到listView
- swipmenulistview+pulltorefresh整合