【问题标题】:how to use the Microsoft Speech API: iOS Speech-to-Text Client Library in swift?如何快速使用 Microsoft Speech API:iOS Speech-to-Text 客户端库?
【发布时间】:2019-07-26 16:07:05
【问题描述】:

由于我是 swift 语言的新手,我知道如何使用 microsoft API 将语音转换为目标 c 中的文本,但作为客户端请求的一部分,我需要 swift 语言。谁能帮助我如何用 swift 语言做到这一点。我还添加了我在目标 c 中使用的示例代码

-(void)onFinalResponseReceived:(RecognitionResult*)response {
    bool isFinalDicationMessage = self.mode == SpeechRecognitionMode_LongDictation &&
    (response.RecognitionStatus == RecognitionStatus_EndOfDictation ||
     response.RecognitionStatus == RecognitionStatus_DictationEndSilenceTimeout);
    if (nil != micClient && self.useMicrophone && ((self.mode == SpeechRecognitionMode_ShortPhrase) || isFinalDicationMessage)) {
        // we got the final result, so it we can end the mic reco.  No need to do this
        // for dataReco, since we already called endAudio on it as soon as we were done
        // sending all the data.
        [micClient endMicAndRecognition];
    }

    if ((self.mode == SpeechRecognitionMode_ShortPhrase) || isFinalDicationMessage) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[self startButton] setEnabled:YES];
        });
    }

    if (!isFinalDicationMessage) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self WriteLine:(@"********* Final n-BEST Results *********")];
            for (int i = 0; i < [response.RecognizedPhrase count]; i++) {
                RecognizedPhrase* phrase = response.RecognizedPhrase[i];
                [self WriteLine:[[NSString alloc] initWithFormat:(@"[%d] Confidence=%@ Text=\"%@\""),
                                 i,
                                 ConvertSpeechRecoConfidenceEnumToString(phrase.Confidence),
                                 phrase.DisplayText]];
            }

            [self WriteLine:(@"")];
        });
    }
}

//convert speech
OSStatus status = [micClient startMicAndRecognition];
        if (status) {
            [self WriteLine:[[NSString alloc] initWithFormat:(@"Error starting audio. %@"), 
ConvertSpeechErrorToString(status)]];
        }

NSString* ConvertSpeechErrorToString(int errorCode) {
    switch ((SpeechClientStatus)errorCode) {
        case SpeechClientStatus_SecurityFailed:         return @"SpeechClientStatus_SecurityFailed";
}

【问题讨论】:

    标签: ios swift speech-recognition


    【解决方案1】:

    试试这个:

    func onFinalResponseReceived(_ response: RecognitionResult?) {
        let isFinalDicationMessage: Bool = mode == SpeechRecognitionMode_LongDictation && (response?.recognitionStatus == RecognitionStatus_EndOfDictation || response?.recognitionStatus == RecognitionStatus_DictationEndSilenceTimeout)
        if nil != micClient && useMicrophone && ((mode == SpeechRecognitionMode_ShortPhrase) || isFinalDicationMessage) {
            // we got the final result, so it we can end the mic reco.  No need to do this
            // for dataReco, since we already called endAudio on it as soon as we were done
            // sending all the data.
            micClient.endMicAndRecognition()
        }
        if (mode == SpeechRecognitionMode_ShortPhrase) || isFinalDicationMessage {
            DispatchQueue.main.async(execute: {() -> Void in
                self.startButton().enabled = true
            })
        }
        if !isFinalDicationMessage {
            DispatchQueue.main.async(execute: {() -> Void in
                self.writeLine(("********* Final n-BEST Results *********"))
                var i = 0
                while i < response.recognizedPhrase.count() {
                    var phrase: RecognizedPhrase? = response.recognizedPhrase[i]
                    if let aText = phrase?.displayText {
                        self.writeLine(("[\(i)] Confidence=\(ConvertSpeechRecoConfidenceEnumToString(phrase?.confidence)) Text=\"\(aText)\""))
                    }
                    i
                }
                i += 1
                self.writeLine((""))
            })
        }
    }
    
    // edit:
    var status: OSStatus = micClient.startMicAndRecognition()
    func (int errorCode) -> String? {
        switch errorCode as? SpeechClientStatus {
        case SpeechClientStatus_SecurityFailed:
            return "SpeechClientStatus_SecurityFailed"
        }
    }
    

    【讨论】:

    • 感谢您的宝贵回复,我还需要一个转换功能。我在上面编辑了我的问题
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多