ios 音頻錄製

來源:互聯網
上載者:User

     建立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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.