录音,

1,初始化 AVAudioSession

 //7.0第一次运行会提示,是否允许使用麦克风

        AVAudioSession *session = [AVAudioSessionsharedInstance];

        NSError *sessionError;

        //AVAudioSessionCategoryPlayAndRecord用于录音和播放

        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

        if(session == nil)

            NSLog(@"Error creating session: %@", [sessionError description]);

        else

            [session setActive:YES error:nil];

2,生成目录

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    playName = [NSString stringWithFormat:@"%@/play.aac",docDir];

3,录制

   recorder = [[AVAudioRecorderalloc] initWithURL:[NSURLURLWithString:playName] settings:recorderSettingsDicterror:&error];

        

        if (recorder) {

            recorder.meteringEnabled = YES;

            [recorder prepareToRecord];

            [recorder record];

4,播放

  player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLURLWithString:playName] error:&playerError];

    

    if (player == nil)

    {

        NSLog(@"ERror creating player: %@", [playerError description]);

    }else{

        [player play];

    }

ps:好像AVAudioSession 与AVAudioRecorder, AVAudioPlayer没有什么关系,代码之间看不到,

a,设置:

//录音设置
NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//要找的文件路径
NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//创建存储的文件夹
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/lll.aac", strUrl]];
urlPlay = url;//获得播放路径
NSLog(@"录音的路径为:%@",strUrl);
NSLog(@"录音====路径:%@",url);
NSError *error;
//初始化
recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];

//开启音量检测
recorder.meteringEnabled = YES;
recorder.delegate = self;

b,启动,时间限制,

- (IBAction)btnDown:(id)sender
{

//创建录音文件,准备录音
if ([recorder prepareToRecord]) {
//开始
[recorder record];
}

//设置定时检测
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];

}

- (IBAction)btnUp:(id)sender
{
double cTime = recorder.currentTime;
if (cTime > 2) {//如果录制时间<2 不发送

[mainRecorder recordForDuration:60.0];//设置录音时限60秒,该方法自动执行prepareToRecord


NSLog(@"发出去");
}else {
//删除记录的文件
[recorder deleteRecording];
//删除存储的
}
[recorder stop];
[timer invalidate];//移除计时器
}
- (IBAction)btnDragUp:(id)sender
{
//删除录制文件
[recorder deleteRecording];
[recorder stop];
[timer invalidate];

NSLog(@"取消发送");
}


posted @ 2014-01-13 18:37  路在脚下,  阅读(387)  评论(0编辑  收藏  举报