国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS開發實現錄音功能

2020-07-26 03:26:00
字體:
來源:轉載
供稿:網友

導入框架:

#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);  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蚌埠市| 凌海市| 扶余县| 娄烦县| 牡丹江市| 清水县| 札达县| 特克斯县| 壤塘县| 集安市| 商水县| 衡山县| 铜梁县| 舟曲县| 三明市| 区。| 兴安县| 娄烦县| 鹿泉市| 绥德县| 汉沽区| 虞城县| 和静县| 广安市| 肃南| 连州市| 齐河县| 盐山县| 长白| 海林市| 根河市| 石棉县| 金坛市| 哈巴河县| 姜堰市| 赤壁市| 洛阳市| 嘉黎县| 洪江市| 潜江市| 措美县|