AVFoundation之录音及播放

来源:互联网 发布:淘宝防排查秒上架 编辑:程序博客网 时间:2024/04/27 23:35

录音

在开始录音前,要把会话方式设置成AVAudioSessionCategoryPlayAndRecord

//设置为播放和录音状态,以便可以在录制完之后播放录音    AVAudioSession *session = [AVAudioSession sharedInstance];    NSError *setCategoryError = nil;    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];    NSError *activationError = nil;    [session setActive:YES error:&activationError];

创建AVAudioRecorder

给出录音存放的地址,录音的设置等

//录音存放位置-(NSURL *) getRecordAudioPath{    NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    //#define kRecordAudioFile @"myRecord.caf"    urlStr=[urlStr stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf",[[NSUUID UUID]UUIDString]]];    NSLog(@"save file path at:%@",urlStr);    NSURL *url=[NSURL fileURLWithPath:urlStr];    return url;}
- (void) startRecord{    if (recorder.isRecording) {        return;    }    recorderUrl = [self getRecordAudioPath];    NSError *err =nil;    NSMutableDictionary *dicM=[NSMutableDictionary dictionary];    //    [dicM setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];    //设置录音格式    [dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];    //设置录音采样率,8000是电话采样率,对于一般录音已经够了    [dicM setObject:@(8000) forKey:AVSampleRateKey];    //设置通道,这里采用单声道    [dicM setObject:@(1) forKey:AVNumberOfChannelsKey];    //每个采样点位数,分为8、16、24、32    [dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];    //是否使用浮点数采样    [dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];    recorder = [[AVAudioRecorder alloc]initWithURL:recorderUrl settings:dicM error:&err];    recorder.delegate = self;    ////如果要监控声波则必须设置为YES    recorder.meteringEnabled = YES;    [recorder updateMeters];    if (err) {        NSLog(@"创建录音机对象时发生错误,错误信息:%@",err.localizedDescription);        recorder = nil;        return ;    }    [recorder prepareToRecord];    [recorder record];}

录制暂停

    //暂停录音,且可以继续录音    [recorder pause];   //[recorder record];

录制结束

    //录制结束,录音文件关闭,会执行代理方法audioRecorderDidFinishRecording/* stops recording. closes the file. */    [recorder stop];

代理方法

pragma mark — AVAudioRecorderDelegate

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
NSLog(@”录音完成!”);

}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder )recorder error:(NSError )error
{
NSLog(@”录音出错!”);

}

播放录音

创建AVAudioPlayer,AVAudioPlayer播放本地音频。

    NSError *err =nil;    player = [[AVAudioPlayer alloc]initWithContentsOfURL:recorderUrl error:&err];    player.delegate = self;    //设置音乐播放次数  -1为一直循环    player.numberOfLoops = 0;    //开启仪表计数功能    player.meteringEnabled = YES;    [player updateMeters];    //    player.enableRate = YES;    //设置左右声道 left:-1,center:0.0,right =1;    player.pan = 0.0;    //设置音量,其值0.0~1.0    player.volume = 0.6;    [player prepareToPlay];    [player play];

播放代理AVAudioPlayerDelegate

#pragma mark- AVAudioPlayerDelegate-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{    NSLog(@"player 播放失败%@",error);}-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{    NSLog(@"player:%@ 播放完成%d",player,flag);    //    //根据实际情况播放完成可以将会话关闭,其他音频应用继续播放    //    [[AVAudioSession sharedInstance]setActive:NO error:nil];    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);    playBtn.selected = NO;}
1 0
原创粉丝点击