欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS-录音功能

程序员文章站 2022-05-18 10:21:33
初始化avaudiorecorder并设置录音的参数和存储地址: [cpp] //录音设置       nsmutabl...

初始化avaudiorecorder并设置录音的参数和存储地址:


[cpp]
//录音设置  
    nsmutabledictionary *recordsetting = [[[nsmutabledictionary alloc]init] autorelease]; 
    //设置录音格式  avformatidkey==kaudioformatlinearpcm  
    [recordsetting setvalue:[nsnumber numberwithint:kaudioformatmpeg4aac] forkey:avformatidkey]; 
    //设置录音采样率(hz) 如:avsampleratekey==8000/44100/96000(影响音频的质量)  
    [recordsetting setvalue:[nsnumber numberwithfloat:44100] forkey:avsampleratekey]; 
    //录音通道数  1 或 2  
    [recordsetting setvalue:[nsnumber numberwithint:1] forkey:avnumberofchannelskey]; 
    //线性采样位数  8、16、24、32  
    [recordsetting setvalue:[nsnumber numberwithint:16] forkey:avlinearpcmbitdepthkey]; 
    //录音的质量  
    [recordsetting setvalue:[nsnumber numberwithint:avaudioqualityhigh] forkey:avencoderaudioqualitykey]; 
     
    nsstring *strurl = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; 
    nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/lll.aac", strurl]]; 
    urlplay = url; 
     
    nserror *error; 
    //初始化  
    recorder = [[avaudiorecorder alloc]initwithurl:url settings:recordsetting error:&error]; 
    //开启音量检测  
    recorder.meteringenabled = yes; 
    recorder.delegate = self; 

//录音设置
    nsmutabledictionary *recordsetting = [[[nsmutabledictionary alloc]init] autorelease];
    //设置录音格式  avformatidkey==kaudioformatlinearpcm
    [recordsetting setvalue:[nsnumber numberwithint:kaudioformatmpeg4aac] forkey:avformatidkey];
    //设置录音采样率(hz) 如:avsampleratekey==8000/44100/96000(影响音频的质量)
    [recordsetting setvalue:[nsnumber numberwithfloat:44100] forkey:avsampleratekey];
    //录音通道数  1 或 2
    [recordsetting setvalue:[nsnumber numberwithint:1] forkey:avnumberofchannelskey];
    //线性采样位数  8、16、24、32
    [recordsetting setvalue:[nsnumber numberwithint:16] forkey:avlinearpcmbitdepthkey];
    //录音的质量
    [recordsetting setvalue:[nsnumber numberwithint:avaudioqualityhigh] forkey:avencoderaudioqualitykey];
   
    nsstring *strurl = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
    nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/lll.aac", strurl]];
    urlplay = url;
   
    nserror *error;
    //初始化
    recorder = [[avaudiorecorder alloc]initwithurl:url settings:recordsetting error:&error];
    //开启音量检测
    recorder.meteringenabled = yes;
    recorder.delegate = self;

 

识别音量的大小,从而显示不同的图片,来呈现出音量图波动


[cpp]
[recorder updatemeters];//刷新音量数据  
    //获取音量的平均值  [recorder averagepowerforchannel:0];  
    //音量的最大值  [recorder peakpowerforchannel:0];  
     
    double lowpassresults = pow(10, (0.05 * [recorder peakpowerforchannel:0])); 
    nslog(@"%lf",lowpassresults); 
    //最大50  0  
    //图片 小-》大  
    if (0<lowpassresults<=0.06) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_01.png"]]; 
    }else if (0.06<lowpassresults<=0.13) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_02.png"]]; 
    }else if (0.13<lowpassresults<=0.20) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_03.png"]]; 
    }else if (0.20<lowpassresults<=0.27) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_04.png"]]; 
    }else if (0.27<lowpassresults<=0.34) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_05.png"]]; 
    }else if (0.34<lowpassresults<=0.41) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_06.png"]]; 
    }else if (0.41<lowpassresults<=0.48) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_07.png"]]; 
    }else if (0.48<lowpassresults<=0.55) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_08.png"]]; 
    }else if (0.55<lowpassresults<=0.62) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_09.png"]]; 
    }else if (0.62<lowpassresults<=0.69) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_10.png"]]; 
    }else if (0.69<lowpassresults<=0.76) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_11.png"]]; 
    }else if (0.76<lowpassresults<=0.83) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_12.png"]]; 
    }else if (0.83<lowpassresults<=0.9) { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_13.png"]]; 
    }else { 
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_14.png"]]; 
    } 

[recorder updatemeters];//刷新音量数据
    //获取音量的平均值  [recorder averagepowerforchannel:0];
    //音量的最大值  [recorder peakpowerforchannel:0];
   
    double lowpassresults = pow(10, (0.05 * [recorder peakpowerforchannel:0]));
    nslog(@"%lf",lowpassresults);
    //最大50  0
    //图片 小-》大
    if (0<lowpassresults<=0.06) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_01.png"]];
    }else if (0.06<lowpassresults<=0.13) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_02.png"]];
    }else if (0.13<lowpassresults<=0.20) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_03.png"]];
    }else if (0.20<lowpassresults<=0.27) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_04.png"]];
    }else if (0.27<lowpassresults<=0.34) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_05.png"]];
    }else if (0.34<lowpassresults<=0.41) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_06.png"]];
    }else if (0.41<lowpassresults<=0.48) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_07.png"]];
    }else if (0.48<lowpassresults<=0.55) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_08.png"]];
    }else if (0.55<lowpassresults<=0.62) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_09.png"]];
    }else if (0.62<lowpassresults<=0.69) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_10.png"]];
    }else if (0.69<lowpassresults<=0.76) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_11.png"]];
    }else if (0.76<lowpassresults<=0.83) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_12.png"]];
    }else if (0.83<lowpassresults<=0.9) {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_13.png"]];
    }else {
        [self.imageview setimage:[uiimage imagenamed:@"record_animate_14.png"]];
    }