Record Audio (recording)

Source: Internet
Author: User

1. Import #import <AVFoundation/AVFoundation.h>

2. Implementation Protocol <AVAudioRecorderDelegate>


e.g.

@property (nonatomic, strong) Avaudiorecorder *audiorecorder;

@property (nonatomic, strong) Avaudioplayer *audioplayer;


Path of audio files saved after recording

-(Nsurl *) audiorecordingpath{

Nsfilemanager *filemanager = [[Nsfilemanager alloc] init];

Nsurl *documentsfolderurl = [FileManager urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask

Appropriateforurl:nil Create:no Error:nil];

Nsurl *url =[documentsfolderurl urlbyappendingpathcomponent:@ "recording.m4a"];

return URL;

}

/* Recording options set in dictionary form * *

-(Nsdictionary *) audiorecordingsettings{

Nsdictionary *dic = @{

Avformatidkey: @ (kaudioformatapplelossless),//recording format kaudioformatlinearpcm,kaudioformatapplelossless

Avsampleratekey: @ (44100.0f),//sampling rate

Avnumberofchannelskey: @1,//number of channels

Avencoderaudioqualitykey: @ (Avaudioqualitylow),//recording quality Avaudioqualitymin,avaudioqualitylow,

Avaudioqualitymedium,avaudioqualityhigh,avaudioqualitymax

};

return dic;

}

-(Ibaction) Actionrecord: (ID) Sender {

/* Request Recording Permission * *

Avaudiosession *session = [Avaudiosession sharedinstance];

[Session Setcategory:avaudiosessioncategoryplayandrecord

Withoptions:avaudiosessioncategoryoptionduckothers

Error:nil];

[Session requestrecordpermission:^ (BOOL granted) {

if (granted) {

NSLog (@ "Permission OK to record audio");

[Self startrecordingaudio];

}else {

NSLog (@ "We don ' t have permission to the record audio.");

}

}];

}

-(void) startrecordingaudio{

Nserror *error = nil;

Nsurl *audiorecordingurl = [self audiorecordingpath];

Self.audiorecorder = [[Avaudiorecorder alloc] Initwithurl:audiorecordingurl

Settings:[self Audiorecordingsettings] error:&error];

if (Self.audiorecorder!= nil) {

Self.audioRecorder.delegate = self;

if ([Self.audiorecorder Preparetorecord] &&

[Self.audiorecorder Record]) {

NSLog (@ "successfully started to record.");

* * 5 Seconds to stop recording * *

[Self performselector: @selector (stoprecordingonaudiorecorder:)

WithObject:self.audioRecorder

AFTERDELAY:5.0F];

} else {

NSLog (@ "Failed to record.");

Self.audiorecorder = nil;

}

} else {

NSLog (@ "Failed to create a instance of the audio recorder.");

}

}

-(void) Stoprecordingonaudiorecorder: (Avaudiorecorder *) paramrecorder{

NSLog (@ "Stop to record");

[Paramrecorder stop];

}

The recording was interrupted.

-(void) Audiorecorderbegininterruption: (Avaudiorecorder *) recorder{

NSLog (@ "Recording process is interrupted");

}

-(void) Audiorecorderendinterruption: (Avaudiorecorder *) Recorder withoptions: (nsuinteger) flags{

if (Flags ==avaudiosessioninterruptionoptionshouldresume) {

NSLog (@ "Resuming the recording ...");

[Recorder record];

}

}

To be released at the end, whether it's a recording or a playback.

-(void) audiorecorderdidfinishrecording: (Avaudiorecorder *) Recorder successfully: (BOOL) flag{

if (flag) {

NSLog (@ "Successfully stopped audio recording process.");

} else {

NSLog (@ "Stopping the audio recording failed.");

}

Self.audiorecorder = nil;

}

------Play---------

-(Ibaction) Actionplay: (ID) Sender {

/* Get the recording * *

Nserror *playbackerror = nil;

Nserror *readingerror = nil;

NSData *filedata = [NSData datawithcontentsofurl:[self Audiorecordingpath]

options:nsdatareadingmapped error:&readingerror];

Self.audioplayer = [[Avaudioplayer alloc] Initwithdata:filedata error:&playbackerror];

if (Self.audioplayer!= nil) {

Self.audioPlayer.delegate = self;

if ([Self.audioplayer preparetoplay] && [Self.audioplayer play]) {

NSLog (@ "started playing the recorded audio.");

} else {

NSLog (@ "Could not play the audio.");

}

} else {

NSLog (@ "Failed to create a audio player.");

}

}

-(void) Audioplayerbegininterruption: (Avaudioplayer *) player{

* * The audio session has been deactivated here * *

}


-(void) Audioplayerendinterruption: (Avaudioplayer *) player withoptions: (Nsuinteger) flags{

if (flags = = Avaudiosessioninterruptionoptionshouldresume) {

[Player play];

}

}


-(void) audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag{

if (flag) {

NSLog (@ "Audio player stopped correctly.");

} else {

NSLog (@ "Audio player did not stop correctly.");

}

if ([player IsEqual:self.audioPlayer]) {

Self.audioplayer = nil;

} else {

/* This isn't our player * *

}

}




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.