ios 音頻錄制


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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM