iOS 录音
前面 我們講了 iOS下播放音效,播放音樂,這一節(jié)我們主要講一下iOS錄音功能
上一節(jié)地址:http://blog.csdn.net/lwjok2007/article/details/50425324
錄音和播放音樂使用的是同一個框架,都是iOS系統(tǒng)提供的AVFoundation.framwork
首先,還是創(chuàng)建項目 ?
起名:TestAVFoundationRecorder
接下來 導(dǎo)入AVFoundation.framwork
具體操作參照上一節(jié)
完成后項目目錄如下
打開項目默認(rèn)生成ViewController.m 添加代碼
導(dǎo)入頭文件
#import <AVFoundation/AVFoundation.h>創(chuàng)建變量 @property (nonatomic,strong) AVAudioRecorder *audioRecorder;//音頻錄音機(jī) @property (nonatomic,strong) AVAudioPlayer *audioPlayer;//音頻播放器,用于播放錄音文件 @property (nonatomic,strong) NSTimer *timer;//錄音聲波監(jiān)控(注意這里暫時不對播放進(jìn)行監(jiān)控)@property (strong, nonatomic) UIButton *record;//開始錄音 @property (strong, nonatomic) UIButton *pause;//暫停錄音 @property (strong, nonatomic) UIButton *resume;//恢復(fù)錄音 @property (strong, nonatomic) UIButton *stop;//停止錄音 @property (strong, nonatomic) UIProgressView *audioPower;//音頻波動
最后我們實(shí)現(xiàn)一下 個個按鈕點(diǎn)擊之后的動作 ?以及錄音等等?
#pragma mark - 私有方法 /*** 設(shè)置音頻會話*/ -(void)setAudioSession{AVAudioSession *audioSession=[AVAudioSession sharedInstance];//設(shè)置為播放和錄音狀態(tài),以便可以在錄制完之后播放錄音[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];[audioSession setActive:YES error:nil]; }/*** 取得錄音文件保存路徑** @return 錄音文件路徑*/ -(NSURL *)getSavePath{NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSFileManager *fileManager = [NSFileManager defaultManager];if(![fileManager fileExistsAtPath:urlStr]) //如果不存在{NSLog(@"xxx.txt is not exist");}else{NSLog(@"xxx.txt is exist");}urlStr=[urlStr stringByAppendingPathComponent:kRecordAudioFile];NSLog(@"file path:%@",urlStr);NSURL *url=[NSURL fileURLWithPath:urlStr];return url; }///var/mobile/Applications/F0CCA9DC-FFBE-4701-8396-2E6EB9509292/Documents/myRecord.caf ///var/mobile/Applications/F0CCA9DC-FFBE-4701-8396-2E6EB9509292/Documents/myRecord.caf /*** 取得錄音文件設(shè)置** @return 錄音設(shè)置*/ -(NSDictionary *)getAudioSetting{NSMutableDictionary *dicM=[NSMutableDictionary dictionary];//設(shè)置錄音格式[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];//設(shè)置錄音采樣率,8000是電話采樣率,對于一般錄音已經(jīng)夠了[dicM setObject:@(8000) forKey:AVSampleRateKey];//設(shè)置通道,這里采用單聲道[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];//每個采樣點(diǎn)位數(shù),分為8、16、24、32[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];//是否使用浮點(diǎn)數(shù)采樣[dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];//....其他設(shè)置等return dicM; }/*** 獲得錄音機(jī)對象** @return 錄音機(jī)對象*/ -(AVAudioRecorder *)audioRecorder{if (!_audioRecorder) {//創(chuàng)建錄音文件保存路徑NSURL *url=[self getSavePath];//創(chuàng)建錄音格式設(shè)置NSDictionary *setting=[self getAudioSetting];//創(chuàng)建錄音機(jī)NSError *error=nil;_audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];_audioRecorder.delegate=self;_audioRecorder.meteringEnabled=YES;//如果要監(jiān)控聲波則必須設(shè)置為YESif (error) {NSLog(@"創(chuàng)建錄音機(jī)對象時發(fā)生錯誤,錯誤信息:%@",error.localizedDescription);return nil;}}return _audioRecorder; }/*** 創(chuàng)建播放器** @return 播放器*/ -(AVAudioPlayer *)audioPlayer{if (!_audioPlayer) {NSURL *url=[self getSavePath];NSError *error=nil;_audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];_audioPlayer.numberOfLoops=0;[_audioPlayer prepareToPlay];if (error) {NSLog(@"創(chuàng)建播放器過程中發(fā)生錯誤,錯誤信息:%@",error.localizedDescription);return nil;}}return _audioPlayer; }/*** 錄音聲波監(jiān)控定制器** @return 定時器*/ -(NSTimer *)timer{if (!_timer) {_timer=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(audioPowerChange) userInfo:nil repeats:YES];}return _timer; }/*** 錄音聲波狀態(tài)設(shè)置*/ -(void)audioPowerChange{[self.audioRecorder updateMeters];//更新測量值float power= [self.audioRecorder averagePowerForChannel:0];//取得第一個通道的音頻,注意音頻強(qiáng)度范圍時-160到0CGFloat progress=(1.0/160.0)*(power+160.0);[self.audioPower setProgress:progress]; }#pragma mark - UI事件 /*** 點(diǎn)擊錄音按鈕** @param sender 錄音按鈕*/- (void)recordClick:(UIButton *)sender { // if (![self.audioPlayer isPlaying]) { // [self.audioPlayer play]; // } //if (![self.audioRecorder isRecording]) {[self.audioRecorder record];//首次使用應(yīng)用時如果調(diào)用record方法會詢問用戶是否允許使用麥克風(fēng)self.timer.fireDate=[NSDate distantPast];} }/*** 點(diǎn)擊暫定按鈕** @param sender 暫停按鈕*/ - (void)pauseClick:(UIButton *)sender {if ([self.audioRecorder isRecording]) {[self.audioRecorder pause];self.timer.fireDate=[NSDate distantFuture];} }/*** 點(diǎn)擊恢復(fù)按鈕* 恢復(fù)錄音只需要再次調(diào)用record,AVAudioSession會幫助你記錄上次錄音位置并追加錄音** @param sender 恢復(fù)按鈕*/ - (void)resumeClick:(UIButton *)sender {[self recordClick:sender]; }/*** 點(diǎn)擊停止按鈕** @param sender 停止按鈕*/ - (void)stopClick:(UIButton *)sender {[self.audioRecorder stop];self.timer.fireDate=[NSDate distantFuture];self.audioPower.progress=0.0; }#pragma mark - 錄音機(jī)代理方法 /*** 錄音完成,錄音完成后播放錄音** @param recorder 錄音機(jī)對象* @param flag 是否成功*/ -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{if (![self.audioPlayer isPlaying]) {[self.audioPlayer play];}NSLog(@"錄音完成!"); }到此 我們基本實(shí)現(xiàn)了功能
運(yùn)行項目看看?
點(diǎn)擊錄音 看看 是不是 可以是用來 。點(diǎn)擊停止之后錄音結(jié)束并且自動開發(fā)播放錄音
項目代碼我們會上傳群空間【51230AVFoundationRecorder.zip】?
蘋果開發(fā)群 :512298106 ?歡迎加入 ?歡迎討論問題
總結(jié)
- 上一篇: 使用原生js实现简单动画效果
- 下一篇: SCM提升供应链管理效率