Recording:
1. Setting Global Properties
Nsurl *recordedfile; // Storage Path Avaudioplayer *player; // Play Avaudiorecorder *recorder; // recording NSString *recodername; // file name
2. Set the storage path
NSDateFormatter *formater =[[NSDateFormatter alloc] init]; [Formater Setdateformat:@"Yyyy-mm-dd-hh:mm:ss"]; NSString* Datestr =[formater stringfromdate:[nsdate Date]]; NSString* Path = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask,true) Lastobject] stringbyappendingpathcomponent:[nsstring stringWithFormat:@"%@.AAC", Datestr]]; Recodername= [NSString stringWithFormat:@"%@.AAC", Datestr];recordedfile= [Nsurl Fileurlwithpath:path];
3. Set Audio Session classification
Avaudiosession *session =*sessionerror;[ Session Setcategory:avaudiosessioncategoryplayandrecord Error:&Sessionerror]; if (Session = nil) { }else{ [session Setactive:yes Error:nil];}
4. Start recording
Nsmutabledictionary *recordsetting =[[Nsmutabledictionary alloc]init];//set the recording format[recordsetting setvalue:[nsnumber NUMBERWITHINT:KAUDIOFORMATMPEG4AAC] forkey:avformatidkey];//Set the recording sample rate[Recordsetting setvalue:[nsnumber Numberwithfloat:8000] Forkey:avsampleratekey];//quality of the recording[recordsetting setvalue:[nsnumber Numberwithint:avaudioqualityhigh] forkey:avencoderaudioqualitykey];//number of linear samples 8, 16, 24,[Recordsetting setvalue:[nsnumber Numberwithint: -] Forkey:avlinearpcmbitdepthkey];//number of audio channels 1 or 2[Recordsetting setvalue:[nsnumber Numberwithint:1] Forkey:avnumberofchannelskey];recorder=[[Avaudiorecorder alloc] initwithurl:recordedfile settings:recordsetting Error:nil];//Buffered Recording[Recorder Preparetorecord];//Start Recording[recorder record];//if it is repeated recording requires an emptyPlayer = nil;
5. Stop Recording
// Stop Recording =*= [[Avaudioplayer alloc] Initwithcontentsofurl:recordedfile error:&playererror];
Play
1. Play the recording
//The following settings are set when initializing the playerUInt32 sessioncategory =Kaudiosessioncategory_mediaplayback; Audiosessionsetproperty (Kaudiosessionproperty_audiocategory,sizeof(sessioncategory),&sessioncategory); UInt32 Audiorouteoverride=Kaudiosessionoverrideaudioroute_speaker; Audiosessionsetproperty (Kaudiosessionproperty_overrideaudioroute,sizeof(audiorouteoverride),&audiorouteoverride); Avaudiosession*audiosession =[Avaudiosession sharedinstance];//speaker Playback By default[Audiosession setcategory:avaudiosessioncategoryplayback Error:nil]; [Audiosession Setactive:yes Error:nil];//It is recommended that you set yes before playing, the playback end setting no, this function is to turn on the infrared sensor[[Uidevice Currentdevice] setproximitymonitoringenabled:yes];//Add Listener[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selecto R (sensorstatechange:) Name:@"uideviceproximitystatedidchangenotification" Object: nil];//Start Playback[_myplayer play];
2. Handling Listener Events
-(void) Sensorstatechange: (Nsnotificationcenter *) notification; { // If the phone is placed near the ear at this point, the sound will be output through the handset and darken the screen (save power) if ([[Uidevice currentdevice] proximitystate] = = YES) {[ [avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayandrecord error:nil]; } Else {[ [avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback error:nil];} }
3. Pause Playback
// Turn off Infrared sensing [[Uidevice Currentdevice] setproximitymonitoringenabled:no]; // Pause Playback [_myplayer stop];
69th, OC_ recording voice and playback voice function implementation