Skip to content

Fix Audio Subscription Cancellation and Stream Handling #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions evi-flutter-example/audio/lib/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Audio {
(event) {
if (event is Map) {
if (event['type'] == 'audio') {
if (_audioController.isClosed) {
return;
}
final audioData = event['data'] as String;
_audioController.add(audioData);
} else if (event['type'] == 'error') {
Expand Down
4 changes: 3 additions & 1 deletion evi-flutter-example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class _MyHomePageState extends State<MyHomePage> {
bool _isConnected = false;
bool _isMuted = false;
var chatEntries = <ChatEntry>[];
StreamSubscription<String>? _audioSubscription;

// EVI sends back transcripts of both the user's speech and the assistants speech, along
// with an analysis of the emotional content of the speech. This method takes
Expand Down Expand Up @@ -331,7 +332,7 @@ class _MyHomePageState extends State<MyHomePage> {
void _startRecording() async {
await _audio.startRecording();

_audio.audioStream.listen((data) async {
_audioSubscription = _audio.audioStream.listen((data) async {
_sendAudio(data);
});
_audio.audioStream.handleError((error) {
Expand All @@ -341,6 +342,7 @@ class _MyHomePageState extends State<MyHomePage> {

void _stopRecording() {
_audio.stopRecording();
_audioSubscription?.cancel();
}

void _unmuteInput() {
Expand Down