Gavin.han

致力于移动开发 技术改变生活

导航

ios 音频录制

Posted on 2012-10-30 22:28  gavin.han  阅读(9303)  评论(0编辑  收藏  举报

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