public void Dispose() { _speechRecognizer?.Dispose(); _audioEngine?.Dispose(); _recognitionRequest?.Dispose(); _recognitionTask?.Dispose(); _initialSilenceTimeout?.Dispose(); _endSilenceTimeout?.Dispose(); }
public void Start() { recognitionTask?.Cancel(); recognitionTask = null; recognizedText = ""; var audioSession = AVAudioSession.SharedInstance(); NSError err; err = audioSession.SetCategory(AVAudioSessionCategory.Record); audioSession.SetMode(AVAudioSession.ModeMeasurement, out err); err = audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); liveRequest = new SFSpeechAudioBufferRecognitionRequest { ShouldReportPartialResults = true }; var node = audioEngine.InputNode; var recordingFormat = node.GetBusOutputFormat(0); node.InstallTapOnBus(0, 1024, recordingFormat, (AVAudioPcmBuffer buffer, AVAudioTime when) => { // Append buffer to recognition request liveRequest.Append(buffer); }); recognitionTask = speechRecognizer.GetRecognitionTask(liveRequest, (SFSpeechRecognitionResult result, NSError error) => { var isFinal = false; if (result != null) { recognizedText = result.BestTranscription.FormattedString; OnSpeechRecognized(new SpeechRecognizedEvent(recognizedText)); isFinal = result.Final; } if (error != null || isFinal) { audioSession.SetCategory(AVAudioSessionCategory.Playback); audioSession.SetMode(AVAudioSession.ModeDefault, out err); node.RemoveTapOnBus(0); audioEngine.Dispose(); liveRequest.Dispose(); recognitionTask.Dispose(); liveRequest = null; recognitionTask = null; } }); audioEngine.Prepare(); audioEngine.StartAndReturnError(out err); }
public void stop() { if (!running) { return; } running = false; AVAudioSession.SharedInstance().SetActive(false); if (audioPlayer != null) { try { audioPlayer.Stop(); audioPlayer.Reset(); } catch (Exception) { } audioPlayer.Dispose(); audioPlayer = null; } if (audioDecoder != null) { audioDecoder.stop(); audioDecoder.Dispose(); audioDecoder = null; } if (audioEngine != null) { try { audioEngine.Stop(); audioEngine.Reset(); } catch (Exception) { } audioEngine.Dispose(); audioEngine = null; } }
static void StopInstances() { AudioEngine?.InputNode?.RemoveTapOnBus(0); AudioEngine?.Stop(); AudioEngine?.Dispose(); AudioEngine = null; LiveSpeechRequest?.EndAudio(); LiveSpeechRequest?.Dispose(); LiveSpeechRequest = null; SpeechRecognizer?.Dispose(); SpeechRecognizer = null; //Timer?.Dispose(); //Timer = null; }
public void stop() { if (!running) { return; } running = false; AVAudioSession.SharedInstance().SetActive(false); if (audioRecorder != null) { try { audioRecorder.InputNode.RemoveTapOnBus(0); audioRecorder.Stop(); audioRecorder.Reset(); } catch (Exception) { } audioRecorder.Dispose(); audioRecorder = null; } if (audioEncoder != null) { audioEncoder.stop(); audioEncoder.Dispose(); audioEncoder = null; } lock (outputBuffers) { outputBuffers.Clear(); } }
void DeletePlayer() { Stop(); if (player != null && player.Playing) { player.Stop(); } if (engine != null && engine.Running) { engine.Stop(); } if (player != null && engine != null && pitch != null) { engine.Dispose(); player.Dispose(); pitch.Dispose(); engine = null; player = null; pitch = null; } }