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

Proper way to turn off the microphone #79

Closed Uuttssaavv closed 11 months ago

Uuttssaavv commented 11 months ago

When I cancel the stream subscription, the mic is still on(shown by android platform). What is the proper way to turn off the microphone after we complete listening to the audio?

anarchuser commented 11 months ago

Cancelling the stream subscription should suffice. On my android 13 device the "mic currently recording" notifier definitely disappears a short time after cancelling the stream (one second, max). Please tell me

Alternatively you can also try to build my example app and see how starting/stopping the stream there affects your mic notifier.

Uuttssaavv commented 11 months ago

I am using the plugin version mic_stream: ^0.7.1 I am providing the sample

if (!state.isMicOn) {
      stream = await MicStream.microphone(
        audioSource: AudioSource.MIC,
        sampleRate: 16000,
        channelConfig: ChannelConfig.CHANNEL_IN_MONO,
        audioFormat: AudioFormat.ENCODING_PCM_16BIT,
      );
      listener = stream?.listen(print);
    } else {
      listener?.cancel();
    }
anarchuser commented 11 months ago

And you can confirm that the prints definitely start and stop, accordingly, and the mic notifier doesn't disappear even after 2-3 seconds?

Uuttssaavv commented 11 months ago

Yes, print stops but the mic notifier does not disappear.

anarchuser commented 11 months ago

Is there a reason for the await? It's hard to tell without context but I think you should be able to simply omit it

Uuttssaavv commented 11 months ago

Yes, because microphone returns the Future<Stream>

image
anarchuser commented 11 months ago

The return type was changed in 0.7.0 to just Stream<Uint8list>. The future got merged into the stream and the nullability was changed to emit an error instead. It seems you didn't update your project yet to reflect the new version, so please run flutter pub upgrade, remove the await and try again.

Uuttssaavv commented 11 months ago

I updated the version and tested it. It's working now. But still it would be better if there would be a proper way to turn off the microphone :)

anarchuser commented 11 months ago

What do you mean with a proper way? This plugin is modelled after the official sensors plugin. A microphone is just another sensor. Dart's asynchronous framework lays the foundation to fit something as complex as audio recording into something as simple as a single stream.

If you do have ideas to improve the API feel free to make suggestions in the form of Issues or Pull Requests and we may find common ground.