導入框架:
#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];
按鈕的觸發事宜
- (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 通道數 平日為雙聲道 值2 2.AVSampleRateKey 采樣率 單元HZ 平日設置成44100 也就是44.1k 3.AVLinearPCMBitDepthKey 比特率 8 16 24 32 4.AVEncoderAudioQualityKey 聲響質量 ① AVAudioQualityMin = 0, 最小的質量 ② AVAudioQualityLow = 0x20, 比擬低的質量 ③ AVAudioQualityMedium = 0x40, 中央的質量 ④ AVAudioQualityHigh = 0x60,高的質量 ⑤ AVAudioQualityMax = 0x7F 最好的質量 5.AVEncoderBitRateKey 音頻編碼的比特率 單元Kbps 傳輸的速度 普通設置128000 也就是128kbps */ NSLog(@"%@",path); }
署理辦法:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ NSLog(@"灌音停止"); // 文件操作的類 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) { // 經由過程比較文件的延展名(擴大名 尾綴) 來辨別是否是灌音文件 if ([audioPath.pathExtension isEqualToString:@"aiff"]) { // 把挑選出來的文件放到數組中 [audioPathList addObject:audioPath]; } } NSLog(@"%@",audioPathList); }
【IOS開辟完成灌音功效】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!