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
101 stars 70 forks source link

Keep microphone alive in background when other audio app open #47

Open yelkamel opened 1 year ago

yelkamel commented 1 year ago

Hello,

I really like this package and his simplicity.

In my app, I wanna to be able to keep listenning in background even if another app like Youtube is open.

I see that some app who use microphone (like SleepCycle) is keeping the microphone alive in background even if the user open Youtube app.

So it's shoud be possible to do that in Flutter.

Regards,

anarchuser commented 1 year ago

This plugin does not interfere in the life cycle management of apps. As long as the mobile operating system does not suspend an app recording audio, the app should keep on recording.

Note that the example application does pause recording because it has been designed to do so, by explicitly managing the life cycle of apps: example/lib/main.dart#L261-L285

Also note that other apps may potentially interfere with the recording process; this is outside of control for this plugin.

anarchuser commented 1 year ago

I recommend you to try it out by cloning this repository, removing every line from function

void didChangeAppLifecycleState(AppLifecycleState state) {}

and then check if the app keeps on recording or not by looking at the log.

If you do encounter issues, please reopen this issue with concrete information about what happened.

yelkamel commented 1 year ago

Hello @anarchuser

Thanks for you answer. I did what you say, and still the same thing, Youtube app terminate the microphone listenner.

According to the doc: https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html

I think it's because I have an interruption because: "Interruptions happen when a competing audio session from an app is activated and that session is not categorized by the system to mix with yours."

So I want to level up the audio session priority. But when I change the AudioSource with audioSource: AudioSource.VOICE_CALL,

I have a crash:

mic_stream/SwiftMicStreamPlugin.swift:76: Fatal error: Unexpectedly found nil while unwrapping an Optional value
* thread #1, queue = 'com.apple.main-thread', stop reason = Fatal error: Unexpectedly found nil while unwrapping an Optional value
    frame #0: 0x0000000189be83d4 libswiftCore.dylib`_swift_runtime_on_report

I think it's because we don't have this value in the enum even if there is in dart side. enum AudioSource : Int { case DEFAULT } in SwiftMicStreamPlugin.swift

Edit: I just saw that in the native code


                    events(FlutterError(code: "-3",
                                        message: "Currently only default AUDIO_SOURCE (id: 0) is supported", details:nil))
                    return nil
                }```
When this will change ? :) 

Regards,
anarchuser commented 1 year ago

Thank you for your detailed report. The problem, however, is that I cannot reliably fix this issue. I only wrote the android part of the plugin; the iOS and macOS parts I integrated from pull requests.

anarchuser commented 1 year ago

I will have a look at the issue but it is unlikely that I am able to fix this.

If you want, please have a look at the issue and try to fix it yourself. I am happy to provide my support and, if you do manage to fix it, integrate it into the plugin.