IOS开发实现录音功能

编程入门 行业动态 更新时间:2024-10-11 15:17:20

IOS开发实现<a href=https://www.elefans.com/category/jswz/34/1743415.html style=录音功能"/>

IOS开发实现录音功能

导入框架:

#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];

按钮的触发事件

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 通道数 通常为双声道 值22.AVSampleRateKey 采样率 单位HZ 通常设置成44100 也就是44.1k3.AVLinearPCMBitDepthKey 比特率 8 16 24 324.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];
//  获得当前文件的所有子文件subpathsAtPathNSArray *pathlList = [manger subpathsAtPath:path];//  需要只获得录音文件NSMutableArray *audioPathList = [NSMutableArray array];
//  遍历所有这个文件夹下的子文件for (NSString *audioPath in pathlList) {
//    通过对比文件的延展名(扩展名 尾缀) 来区分是不是录音文件if ([audioPath.pathExtension isEqualToString:@ "aiff" ]) {
//      把筛选出来的文件放到数组中[audioPathList addObject:audioPath];}}NSLog(@ "%@" ,audioPathList);}

更多推荐

IOS开发实现录音功能

本文发布于:2024-03-07 18:55:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:录音功能   IOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!