1. Guide Frame
1. System Framework
Coreaudio.framework
Avfoundation.framework
2. header file
#import <AVFoundation/AVFoundation.h>
3. Compliance with agreements
<AVAudioRecorderDelegate>
{
Avaudiorecorder *_recoder; Recording
}
2. Recording
Record
First parameter: The URL where the recording location is converted into a string
NSString *path = Nshomedirectory ();
Use a string of the current date as the file name
NSDate *date = [NSDate Date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
Formatter.dateformat = @ "YYYYMMDDHHMMSS";
NSString *datestr = [Formatter stringfromdate:date];
NSString *path1 = [path stringbyappendingpathcomponent:[nsstring stringwithformat:@ "/DOCUMENTS/%@.CAF", DateStr]];
NSLog (@ "path1 =%@", path1);
Path = [path stringbyappendingpathcomponent:@ "/DOCUMENTS/1.CAF"];
NSLog (@ "path =%@", path);
Nsurl *url = [Nsurl Fileurlwithpath:path];
Second parameter: Recording related settings
Nsmutabledictionary *dict = [[Nsmutabledictionary alloc]init];
1. Set the recording format: Avformatidkey
[Dict Setobject:[nsnumber NUMBERWITHINT:KAUDIOFORMATLINEARPCM] forkey:avformatidkey];
2. Set recording sample rate: Avsampleratekey 8000/44100/96000
[Dict Setobject:[nsnumber numberwithint:44100] forkey:avsampleratekey];
3. Set recording quality: Avencoderaudioqualitykey
[Dict Setobject:[nsnumber Numberwithint:avaudioqualitylow] forkey:avencoderaudioqualitykey];
4. Set the number of linear sample bits: Avlinearpcmbitdepthkey 8/16/24
[Dict Setobject:[nsnumber numberwithint:16] forkey:avlinearpcmbitdepthkey];
5. Recording Channel: Avnumberofchannelskey 1/2
[Dict Setobject:[nsnumber Numberwithint:2] forkey:avnumberofchannelskey];
Avaudiosession *session = [[Avaudiosession alloc]init];
[Session Setcategory:avaudiosessioncategoryrecord Error:nil];
[Session Setactive:yes Error:nil];
if (!_recoder) {
_recoder = [[Avaudiorecorder alloc]initwithurl:url settings:dict Error:nil];
}
_recoder.delegate = self;
if ([_recoder Preparetorecord]) {
[_recoder Record];
}
Pause
[_recoder pause];
Stop
[_recoder stop];
3. Agreement
Avaudiorecorderdelegate related
-(void) audiorecorderdidfinishrecording: (Avaudiorecorder *) Recorder successfully: (BOOL) flag{
NSLog (@ "Finish");
}
-(void) Audiorecorderencodeerrordidoccur: (Avaudiorecorder *) Recorder error: (Nserror *) error{
NSLog (@ "error:%@", error);
}
-(void) Audiorecorderbegininterruption: (Avaudiorecorder *) recorder{
NSLog (@ "System-level interrupt start");
}
-(void) Audiorecorderendinterruption: (Avaudiorecorder *) Recorder withoptions: (nsuinteger) flags{
NSLog (@ "system-level Interrupt End");
Decide if you want to resume recording/playback after the end of the interruption
if (flags = = Avaudiosessioninterruptionoptionshouldresume) {
[_recoder Record];
}
Resume recording or playback
}