1. 程式人生 > >IOS中錄音後再播放聲音太小問題解決

IOS中錄音後再播放聲音太小問題解決

- (BOOL)canRecord

{

    __block BOOL bCanRecord = YES;

if ([[[UIDevicecurrentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)

    {

AVAudioSession *audioSession = [AVAudioSessionsharedInstance];

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

            [audioSession performSelector

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

                if (granted) {

                    bCanRecord = YES;

                } else {

                    bCanRecord = NO;

                }

            }];

        }

    }

    return bCanRecord;

}

-(void)initRecordSession

{

AVAudioSession *session = [AVAudioSessionsharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

    [session setActive:YES error:nil];

}


-(void)onRecordSoundStop:(UIButton *)sender

{

AVAudioSession *session = [AVAudioSessionsharedInstance];

    [session setCategory:

AVAudioSessionCategoryPlaybackerror:nil];  //此處需要恢復設定回放標誌,否則會導致其它播放聲音也會變小

    [session setActive:YES error:nil];

    [_timerRec invalidate];

if (_recorder.currentTime > 1)

    {

        [_recorderstop];

PlayNodeData *model = _dataOfVideoArrary[sender.tag];

        model.hasSound  = YES;

        [_btnPlay setImage:[UIImage imageNamed:@"simulate_image_play1"] forState:UIControlStateNormal];

    }

}

//

-(void)onRecordSoundStart:(UIButton *)sender

{

    if (![self canRecord])

    {

        [[[UIAlertViewalloc] initWithTitle:nil

                                    message:[NSString stringWithFormat:@"應用需要訪問您的麥克風。\n請啟用麥克風-設定/隱私/麥克風"]

                                   delegate:nil

                          cancelButtonTitle:@""

                          otherButtonTitles:nil] show];

        return;

    }

    [selfinitRecordSession];

NSDictionary *settings = [[NSDictionaryalloc] initWithObjectsAndKeys:

                                                    [NSNumber numberWithFloat:44100.0],AVSampleRateKey ,    //取樣率 8000/44100/96000

                                                    [NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey//錄音格式

                                                    [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,   //線性取樣位數  8162432

                                                    [NSNumber numberWithInt:2],AVNumberOfChannelsKey,      //聲道 12

                                                    [NSNumbernumberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey, //錄音質量

                                                    nil];

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

_recorder = [[AVAudioRecorderalloc] initWithURL:strURL settings:settings error:nil];

_recorder.meteringEnabled = YES;

_recorder.delegate = self;

    [_recorder prepareToRecord];

    [_recorder record];

_timerRec = [NSTimerscheduledTimerWithTimeInterval:0 target:selfselector:@selector(detectionVoice) userInfo:nilrepeats:YES];

}


-(void)detectionVoice

{

return;

    [_recorder updateMeters];//重新整理音量資料

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

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

    double lowPassResults = pow(10, (0.05 * [_recorder peakPowerForChannel: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) {

        ;

    }

}