美文网首页
required condition is false: _re

required condition is false: _re

作者: Shirley__XY | 来源:发表于2017-07-05 16:22 被阅读0次

    使用苹果语音识别,主要代码如下

    // !!!:录音
    
    - (void)startRecordVoice
    {
      [selfstartRecording];
      [_recordViewstartRecordVoice];
    }
    
    - (void)endRecordVoice
    {
      [self.audioEnginestop];
      if(_recognitionRequest) {
        [_recognitionRequestendAudio];
      }
      [_recordViewendRecordVoice];
    
    }
    
    - (void)startRecording{
       if(_recognitionTask) {
      [_recognitionTaskcancel];
      _recognitionTask=nil;
    }
    
    AVAudioSession*audioSession = [AVAudioSessionsharedInstance];
    NSError*error;
    [audioSessionsetCategory:AVAudioSessionCategoryRecorderror:&error];
    NSParameterAssert(!error);
    [audioSessionsetMode:AVAudioSessionModeMeasurementerror:&error];
    NSParameterAssert(!error);
    [audioSessionsetActive:YESwithOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivationerror:&error];
    NSParameterAssert(!error);
    _recognitionRequest= [[SFSpeechAudioBufferRecognitionRequestalloc]init];
    AVAudioInputNode*inputNode =self.audioEngine.inputNode;
    //这个属性是是否实时返回结果,默认为yes
    _recognitionRequest.shouldReportPartialResults=NO;
    __weaktypeof(self) weakSelf =self;
    _recognitionTask= [self.speechRecognizerrecognitionTaskWithRequest:_recognitionRequestresultHandler:^(SFSpeechRecognitionResult*_Nullableresult,NSError*_Nullableerror) {
    __strongtypeof(weakSelf) strongSelf = weakSelf;
    OOLisFinal =NO;
    if(result) {
    NSLog(@"formattedString++++++++++++%@", result.bestTranscription.formattedString);
    //strongSelf.resultStringLable.text = result.bestTranscription.formattedString;
    isFinal = result.isFinal;
    [selfgetDataWithMessage:result.bestTranscription.formattedStringmessageOwner:MessageOwnerCustomer];
    }
    
    if(error || isFinal) {
      [self.audioEnginestop];
      [self.audioEngine.inputNode removeTapOnBus:0];
      strongSelf.recognitionTask=nil;
      strongSelf.recognitionRequest=nil;
    
    }
    
    }
    
    }];
    
    AVAudioFormat*recordingFormat = [inputNodeoutputFormatForBus:0];
    [inputNodeinstallTapOnBus:0bufferSize:1024format:recordingFormatblock:^(AVAudioPCMBuffer*_Nonnullbuffer,AVAudioTime*_Nonnullwhen) {
    __strongtypeof(weakSelf) strongSelf = weakSelf;
    if(strongSelf.recognitionRequest) {
      [strongSelf.recognitionRequestappendAudioPCMBuffer:buffer];
    }
    }];
    [self.audioEngineprepare];
    [self.audioEnginestartAndReturnError:&error];
    NSParameterAssert(!error);
    
    }
    
    

    结果在语音输入是总是报这个错误。原因是在结束录音的方法里面没有加上这行代码:

    
    - (void)endRecordVoice
    {
    [self.audioEnginestop];
    [self.audioEngine.inputNode removeTapOnBus:0]; //**c
    if(_recognitionRequest) {
    [_recognitionRequestendAudio];
    }
    [_recordViewendRecordVoice];
    }
    
    

    相关文章

      网友评论

          本文标题:required condition is false: _re

          本文链接:https://www.haomeiwen.com/subject/ljuqhxtx.html