Search code examples
iosspeech-recognitionspeech-to-textsfspeechrecognizer

SFSpeechRecognizer not working on real device if playing audio from Apple Music


have implemented speech to text functionality which is working fine if I am speaking to the microphone. But I want it to work if am selecting the audio from Apple Music. I have am playing the audio using MPMediaPickerController and the audio is playing perfectly. The issue is it is not converting it into text. Here is my code : ''' func startRecording() {

        // Clear all previous session data and cancel task
        if recognitionTask != nil {
            recognitionTask?.cancel()
            recognitionTask = nil
        }
        
        // Create instance of audio session to record voice
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSession.Category.record, mode: AVAudioSession.Mode.measurement, options: AVAudioSession.CategoryOptions.duckOthers)
            try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
        } catch {
            print("audioSession properties weren't set because of an error.")
        }
        
        self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
        
        let inputNode = audioEngine.inputNode
        
        guard let recognitionRequest = recognitionRequest else {
            fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
        }
        
        // Keep speech recognition data on device
        if #available(iOS 13, *) {
             recognitionRequest.requiresOnDeviceRecognition = true
        }
        
        recognitionRequest.shouldReportPartialResults = true
        self.recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
            
            var isFinal = false
            
            if result != nil {
                
                self.timer.invalidate()
                if self.count == 0 {
                    self.textView.text = result!.bestTranscription.formattedString
                } else {
                    self.textView.text = self.text + result!.bestTranscription.formattedString
                }
                isFinal = (result?.isFinal)!
            }
            else if result == nil || !isFinal {
                self.textView.text = "Press record button and say something, I'm listening!"
            }
            
            if isFinal {
// this is to remove 1 minute limit.
                self.count = self.count + 1
                self.text = self.textView.text
                
                self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(1), target: self, selector: #selector(self.againStartRec), userInfo: nil, repeats: false)
                
                self.audioEngine.stop()
                inputNode.removeTap(onBus: 0)
                
                self.recognitionRequest = nil
                self.recognitionTask = nil
                isFinal = false
                self.MicButton.isEnabled = true
            }
            
            if error != nil {
                URLCache.shared.removeAllCachedResponses()
                self.audioEngine.stop()
                inputNode.removeTap(onBus: 0)
                
                guard let task = self.recognitionTask else { return }
                task.cancel()
                task.finish()
            }
        })
        audioEngine.reset()
        inputNode.removeTap(onBus: 0)
        
        let recordingFormat = inputNode.outputFormat(forBus: 0)
        inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
            self.analyzer.analyze(buffer, atAudioFramePosition: when.sampleTime)
            self.recognitionRequest?.append(buffer)
        }
        
        self.audioEngine.prepare()
        
        do {
            try self.audioEngine.start()
        } catch {
            print("audioEngine couldn't start because of an error.")
        }
    } 
'''

Solution

  • I found the answer, mentioning it here if somebody needs in future. So I was using SFSpeechAudioBufferRecognitionRequest() instead of SFSpeechURLRecognitionRequest(). If you are selecting media from device, you need to take the url of the selected audio file and pass it to SFSpeechURLRecognitionRequest(url: audioURL). This worked for me.