Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
866 stars 566 forks source link

[BUG]: Audio does not record when screen locks #1070

Open akshay-kapase opened 4 weeks ago

akshay-kapase commented 4 weeks ago

Version of flutter_sound ^9.10.4

flutter doctor [√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.19045.4780], locale en-IN) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Chrome - develop for the web [√] Android Studio (version 2022.2) [√] Connected device (4 available) [√] Network resources

Platforms you faced the error (IOS or Android or both?) Android

Expected behavior Keep recording even when the screen times out.

Actual behavior Recording continues, but mic audio cuts off when Android times out . For example, if you record an hour long presentation, you'll find that the output file is one hour long, but there is only 1 minute of usable audio, and 59 minutes of total silence.

Tested environment (Emulator? Real Device?) Real Device. Redmi 11i OS : Android 13

Steps to reproduce the behavior Start recording, let Android time out while you are recording. After a few minutes, stop the recording, and listen to the output.

Flutter version Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision 761747bfc5 (2 months ago) • 2024-06-05 22:15:13 +0200 Engine • revision edd8546116 Tools • Dart 3.4.3 • DevTools 2.34.3

Larpoux commented 4 weeks ago

Thank you @akshay-kapase for your problem report. We already have several issue reported around this area, so we need to address this issue. Someone should investigate to understand what is the problem and what could be a solution. I added this issue in the column todo in 10.0 of the Kanban table to be sure that we will do something.

10.0 is not for today, nor for tomorrow. So it would be good if someone does something in 9.x. Perhaps the solution is in the new isBGService that we added recently on openRecorder(). Or perhaps this is something that can be fixed with some good parameters when opening the session...

Someone expert on Android should explain the problem and the solution.

akshay-kapase commented 4 weeks ago

Hi @Larpoux thanks for the feedback. I just tried using isBgService but did not got any success using it. May i know what does isBgService actually does ?

Larpoux commented 4 weeks ago

Perhaps Mohsin or @together87 could help you. We definitely need an Android expert to tell us what we need to do inside Flutter Sound. I am sure that this issue could be very easily fixed as soon as we understand what we must do.

akshay-kapase commented 4 weeks ago

ok, after going through a lot of documentation & trial and errors, it seems the recording should start by starting a foreground service with a notification. I have tried with flutter_sound + flutter_foreground_task which did not work because i was getting this error from flutter_sound package

Error starting recorder: MissingPluginException(No implementation found for method resetPlugin on channel xyz.canardoux.flutter_sound_recorder)

so then i tried with record plugin + flutter_foreground_task in which case the recording works even when the phone is locked.

Larpoux commented 4 weeks ago

Interesting. When you had success, it was not with Flutter Sound, was it ? I am not sure to have completely understood

akshay-kapase commented 4 weeks ago

yes with flutter sound i got this resetPlugin error. i tried to run it in release mode but still got the same error. so i have changed this small poc code from flutter_sound to record plugin

akshay-kapase commented 3 weeks ago

@Larpoux can you please guide me where you are running the background service inside the library

@Override
    public void onAttachedToEngine ( FlutterPlugin.FlutterPluginBinding binding )
    {
        this.pluginBinding = binding;

        new MethodChannel(binding.getBinaryMessenger(), "xyz.canardoux.flutter_sound_bgservice").setMethodCallHandler(new MethodCallHandler() {
            @Override
            public void onMethodCall ( final MethodCall call, final Result result )
            {
                if (call.method.equals("setBGService")) {
                    attachFlauto();
                }
                result.success(0);
            }
        });
    }
kaushik072 commented 3 weeks ago

Hi @akshay-kapase @Larpoux I am also getting the same error and I am trying to find the solution, have you got any solution for this issue?

akshay-kapase commented 3 weeks ago

use combination of record plugin + foreground service task plugin + audioplayers plugin it should work

truongdo commented 2 weeks ago

use combination of record plugin + foreground service task plugin + audioplayers plugin it should work

@akshay-kapase would you share your code, we are also struggling with this problem.