Canardoux / flutter_sound

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

FlutterSoundPlayer should work in background service #1061

Closed together87 closed 3 months ago

together87 commented 3 months ago

This plugin is amazing and I love this. But while I was working on a music app, I had to play audio in background. I am using flutter_background_service and FlutterSoundPlayer throws missing plugin exception. I checked native code of Android and initialized player engine at onAttachedToEngine which was at onAttachedToActivity. Now plugin exception get disappeared but player doesn't work because openPlayerCompleted callback is never called from native part. More correctly, FlautoPlayerCallback doesn't call this function and in flutter part it awaits forever. I want this problem is resolved and it would be amazing. If you give me a hint, I am ready to open a PR and fix this problem. Thanks.

Larpoux commented 3 months ago

Hi @together87 , Thank you for your problem report. We are actually working on the the openPlayerCompleted issue. When it will be fixed, I will look to your issue. Sorry for the bugs…

Larpoux commented 3 months ago

@together87 , Flutter Sound 9.9.4 is released

9.9.4

9.9.3

If you give me a hint, I am ready to open a PR and fix this problem.

This is a very nice proposition. Please, note that I am not very familiar with Android. I moved attachFlautoPlayer and attachFlautoRecorder from onAttachedToActivity to onAttachedToEngine, but I am not sure of this modification. I don't remember why, but there was certainly a reason to put this code in onAttachedToActivity ... Please help me with this ... And if this is not good, send me your logs

together87 commented 3 months ago

Hello, @Larpoux. As I said, I tried to move those attach functions to onAttachedToEngine. I found that method channel communication doesn't work from Android native to Dart. I confirmed that FlutterSoundManager::invokeMethod of android was called literally. But in flutter dart, MethodCallHandler is not called. This was the cause of error. From survey, I found one proper article about this problem. [(https://stackoverflow.com/questions/64809293/how-do-i-use-a-flutter-methodchannel-to-invoke-a-method-in-dart-code-from-the-na)] I am trying it.

together87 commented 3 months ago

Hi, @Larpoux. I checked your 9.9.4 version and it works in background service as well but not in normal widget. It implied player and record should be initialized at onAttachedToActivity when using flutter_sound in normal widget flutterengine. I patched it work both in background service and normal situation. I only changed 2 files. (FlutterSound.java in native and flutter_sound_player.dart in dart plugin). I already tested it and worked fine. I wish you please apply my patch to new version 9.9.5 so that I can use it. You can refine my code for your style. Thanks. Regards. modified_files.zip

together87 commented 3 months ago

I forgot to remind you that usage in background service. I added isBGService parameter to openPlayer of FlutterSoundPlayer. It is false by default and if you set it as true, it indicates that it is running under background service and it does necessary work to be used in bg service.

Larpoux commented 3 months ago

@together87 , Thank you so much for your contribution. Actually I am overloaded with another project and I have not much time to work on flutter sound maintenance. So, I am very grateful when someone helps. Also I don’t master Android, so your help is precious.

I am going to publish 9.9.4 with your PR tomorrow. Again, thank you for your time spent to improve flutter sound

together87 commented 3 months ago

@Larpoux, I am glad to contribute to your plugin. Actually there is a still one lack that opening player both in main widget and background doesn't work. I think this is due to only one player should be opened at one time. At the moment, this is not a problem in my current work but for better enhancement. And I have a personal question for you. Are you only an open source contributor or also a company developer? I am currently free in project and I literally want a regular ongoing work. If you have some projects, I would be appreciated if you give some. You can contact me on telegram if you want IM- tough0113. Thanks. Regards.

Larpoux commented 3 months ago

Actually, I am retired. I don’t work anymore for a company, for a boss, nor for customers. I am free. I can do what I want, when I want. Freedom is an incredible feeling.

Larpoux commented 3 months ago

Flutter Sound 9.9.5 is released:

9.9.5

together87 commented 3 months ago

Cheers.