anarchuser / mic_stream

Flutter plugin to get an infinite audio stream from the microphone
https://pub.dev/packages/mic_stream
GNU General Public License v3.0
100 stars 68 forks source link

[Question] can use in isolate? #89

Closed docaohuynh closed 9 months ago

docaohuynh commented 9 months ago

Hi @anarchuser tks for your package I have a question. Can this package use in isolate

I create another isolate to create micstream in there

final micStream = MicStream.microphone(audioFormat: AudioFormat.ENCODING_PCM_16BIT);

and error

E/flutter (14268): [ERROR:flutter/runtime/dart_isolate.cc(1097)] Unhandled exception:
E/flutter (14268): Unsupported operation: Background isolates do not support setMessageHandler(). Messages from the host platform always go to the root isolate.
E/flutter (14268): #0      BackgroundIsolateBinaryMessenger.setMessageHandler (package:flutter/src/services/_background_isolate_binary_messenger_io.dart:94:5)
E/flutter (14268): #1      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:639:23)
E/flutter (14268): #2      _runGuarded (dart:async/stream_controller.dart:823:24)
E/flutter (14268): #3      _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:207:7)
E/flutter (14268): #4      _ControllerStream._createSubscription (dart:async/stream_controller.dart:836:19)
E/flutter (14268): #5      _StreamImpl.listen (dart:async/stream_impl.dart:471:9)
E/flutter (14268): #6      CastStream.listen (dart:_internal/async_cast.dart:17:17)
E/flutter (14268): #7      Stream.first (dart:async/stream.dart:1573:14)
E/flutter (14268): #8      MicStream._setupMicStream (package:mic_stream/mic_stream.dart:214:18)
E/flutter (14268): #9      MicStream.microphone.<anonymous closure> (package:mic_stream/mic_stream.dart:153:14)
E/flutter (14268): #10     Stream.asyncExpand.<anonymous closure>.<anonymous closure> (dart:async/stream.dart:850:30)
E/flutter (14268): #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (14268): #12     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (14268): #13     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (14268): #14     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:784:19)
E/flutter (14268): #15     _StreamController._add (dart:async/stream_controller.dart:658:7)
E/flutter (14268): #16     new Stream.fromFuture.<anonymous closure> (dart:async/stream.dart:242:18)
E/flutter (14268): <asynchronous suspension>
E/flutter (14268): #17     new Stream.fromFuture.<anonymous closure> (dart:async/stream.dart:241:17)
E/flutter (14268): <asynchronous suspension>
anarchuser commented 9 months ago

EventChannel.receiveBroadcastStream

The issue is the communication between flutter and the host platform. As it seems, in order to allow this plugin to work in isolates, you'd need to rewrite both communication between isolate and host and implement the recording in the host. This defeats the purpose of isolates, though. Depending on how isolates are implemented you may find a much simpler solution to your problem, though

docaohuynh commented 9 months ago

Tks @anarchuser I will do some research for this