4

I've implemented Apple's SpeechRecognizer to convert speech to text. I have multiple audio recordings so I'm creating mulitple SFSpeechRecognizer instance so that all of those are converted parallely and I've also used DispatchGroup so that I can get completion at last one's end. But I'm keep getting error kAFAssistantErrorDomain error 209.

private var dispatchGroup = DispatchGroup()
allURLs.forEach { (singleURL) in
    DispatchQueue.main.async {
        thisSelf.dispatchGroup.enter()
        let request = SFSpeechURLRecognitionRequest(url: url)
        guard let recognizer = SFSpeechRecognizer() else {
            thisSelf.dispatchGroup.leave()
            completion(.failure(thisSelf.speechReconInitError))
            return
        }
        request.shouldReportPartialResults = false
        if !recognizer.isAvailable {
            thisSelf.dispatchGroup.leave()
            return
        }

        recognizer.recognitionTask(with: request) { [weak thisSelf] (result, error) in
            guard let reconSelf = thisSelf else { return }
            if let error = error {
                completion(.failure(error))
                if let nsError = error as NSError? {
                    print("Error while transcripting audio: \(url.path), Code, Domain, Description: \(nsError.code), \(nsError.domain), \(nsError.localizedDescription)")
                } else {
                    print("Error while transcripting audio: \(url.path), Error: \(error.localizedDescription)")
                }
                reconSelf.dispatchGroup.leave()
            } else if let transcriptionResult = result, transcriptionResult.isFinal {
                transcribedText += transcriptionResult.bestTranscription.formattedString
                reconSelf.dispatchGroup.leave()
            }

        }

        thisSelf.dispatchGroup.notify(queue: .main) {
            if !transcribedText.isEmpty {
               completion(transcribedText)
            }
        }
    }
}

And If I transcribe only one audio to text at one time then I don't get any error. TIA

Rajat Mishra
  • 995
  • 7
  • 18

0 Answers0