IOS中录音后再播放声音太小问题解决

- (BOOL)canRecord

{

    __block BOOL bCanRecord = YES;

    if ([[[UIDevice currentDevicesystemVersioncompare:@"7.0"] != NSOrderedAscending)

    {

        AVAudioSession *audioSession = [AVAudioSessionsharedInstance];

        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {

            [audioSessionperformSelector:@selector(requestRecordPermission:)withObject:^(BOOL granted) {

                if (granted) {

                    bCanRecord = YES;

                } else {

                    bCanRecord = NO;

                }

            }];

        }

    }

    

    return bCanRecord;

}



-(void)initRecordSession

{

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

    [session setActive:YES error:nil];

}



-(void)onRecordSoundStop:(UIButton *)sender

{

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlaybackerror:nil];  //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小

    [session setActive:YES error:nil];

    

    [_timerRec invalidate];

    if (_recorder.currentTime > 1)

    {

        [_recorder stop];

        

        PlayNodeData *model = _dataOfVideoArrary[sender.tag];

        model.hasSound  = YES;

        [_btnPlay setImage:[UIImageimageNamed:@"simulate_image_play1"]forState:UIControlStateNormal];

    }

   

}



//

-(void)onRecordSoundStart:(UIButton *)sender

{

    if (![self canRecord])

    {

        [[[UIAlertView allocinitWithTitle:nil

                                    message:[NSStringstringWithFormat:@"应用需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"]

                                   delegate:nil

                          cancelButtonTitle:@""

                          otherButtonTitles:nilshow];

        return;

    }

    

    

    [self initRecordSession];

    

    NSDictionary *settings = [[NSDictionary allocinitWithObjectsAndKeys:

                                                    [NSNumbernumberWithFloat:44100.0],AVSampleRateKey ,    //采样率8000/44100/96000

                                                    [NSNumbernumberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,  //录音格式

                                                    [NSNumbernumberWithInt:16],AVLinearPCMBitDepthKey,   //线性采样位数  8162432

                                                    [NSNumbernumberWithInt:2],AVNumberOfChannelsKey,      //声道 12

                                                    [NSNumbernumberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey//录音质量

                              

                                                    nil];

   

    NSURL *strURL = [NSURL fileURLWithPath:[selfGetRecordSoundFileName:sender.tag]];

    _recorder = [[AVAudioRecorder allocinitWithURL:strURLsettings:settings error:nil];

    _recorder.meteringEnabled = YES;

    _recorder.delegate = self;

    [_recorder prepareToRecord];

    [_recorder record];

    

    

    _timerRec = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nilrepeats:YES];

    

    

}



-(void)detectionVoice

{

    return;

    [_recorder updateMeters];//刷新音量数据

    //获取音量的平均值  [recorder averagePowerForChannel:0];

    //音量的最大值  [recorder peakPowerForChannel:0];

    

    double lowPassResults = pow(10, (0.05 * [_recorderpeakPowerForChannel:0]));

    NSLog(@"%lf",lowPassResults);

    //最大50  0

    //图片 -》大

    if (0<lowPassResults<=0.06) {

        ;

    }else if (0.06<lowPassResults<=0.13) {

        ;

    }else if (0.13<lowPassResults<=0.20) {

        ;

    }else if (0.20<lowPassResults<=0.27) {

        ;

    }

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值