建立Empty Applicaton,添加HomeViewController檔案。還是看代碼吧,將理論太枯燥,理論在代碼中會提到。
HomeViewController.h代碼:
#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
HomeViewController.m代碼:
#import "HomeViewController.h" @interface HomeViewController () @end @implementation HomeViewController @synthesize audioPlayer;@synthesize audioRecorder; //設定錄製的音頻檔案的位置- (NSString *)audioRecordingPath{ NSString *result = nil; NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsFolde = [folders objectAtIndex:0]; result = [documentsFolde stringByAppendingPathComponent:@"Recording.m4a"]; return (result);} //在初始化AVAudioRecord執行個體之前,需要進行基本的錄音設定- (NSDictionary *)audioRecordingSettings{ NSDictionary *result = nil; NSMutableDictionary *settings = [[[NSMutableDictionary alloc] init] autorelease];//錄音時所必要參數設定 [settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleLossless] 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);} //停止音訊錄製- (void)stopRecordingOnAudioRecorder:(AVAudioRecorder *)recorder{ [recorder stop];} //當AVAudioRecorder對象錄音終止的時候會調用audioRecorderDidFinishRecording:successfully:方法- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ //如果flag為真,代表錄音正常結束,使用AVAudioPlayer將其播放出來,否則日誌記錄失敗原因 if (flag == YES) { NSLog(@"錄音完成!"); 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(@"開始播放錄製的音頻!"); } else { NSLog(@"不能播放錄製的音頻!"); } }else { NSLog(@"音頻播放失敗!"); } } else { NSLog(@"錄音過程意外終止!"); } self.audioRecorder = nil;} - (void)viewDidLoad{ [super viewDidLoad]; NSError *error = nil; NSString *pathOfRecordingFile = [self audioRecordingPath]; NSURL *audioRecordingUrl = [NSURL fileURLWithPath: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) { //如果audioRecorder執行個體化成功,則開始錄製聲音,並且通過performSelector方法設定在錄製聲音10s以後執行stopRecordingOnAudioRecorder方法,用於停止錄音 NSLog(@"錄製聲音開始!"); [self performSelector:@selector(stopRecordingOnAudioRecorder:) withObject:self.audioRecorder afterDelay:10.0f]; } else { NSLog(@"錄製失敗!"); self.audioRecorder =nil; }} else { NSLog(@"auioRecorder執行個體建立失敗!");}} - (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: 錄製音頻時所必要參數設定
AVFormatIDKey 錄製音訊格式。
kAudioFormatLinearPCM: lpcm格式
kAudioFormatAC3: ac-3格式
kAudioFormatMPEG4AAC: aac格式
kAudioFormatMPEG4CELP: celp格式
kAudioFormatMPEG4HVXC: hvxc格式
kAudioFormatMPEG4Layer1: mp1格式
kAudioFormatMPEG4Layer2: mp2 格式
kAudioFormatMPEG4Layer3: mp3 格式
kAudioFormatTimeCode: time格式
kAudioFormatMIDIStream: midi格式
kAudioFormatAppleLossless:alac格式
AVSampleRateKey 錄製音頻時的採用視頻
AVNumberOfChannelsKey 錄製音頻時的通道數量
AVEncoderAudioQualityKey 錄製音訊品質
AVAudioQualityMin
AVAudioQualityLow
AVAudioQualityMedium
AVAudioQualityHigh
AVAudioQualityMax