iOS 实现录音功能

#pragma mark ===== 录制音频初始化

-(void)saveAudio{


    AVAudioSession* session =  [AVAudioSession sharedInstance];

    [sessionsetCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    [sessionsetActive:YESerror:nil];

    //录音设置

    NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc]init];

    //设置录音格式

    [recordSetting  setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];

    //设置录音采样率(HZ)

    [recordSetting setValue:[NSNumber numberWithFloat:16000] forKey:AVSampleRateKey];

    //录音通道数

    [recordSetting setValue:[NSNumber  numberWithInt:2] forKey:AVNumberOfChannelsKey];

    //线性采样位数

    [recordSetting  setValue:[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];

    //录音的质量

    [recordSetting  setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

    //获取沙盒路径 作为存储录音文件的路径

    NSString * strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];

、//    创建url

    NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/voice.wav",strUrl]];

    self.urlPlay= url;

    NSError* error ;

    //初始化AVAudioRecorder

    self.recorder= [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];

    //开启音量监测

    self.recorder.meteringEnabled = YES;

    self.recorder.delegate=self;

    if(error){

        NSLog(@"创建录音对象时发生错误,错误信息:%@",error.localizedDescription);

    }



}

#pragma mark ===== 开始录制音频

-(void)startSaveAvdio{

    if([self.recorder prepareToRecord]){

        //开始

        [self.recorderrecord];

        NSLog(@"开始录音");


        self.avdioTimer=  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatePlayTimerValue)  userInfo:nil  repeats:YES];


        [self.avdioTimerfire];

    }


}

#pragma mark ===== 结束录制音频

-(void)stopSaveAvdio

{

    //    获取当前录音时长

    floatvoiceSize =self.recorder.currentTime;


    NSLog(@"录音时长 = %f",voiceSize);

//

        if(voiceSize <15){

            [self.recorderdeleteRecording];

            UIAlertView* altView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"时长小于15秒,重新录制"delegate:selfcancelButtonTitle:nilotherButtonTitles:nil];

            [altViewshow];

            [selfperformSelector:@selector(performDismiss:)withObject:altViewafterDelay:1.5];

        }elseif(voiceSize >60){

            [self.recorderdeleteRecording];

            UIAlertView * altView = [[UIAlertView alloc]initWithTitle:nil message:@"时长大于1分钟,重新录制" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

            [altViewshow];

            [selfperformSelector:@selector(performDismiss:)withObject:altViewafterDelay:1.5];


        }

        else

        {


            NSArray*dataArray=[selfgetAllFile];


            NSLog(@"======dataArray=======%@",dataArray);


            if(dataArray.count>0) {


                if(self.addBlackAvdioBlock) {

                    self.addBlackAvdioBlock(dataArray);

                }


                [self.navigationController popViewControllerAnimated:YES];


            }


        }


    //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [sessionsetCategory:AVAudioSessionCategoryPlayback error:nil];



    [self.recorderstop];


    [self.avdioTimer invalidate];

    self.avdioTimer = nil ;


    NSLog(@"结束录制音频");


}

你可能感兴趣的:(iOS 实现录音功能)