1. 当程序启动时,界面会出现录音按钮。
2. 用户点击录音按钮,按钮变色,进入录音状态。
3. 录音状态下,用户点击录音按钮,录音结束,播放按钮出现
4. 用户点击播放按钮,进入播放录音状态,播放录音时,录音按钮不可用。
下面就来一步一步的实现这个简单的录音笔应用:
首先,新起项目,并把AVFoundation FrameWork加入到当前项目中:
然后,创建MainViewController,并引入AVFoundation.h文件,创建AVAduioPlayer和AVAudioRecorder属性,以及界面元素需要的属性,同时定义点击按钮时需要使用的方法:
#import#import @interface MainViewController : UIViewController @property (strong, nonatomic) AVAudioRecorder *audioRecorder; @property (strong, nonatomic) AVAudioPlayer *audioPlayer; @property (nonatomic, weak) IBOutlet UIButton *recordButton; @property (nonatomic, weak) IBOutlet UIButton *playButton; -(IBAction)recordButtonAction:(id)sender; -(IBAction)playButtonAction:(id)sender; @end
```
接着,把UI元素和响应的属性,方法绑定起来:
再接下来,在界面加载(viewDidLoad)的时候,隐藏播放按钮并初始化AvdudioRecorder,初始化Recorder需要2个元素:
* 录制效果配置,主要就是配置录制声音时的比特率,采样率等等,更多具体的内容:[Audio配置] http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVFoundationAudioSettings_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40009937
* 录音保存的文件URL
初始化后,调用[self.audioRecorder prepareToRecord]方法,该方法会配置好Recorder并创建好录音文件。
- (void)viewDidLoad { [super viewDidLoad]; self.playButton.hidden = YES; //配置Recorder, NSDictionary *recordSetting = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey, [NSNumber numberWithInt:16],AVEncoderBitRateKey, [NSNumber numberWithInt:2],AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0],AVSampleRateKey, nil]; //录音文件保存地址的URL NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.caf", [[NSBundle mainBundle] resourcePath]]]; NSError *error = nil; self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error]; if (error != nil) { NSLog(@"Init audioRecorder error: %@",error); }else{ //准备就绪,等待录音,注意该方法会返回Boolean,最好做个成功判断,因为其失败的时候无任何错误信息抛出 if ([self.audioRecorder prepareToRecord]) { NSLog(@"Prepare successful"); } } }
配置好Recorder之后,开始,实现录音按钮,录音按钮功能非常简单,只需要判断当前Recorder处于录音状态则调用stop方法结束录音,否则调用record方法开始录音,同时更新UI元素。
-(IBAction)recordButtonAction:(id)sender { if (!self.audioRecorder.recording) { self.playButton.hidden = YES; [self.audioRecorder record]; [self.recordButton setImage:[UIImage imageNamed:@"MicButtonPressed.png"] forState:UIControlStateNormal]; }else { self.playButton.hidden = NO; [self.audioRecorder stop]; [self.recordButton setImage:[UIImage imageNamed:@"MicButton.png"] forState:UIControlStateNormal]; } }
录音结束之后,播放按钮会出现,接下来实现播放按钮的点击效果,其实现和Record按钮差不多, 判断当前的Player处于播放状态则调用pause方法停止播放,否则,初始化Player,然后调用play方法播放按钮。AVAudioPlayer的初始化非常简单,只需要知道声音文件的URL即可。
-(IBAction)playButtonAction:(id)sender { if (!self.audioPlayer.playing) { self.recordButton.hidden = YES; NSError *error; NSLog(@"%@",self.audioRecorder.url); self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioRecorder.url error:&error]; self.audioPlayer.delegate = self; if (error != nil) { NSLog(@"Wrong init player:%@", error); }else{ [self.audioPlayer play]; } [self.playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal]; }else { self.recordButton.hidden = NO; [self.audioPlayer pause]; [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; } }
当声音文件播放结束时,需要通知主程序,这就涉及到使用AVAudioPlayerDelegate,顺道我们也可以实现AVAudioRecorderDelegate以记录Recorder的状态。
#pragma mark audio delegate -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { NSLog(@"Finsh playing"); self.recordButton.hidden = NO; [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; } -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { NSLog(@"Decode Error occurred"); } -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag { NSLog(@"Finish record!"); } -(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error { NSLog(@"Encode Error occurred"); }
That's All! 本文只是对AVAudioRecorder,AVAudioPlayer做了最简单的介绍,更多的内容请参考苹果的开发者文档:
* AVAudioRecorder: http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html#//apple_ref/occ/instp/AVAudioRecorder/settings
* AVAudioPlayer: http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
另外,这儿还有一篇不错的分析iPhone中使用音频的文章: http://www.raywenderlich.com/zh-hans/20608/iphone%E5%BC%80%E5%8F%91%E4%B9%8B%E9%9F%B3%E9%A2%91101-%E7%AC%AC%E4%B8%80%E9%83%A8%E5%88%86%EF%BC%9A-%E6%96%87%E4%BB%B6%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B
所有的源代码在这儿:https://github.com/xianlinbox/AudioDemo