IOS-recording function

Source: Internet
Author: User

Initialize AVAudioRecorder and set the recording parameters and storage address:

// Set recording * recordsetting = [[audio alloc] init] autorelease]; // set the recording format avformatidkey = kaudioformatlinearpcm [recordsetting setvalue: [nsnumber numberwithint: Encrypted] forkey: avformatidkey]; // sets the recording sampling rate (HZ), for example, avsampleratekey = 8000/44100/96000 (affects audio quality) [recordsetting setvalue: [nsnumber numberwithfloat: 44100] forkey: avsampleratekey]; // Number of recording channels 1 or 2 [recordsetting setvalue: [nsnumber numberwithint: 1] forkey: avnumberofchannelskey]; // Number of linear sampling digits 8, 16, 24, 32 [recordsetting setvalue: [nsnumber numberwithint: 16] forkey: audio]; // recording quality [recordsetting setvalue: [nsnumber numberwithint: avaudioqualityhigh] forkey: weight]; nsstring * strurl = [nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; nsurl * url = [nsurl fileurlwithpath: [nsstring stringwithformat: @ "% @/lll. AAC ", strurl]; urlplay = URL; nserror * error; // initialize recorder = [[avaudiorecorder alloc] initwithurl: URL settings: recordsetting error: & error]; // enable the Volume Detection recorder. meteringenabled = yes; recorder. delegate = self;

Identify the volume size to display different images to show fluctuations in the volume chart

[Recorder updatemeters]; // refresh the volume data // obtain the average volume [Recorder averagepowerforchannel: 0]; // The maximum volume [Recorder peakpowerforchannel: 0]; double lowpassresults = POW (10, (0.05 * [Recorder peakpowerforchannel: 0]); nslog (@ "% lf", lowpassresults ); // up to 50 0 // small image-large 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"];}

Demo: Click to download ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.