Skip to content

Commit

Permalink
Add possibility to cancel audio stream subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
felixjunghans committed Sep 27, 2020
1 parent 75ed483 commit 2ae5087
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
14 changes: 12 additions & 2 deletions lib/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<int>> _audioStreamSubscription;

/// Sends a [RecognizeRequest] request to the Google Speech Api.
/// Requires a [RecognitionConfig] and an [RecognitionAudio].
///
Expand Down Expand Up @@ -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();
}
}
14 changes: 12 additions & 2 deletions lib/speech_to_text_beta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<int>> _audioStreamSubscription;

/// Sends a [RecognizeRequest] request to the Google Speech Api.
/// Requires a [RecognitionConfig] and an [RecognitionAudio].
///
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 2ae5087

Please sign in to comment.