IOS Audio Recording

Source: Internet
Author: User
Tags uikit

New empty Applicaton, add Homeviewcontroller file. Just look at the code, it's too boring and the theory will be mentioned in the code.

HomeViewController.h code: #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface Homeviewcontroller:uiviewcontroller <avaudiorecorderdelegate, avaudioplayerdelegate>{} @property (Nonatomic, Retain) Avaudiorecorder *audiorecorder;   @property (nonatomic, retain) Avaudioplayer *audioplayer; -(NSString *) Audiorecordingpath; -(Nsdictionary *) audiorecordingsettings;

@end

&NBSP;HOMEVIEWCONTROLLER.M code:   #import "HomeViewController.h"   @interface Homeviewcontroller ()   @end   @implementation Homeviewcontroller   @synthesize Audioplayer; @synthesize Audiorecorder;  //Setting the location of recorded audio files-(NSString *) audiorecordingpath{      nsstring *result = nil;     Nsarray *folders = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);     NSString *documentsfolde = [folders objectatindex:0];     result = [Documentsfolde stringbyappendingpathcomponent:@ "recording.m4a"];     return (result);  //Before initializing the Avaudiorecord instance, basic recording settings are required-(nsdictionary *) audiorecordingsettings{      Nsdictionary *result = nil;          Nsmutabledictionary *settings = [[[Nsmutabledictionary alloc] init] autorelease];// Required parameter settings for recording          [Settings Setvalue:[nsnumber Numberwithinteger: Kaudioformatapplelossless]&nbsP                 Forkey:avformatidkey];          [Settings Setvalue:[nsnumber numberwithfloat:44100.0f] Forkey:avsampleratekey];          [Settings Setvalue:[nsnumber numberwithinteger:1] Forkey:avnumberofchannelskey];          [Settings Setvalue:[nsnumber numberwithinteger:avaudioqualitylow]                  Forkey:avencoderaudioqualitykey];          result = [Nsdictionary dictionarywithdictionary:settings];          return (result);  //Stop audio Recording-(void) Stoprecordingonaudiorecorder: (Avaudiorecorder *) recorder{      [Recorder stop ];  //Audiorecorderdidfinishrecording:successfully: Method-(void) When Avaudiorecorder object recording is terminated Audiorecorderdidfinishrecording: (Avaudiorecorder *) Recorder successfully: (BOOL) flag{     //If flag is true , representing the normal end of the recording, using AVAUdioplayer it out, otherwise logging failed cause     if (flag = YES) {        NSLOG (@) recording completed. ");         nserror *playbackerror = nil;         nserror *readingerror = nil;         NSData *filedata = [NSData datawithcontentsoffile:[self Audiorecordingpath] Options: nsdatareadingmapped error:&readingerror];                  Avaudioplayer *newplayer = [[Avaudioplayer alloc] Initwithda ta:filedata                                                                  &N bsp;error:&playbackerror];                  Self.audioplayer = Newplayer;         [Newplayer release];                  if (self.audioplayer!= nil) {            self.audioPlayer.delegate = self;           & nbsp if ([self.audioplayer preparetoplay] = YES &&                  [self Audioplayer Play] = = YES) {                NSLOG (@) to begin playback of recorded audio. ");            } else {                NSLOG (@) cannot play recorded Audio. ");                    }else {            NSLog (@) audio playback failed. ");        }             } else {        NSLOG (@ "The recording process terminated unexpectedly. ");    }     Self.audiorecorder = nil; }  -(void) viewdidload {    [super Viewdidload];          Nserror *error = nil;     NSString *pathofrecordingfile = [Self audioreCordingpath];          Nsurl *audiorecordingurl = [Nsurl fileurlwithpath:pathofrecordingfile];          Avaudiorecorder *newrecorder = [[Avaudiorecorder alloc]        &NBSP ;                             Initwithurl:audiorecordi ngurl                                &NBSP ;     Settings:[self Audiorecordingsettings]                                     error:&error];     Self.audiorecorder = Newrecorder;     [Newrecorder release];          if (Self.audiorecorder!= nil) {        Self.audioRecorder.delegate = self;                  if ([Self.audiorecorder preparetorEcord] = YES &&             [self.audiorecorder record] = yes] {    &NB Sp      //If Audiorecorder is successfully instantiated, start recording the sound. And the Performselector method is set to execute the Stoprecordingonaudiorecorder method after recording the sound 10s to stop recording             NSLog (@ "recording sound begins.) ");                          [self performselector: @selector ( Stoprecordingonaudiorecorder:                        withobje Ct:self.audioRecorder                        afterdelay:10.0f] ;                     } else {          &N Bsp NSLog (@) recording failed. ");             Self.audiorecorder =nil;        }} else {    NSLog (@) Auiorecorder instance creation failed. "); } }  -(void) Viewdidunload{      if (Self.audiorecorder!= nil) {        if ([Self.audiorecorder Isrecor Ding] = YES) {            [Self.audiorecorder stop];        }         Self.audiorecorder = nil;    }          if (Self.audioplayer!= nil) {        if ([Self.aud Ioplayer isplaying] = YES) {            [self.audioplayer stop];       &NBS P }         Self.audioplayer = nil;    }  -(void) dealloc{      [Audioplayer release];     [Audiorecorder release ];     [Super Dealloc]; }   @end  

PS: Required parameter settings for recording audio

Avformatidkey The format for recording audio.

KAUDIOFORMATLINEARPCM:LPCM format

kaudioformatac3:ac-3 format

KAUDIOFORMATMPEG4AAC:AAC format

KAUDIOFORMATMPEG4CELP:CELP format

KAUDIOFORMATMPEG4HVXC:HVXC format

KAUDIOFORMATMPEG4LAYER1:MP1 format

KAUDIOFORMATMPEG4LAYER2:MP2 format

Kaudioformatmpeg4layer3:mp3 format

Kaudioformattimecode:time format

Kaudioformatmidistream:midi format

Kaudioformatapplelossless:alac format

Video used when recording audio Avsampleratekey

avnumberofchannelskey number of channels when recording audio

Quality of Avencoderaudioqualitykey recorded audio

Avaudioqualitymin

Avaudioqualitylow

Avaudioqualitymedium

Avaudioqualityhigh

Avaudioqualitymax

Related Article

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.