IOS開發(fā)實現(xiàn)錄音功能
更新時間:2016年03月16日 10:13:02 投稿:hebedich
本文給大家分享的是一個IOS開發(fā)中實現(xiàn)錄音功能的實例,并簡單給大家解析一下,有需要的小伙伴可以參考下
導入框架:
#import <AVFoundation/AVFoundation.h>
聲明全局變量:
@interface ViewController ()<AVAudioRecorderDelegate> { AVAudioRecorder *audioRecorder; } @end
在ViewDidLoad中:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); [button setTitle:@"TICK" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; [button addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
按鈕的觸發(fā)事件
- (void)startAudioRecoder:(UIButton *)sender{ sender.selected = !sender.selected; if (sender.selected != YES) { [audioRecorder stop]; return; } // URL是本地的URL AVAudioRecorder需要一個存儲的路徑 NSString *name = [NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970]; NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name]; NSError *error; // 錄音機 初始化 audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error]; [audioRecorder prepareToRecord]; [audioRecorder record]; audioRecorder.delegate = self; /* 1.AVNumberOfChannelsKey 通道數(shù) 通常為雙聲道 值2 2.AVSampleRateKey 采樣率 單位HZ 通常設(shè)置成44100 也就是44.1k 3.AVLinearPCMBitDepthKey 比特率 8 16 24 32 4.AVEncoderAudioQualityKey 聲音質(zhì)量 ① AVAudioQualityMin = 0, 最小的質(zhì)量 ② AVAudioQualityLow = 0x20, 比較低的質(zhì)量 ③ AVAudioQualityMedium = 0x40, 中間的質(zhì)量 ④ AVAudioQualityHigh = 0x60,高的質(zhì)量 ⑤ AVAudioQualityMax = 0x7F 最好的質(zhì)量 5.AVEncoderBitRateKey 音頻編碼的比特率 單位Kbps 傳輸?shù)乃俾?一般設(shè)置128000 也就是128kbps */ NSLog(@"%@",path); }
代理方法:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ NSLog(@"錄音結(jié)束"); // 文件操作的類 NSFileManager *manger = [NSFileManager defaultManager]; NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 獲得當前文件的所有子文件subpathsAtPath NSArray *pathlList = [manger subpathsAtPath:path]; // 需要只獲得錄音文件 NSMutableArray *audioPathList = [NSMutableArray array]; // 遍歷所有這個文件夾下的子文件 for (NSString *audioPath in pathlList) { // 通過對比文件的延展名(擴展名 尾綴) 來區(qū)分是不是錄音文件 if ([audioPath.pathExtension isEqualToString:@"aiff"]) { // 把篩選出來的文件放到數(shù)組中 [audioPathList addObject:audioPath]; } } NSLog(@"%@",audioPathList); }
相關(guān)文章
iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下2016-04-04UIMenuController在Cell內(nèi)部無法顯示的解決辦法(iOS9.2)
這篇文章主要為大家詳細介紹了UIMenuController在Cell內(nèi)部無法顯示的解決辦法,感興趣的小伙伴們可以參考一下2016-08-08解決蘋果ios用js的Date()出現(xiàn)NaN的問題
下面小編就為大家分享一篇解決蘋果ios用js的Date()出現(xiàn)NaN的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03