AVFoundation框架实现录音和播放(AVAudioRecorder、AVAudioPlayer)

最近实现了一个简单功能,类似微信发送语音,按下录音,松开结束录音;并且可播放;


需要导入

#import <AVFoundation/AVFoundation.h>

利用此框架中的

AVAudioRecorder和AVAudioPlayer来录音和播放

以下是AVAudioRecorder录音的使用方法:

[cpp]  view plain copy
  1. - (IBAction)downAction:(id)sender {  
  2.     //按下录音  
  3.     if ([self canRecord]) {  
  4.   
  5.         NSError *error = nil;  
  6.         //必须真机上测试,模拟器上可能会崩溃  
  7.         recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error];  
  8.           
  9.         if (recorder) {  
  10.             //是否允许刷新电平表,默认是off  
  11.             recorder.meteringEnabled = YES;  
  12.             //创建文件,并准备录音  
  13.             [recorder prepareToRecord];  
  14.             //开始录音  
  15.             [recorder record];  
  16.               
  17.             //启动定时器,为了更新电平  
  18.             timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];  
  19.               
  20.         } else  
  21.         {  
  22.             int errorCode = CFSwapInt32HostToBig ([error code]);  
  23.             NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);  
  24.               
  25.         }  
  26.     }  
  27.      
  28. }  
  29.   
  30. - (IBAction)upAction:(id)sender {  
  31.     //松开 结束录音  
  32.       
  33.     //录音停止  
  34.     [recorder stop];  
  35.     recorder = nil;  
  36.     //结束定时器  
  37.     [timer invalidate];  
  38.     timer = nil;  
  39.     //图片重置  
  40.     soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]];  
  41.       
  42. }  


以下是AVAudioPlayer播放器的使用方法:

[cpp]  view plain copy
  1. - (IBAction)playAction:(id)sender {  
  2.       
  3.     NSError *playerError;  
  4.       
  5.     //播放  
  6.     player = nil;  
  7.     player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:&playerError];  
  8.       
  9.     if (player == nil)  
  10.     {  
  11.         NSLog(@"ERror creating player: %@", [playerError description]);  
  12.     }else{  
  13.         [player play];  
  14.     }  
  15.       
  16. }  

如果是7.0,第一次运行会提示,是否允许使用麦克风:


7.0需要设置:

[cpp]  view plain copy
  1. if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)  
  2.     {  
  3.         //7.0第一次运行会提示,是否允许使用麦克风  
  4.         AVAudioSession *session = [AVAudioSession sharedInstance];  
  5.         NSError *sessionError;  
  6.         //AVAudioSessionCategoryPlayAndRecord用于录音和播放  
  7.         [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];  
  8.         if(session == nil)  
  9.             NSLog(@"Error creating session: %@", [sessionError description]);  
  10.         else  
  11.             [session setActive:YES error:nil];  
  12.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值