【问题标题】:Combination of Voice Over and Speech Recognition in iOSiOS中语音和语音识别的结合
【发布时间】:2020-02-20 15:19:45
【问题描述】:

我想结合语音和语音识别功能。我有调查和问题,为了帮助用户选择选项,我添加了 UIAccessibilityCustomAction,所以当用户准备好选择时,他选择了这个动作,这反过来激活了语音识别,他发音选择的选项,并在 UI 更新后。

- (void)startRecognizingProcess {
     __weak typeof(self) weakSelf = self;
     self.audioEngine = [[AVAudioEngine alloc] init];

     if (self.recognitionTask) {
         [self.recognitionTask cancel];
         self.recognitionTask = nil;
     }

     NSError *error;
     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
     [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

     self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
     AVAudioInputNode *inputNode = self.audioEngine.inputNode;
     self.recognitionRequest.shouldReportPartialResults = YES;
    if (@available(iOS 13, *)) {
        if ([self.speechRecognizer supportsOnDeviceRecognition]) {
            self.recognitionRequest.requiresOnDeviceRecognition = true;
        }
    }
     self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
         __strong typeof(self) strongSelf = weakSelf;
         BOOL isFinal = NO;
         if (result) {
             NSString *selectedOption = result.bestTranscription.formattedString;
             NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
             isFinal = !result.isFinal;
             if (isFinal) {
                 [strongSelf activateSelectedOption:selectedOption];
                 [strongSelf.recognitionTask cancel];
                 [strongSelf.audioEngine stop];
                 [inputNode removeTapOnBus:0];
                 [strongSelf.recognitionRequest endAudio];
                 strongSelf.recognitionRequest = nil;
                 strongSelf.recognitionTask = nil;
             }
         }
         if (error) {
             [strongSelf.audioEngine stop];
             [inputNode removeTapOnBus:0];
             strongSelf.recognitionRequest = nil;
             strongSelf.recognitionTask = nil;
         }
     }];

     AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0];
     [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
         [self.recognitionRequest appendAudioPCMBuffer:buffer];
     }];

     [self.audioEngine prepare];
     [self.audioEngine startAndReturnError:&error];
}

一切正常,除了 Voice Over 失去了声音。 当我按下主页按钮,然后再次打开应用程序时,它再次有声音并按要求工作。 所以我有一个问题: 也许需要一些额外的步骤才能再次制作 Voice over?或者我想念什么。

提前谢谢你。

【问题讨论】:

    标签: ios objective-c accessibility speech-recognition voiceover


    【解决方案1】:

    我设法找出了问题所在。

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
    [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
    

    在实现过程中,一开始我添加了这些代码行,它们驻留在内存中,当应用程序被发送到后台时 - audioSession - 被释放。 所以删除这些行解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 2018-02-23
      • 2014-01-05
      相关资源
      最近更新 更多