追加
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
あなたのプロジェクトに
デリゲートAVAudioRecorderDelegate
を.hファイルに追加します
@property (nonatomic, strong) AVAudioRecorder *Audiorecorder;
.mファイル(ここでは)self.audioFileName
NSString
これはコードのスニペットです:
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
//Now that we have our settings we are going to instanciate an instance of our recorder instance.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd_MM_yyyy_HH_mm_ss"];
self.audioFileName = [NSString stringWithFormat:@"%@.caf", [formatter stringFromDate:[NSDate date]]];
NSString *path = [documentsDirectory stringByAppendingPathComponent:self.audioFileName ];
NSLog(@"%@",path);
NSURL *recordedTmpFile = [[NSURL alloc] initFileURLWithPath:path];
NSLog(@"%@",recordedTmpFile);
//Setup the recorder to use this file and record to it.
if(self.Audiorecorder != nil)
{
[self.Audiorecorder stop];
self.Audiorecorder = nil;
}
NSError * error;
self.Audiorecorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
//NSLog(@"Error : %@",error);
[self.Audiorecorder setDelegate:self];
[self.Audiorecorder prepareToRecord];
//Start the actual Recording
[self.Audiorecorder peakPowerForChannel:8];
[self.Audiorecorder updateMeters];
self.Audiorecorder.meteringEnabled = YES;
[self.Audiorecorder record];