69th, OC_ recording voice and playback voice function implementation

Source: Internet
Author: User

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

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.