Create empty applicaton and add the homeviewcontroller file. Let's look at the code. The theory is too boring and will be mentioned in the code.
Homeviewcontroller. H code:
# Import <uikit/uikit. h> # import <avfoundation/avfoundation. h> @ interface homeviewcontroller: uiviewcontroller <strong, avaudioplayerdelegate >{}@ property (nonatomic, retain) avaudiorecorder * audiorecorder; @ property (nonatomic, retain) avaudioplayer * audioplayer; -(nsstring *) audiorecordingpath;-(nsdictionary *) audiorecordingsettings;
@ End
Homeviewcontroller. m code:
# Import "homeviewcontroller. H "@ interface homeviewcontroller () @ end @ implementation homeviewcontroller @ synthesize audioplayer; @ synthesize audiorecorder; // set the location of the recorded audio file-(nsstring *) audiorecordingpath {nsstring * result = nil; nsarray * folders = folder (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsfolde = [Folders objectatindex: 0]; Result = [documentsf Olde stringbyappendingpathcomponent: @ "recording. m4a "]; Return (result) ;}// before initializing an avaudiorecord instance, You need to perform basic recording settings-(nsdictionary *) audiorecordingsettings {nsdictionary * result = nil; nsmutabledictionary * settings = [[[nsmutabledictionary alloc] init] autorelease]; // set the parameters required for recording [settings setvalue: [nsnumber numberwithinteger: Encrypted] forkey: avformatidkey]; [settings setvalue: [nsnumber n Umberwithfloat: 4410010000f] forkey: Keys]; [settings setvalue: [nsnumber numberwithinteger: 1] forkey: Keys]; [settings setvalue: [nsnumber numberwithinteger: avaudioqualitylow] forkey: Keys]; result = [nsdictionary dictionarywithdictionary: settings]; Return (result);} // stop audio recording-(void) stoprecordingonaudiorecorder :( avaudiorecorder *) Recorder {[Recorder stop];} // When the avaudiorecorder object recording ends, the callback: successfully: method-(void) handle :( avaudiorecorder *) Recorder successfully :( bool) Flag {// if the flag is true, indicates that the recording ends normally. Use avaudioplayer to play it out. Otherwise, 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] initwithdata: filedata error: & playbackerror]; self. audioplayer = newplayer; [newplayer release]; If (self. audioplayer! = Nil) {self. audioplayer. delegate = self; If ([self. audioplayer preparetoplay] = Yes & [self. audioplayer play] = Yes) {nslog (@ "start playing the recorded audio! ");} Else {nslog (@" you cannot play the recorded audio! ") ;}} Else {nslog (@" audio playback failed! ") ;}} Else {nslog (@" unexpected termination of the recording process! ");} Self. audiorecorder = nil;}-(void) viewdidload {[Super viewdidload]; nserror * error = nil; nsstring * pathofrecordingfile = [self audiorecordingpath]; nsurl * response = [nsurl response: pathofrecordingfile]; avaudiorecorder * newrecorder = [[avaudiorecorder alloc] initwithurl: audiorecordingurl 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) {// If audiorecorder is instantiated successfully, the recording sound starts and the stoprecordingonaudiorecorder method is executed 10 s after the recording sound is set using the performselector method, used to stop the recording nslog (@ "the recording starts! "); [Self defined mselector: @ selector (stoprecordingonaudiorecorder :) withobject: Self. audiorecorder afterdelay: 10.0f];} else {nslog (@" Recording failed! "); Self. audiorecorder = nil ;}} else {nslog (@" auiorecorder instance creation failed! ") ;}}-(Void) viewdidunload {If (self. audiorecorder! = Nil) {If ([self. audiorecorder isrecording] = Yes) {[self. audiorecorder stop];} self. audiorecorder = nil;} If (self. audioplayer! = Nil) {If ([self. audioplayer isplaying] = Yes) {[self. audioplayer stop];} self. audioplayer = nil ;}}- (void) dealloc {[audioplayer release]; [audiorecorder release]; [Super dealloc];} @ end
PS: Required parameter settings for recording audio
AvformatidkeyThe format of the recorded 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
AvsampleratekeyVideo used for recording audio
AvnumberofchannelskeyNumber of channels for recording audio
AvencoderaudioqualitykeyQuality of recorded audio
Avaudioqualitymin
Avaudioqualitylow
Avaudioqualitymedium
Avaudioqualityhigh
Avaudioqualitymax