IOS开发实现录音功能
<p><strong>作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS交流群:812157648,不管你是小白还是大牛欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!</strong></p><p>导入框架:</p>
<pre><code>#import <AVFoundation/AVFoundation.h>
</code></pre>
<p>声明全局变量:</p>
<pre><code>@interface ViewController ()<AVAudioRecorderDelegate>
{
AVAudioRecorder *audioRecorder;
}
@end
</code></pre>
<p>在ViewDidLoad中:</p>
<pre><code>UIButton *button = ;
button.frame = CGRectMake(100, 100, 100, 100);
;
button.backgroundColor = ;
;
;
</code></pre>
<p>按钮的触发事件:</p>
<pre><code>sender.selected = !sender.selected;
if (sender.selected != YES) {
;
return ;
}
//URL是本地的URL AVAudioRecorder需要一个存储的路径
NSString *name = .timeIntervalSince1970];
NSString *path = ;
NSError *error;
//录音机 初始化
_audioRecorder = [initWithURL: settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];
;
;
_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);
</code></pre>
<p>代理方法:</p>
<pre><code>- ( void )audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:( BOOL )flag{
NSLog(@ "录音结束" );
//文件操作的类
NSFileManager *manger = ;
NSString *path = ;
//获得当前文件的所有子文件subpathsAtPath
NSArray *pathlList = ;
//需要只获得录音文件
NSMutableArray *audioPathList = ;
//遍历所有这个文件夹下的子文件
for (NSString *audioPath in pathlList) {
// 通过对比文件的延展名(扩展名 尾缀) 来区分是不是录音文件
if () {
// 把筛选出来的文件放到数组中
;
}
}
NSLog(@ "%@" ,audioPathList);
}
</code></pre>
<p>原文作者:复杂化<br>
原文地址:https://blog.csdn.net/m0_46110288/article/details/110572498?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160743835219725211951213%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=160743835219725211951213&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2<sub>all</sub>first_rank_v2~rank_v29-6-110572498.pc_search_result_cache&utm_term=iOS%E5%BC%80%E5%8F%91</p><br><br>
来源:https://www.cnblogs.com/fadaijun/p/14106177.html
頁:
[1]