diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c49b81..dba0497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.2.1] - Add possibility to cancel audio stream subscription. +* Add possibility to cancel audio stream subscription. + ## [1.2.0] - Add SpeechContext support. * Add SpeechContext support. diff --git a/lib/speech_to_text.dart b/lib/speech_to_text.dart index c42e7d8..ce004a1 100644 --- a/lib/speech_to_text.dart +++ b/lib/speech_to_text.dart @@ -29,6 +29,10 @@ class SpeechToText { factory SpeechToText.viaServiceAccount(ServiceAccount account) => SpeechToText._(account.callOptions); + /// Listen to audio stream. + /// Cancelled as soon as dispose is called. + StreamSubscription> _audioStreamSubscription; + /// Sends a [RecognizeRequest] request to the Google Speech Api. /// Requires a [RecognitionConfig] and an [RecognitionAudio]. /// @@ -63,13 +67,19 @@ class SpeechToText { request .add(StreamingRecognizeRequest()..streamingConfig = config.toConfig()); - audioStream.listen((audio) { + _audioStreamSubscription = audioStream.listen((audio) { // Add audio content when stream changes. request.add(StreamingRecognizeRequest()..audioContent = audio); - }).onDone(() { + }); + + _audioStreamSubscription.onDone(() { // Close the request stream, if the audio stream is finished. request.close(); }); return client.streamingRecognize(request.stream); } + + void dispose() { + _audioStreamSubscription?.cancel(); + } } diff --git a/lib/speech_to_text_beta.dart b/lib/speech_to_text_beta.dart index 802412c..5844434 100644 --- a/lib/speech_to_text_beta.dart +++ b/lib/speech_to_text_beta.dart @@ -30,6 +30,10 @@ class SpeechToTextBeta { factory SpeechToTextBeta.viaServiceAccount(ServiceAccount account) => SpeechToTextBeta._(account.callOptions); + /// Listen to audio stream. + /// Cancelled as soon as dispose is called. + StreamSubscription> _audioStreamSubscription; + /// Sends a [RecognizeRequest] request to the Google Speech Api. /// Requires a [RecognitionConfig] and an [RecognitionAudio]. /// @@ -64,13 +68,19 @@ class SpeechToTextBeta { request .add(StreamingRecognizeRequest()..streamingConfig = config.toConfig()); - audioStream.listen((audio) { + _audioStreamSubscription = audioStream.listen((audio) { // Add audio content when stream changes. request.add(StreamingRecognizeRequest()..audioContent = audio); - }).onDone(() { + }); + + _audioStreamSubscription.onDone(() { // Close the request stream, if the audio stream is finished. request.close(); }); return client.streamingRecognize(request.stream); } + + void dispose() { + _audioStreamSubscription?.cancel(); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 6e4c24a..6b289ac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: google_speech description: Flutter Plugin for the Google Cloud GRPC Speech-to-Text Api. -version: 1.2.0 +version: 1.2.1 homepage: https://github.com/felixjunghans/google_speech environment: