iOS recording audio

Source: Internet
Author: User

Import the above 2 frames #import <AVFoundation/AVFoundation.h> #import <corefoundation/corefoundation.h>//   Declaration of the following 4 attributes @property (nonatomic, assign) BOOL recording; Determine if you can record @property (nonatomic, strong) NSString *filename;  The audio file is stored with the file name @property (nonatomic, strong) Avaudiorecorder *recorder;   Declare a Recorder @property (nonatomic, strong) Avaudioplayer *player;    Declares an audio player-(void) startrecording{if (self.recording) {return; } self.recording =yes;//set the recording format avformatidkey==kaudioformatlinearpcm//set the recording sample rate (HZ) such as: avsampleratekey==8000/44100 /96000 (affects the quality of audio)//Recording channels 1 or 2 avnumberofchannelskey//linear sample digits 8, 16, 24, avlinearpcmbitdepthkey//recording quality A Vencoderaudioqualitykey = = avaudioqualityhigh//Avlinearpcmisbigendiankey big or small end, is the way of memory Organization//Avlinearpcmisfloatkey Mining The sample signal is an integer or floating-point number nsdictionary *audiosettingdict =[nsdictionary dictionarywithobjectsandkeys: [NSN Umber Numberwithfloat:8000],avsampleratekey, [NSNumber numberwITHINT:KAUDIOFORMATLINEARPCM], Avformatidkey, [NSNumber Numberwi                            Thint:1],avnumberofchannelskey, [NSNumber Numberwithint:16],avlinearpcmbitdepthkey, [NSNumber Numberwithbool:no],avlinearpcmisbigendiankey, [NSNumber Numberwithboo L:no],avlinearpcmisfloatkey, [NSNumber Numberwithint:avaudioqualityhigh],avencoderaudioqualityk    EY, nil];    Nserror *error =nil;    Avaudiosession * audiosession = [avaudiosession sharedinstance]; [Audiosession Setcategory:avaudiosessioncategoryplayandrecord error:&error];//Sets the audio category, which means that when the app starts, the background other audio is stopped [    Audiosession Setactive:yes error:&error];//Set current app audio active nsdate *now =[nsdate Date];    Instantiate a NSDateFormatter object nsdateformatter *dateformater = [[NSDateFormatter alloc] init]; Set the time format, where you can set the format you want [Dateformater setdateformat:@ "Yyyy-mm-dd hh-Mm-ss "];    NSString *filename = [NSString stringwithformat:@ "Rec_%@.wav", [Dateformater Stringfromdate:now]];    Self.filename =filename;    NSString *filepath =[nsstring Documentpathwith:filename];    Nsurl *fileurl =[nsurl Urlwithstring:filepath];        Initialize the recorded class Self.recorder =[[avaudiorecorder alloc]initwithurl:fileurl settings:audiosettingdict error:&error];    [Self.recorder Preparetorecord];    [Self.recorder peakpowerforchannel:0];    [Self.recorder Setmeteringenabled:yes]; [Self.recorder record];}

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.