flutter_sound: [BUG]: Context.startForegroundService() did not then call Service.startForeground():

Flutter Sound Version :

  • LITE

  • Important: Result of the command :

flutter pub deps | grep flutter_sound


|-- flutter_sound_lite 8.4.1
|   |-- flutter_sound_platform_interface 8.4.1
|   |-- flutter_sound_web 8.4.1
|   |   |-- flutter_sound_platform_interface...

Severity

-Crashes with the error :

/AndroidRuntime(31243): Shutting down VM
E/AndroidRuntime(31243): FATAL EXCEPTION: main
E/AndroidRuntime(31243): Process: com.ryanheise.audioserviceexample, PID: 31243
E/AndroidRuntime(31243): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{7519a88 u0 com.ryanheise.audioserviceexample/com.dooboolab.TauEngine.FlautoBackgroundAudioService}
E/AndroidRuntime(31243): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2188)
E/AndroidRuntime(31243): 	at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(31243): 	at android.os.Looper.loop(Looper.java:237)
E/AndroidRuntime(31243): 	at android.app.ActivityThread.main(ActivityThread.java:8167)
E/AndroidRuntime(31243): 	at java.lang.reflect.Method.invoke(Native Method)

  • Result is not what expected ? Dont expect that just by adding the plugin flutter sound lite will crash the same audio service example

  • Cannot build my App ? Ok - can build but we get a warning on build :

WARNING: [Processor] Library '/.gradle/caches/modules-2/files-2.1/com.github.canardoux/flutter_sound_core/8.4.1/df1da2efb4085098ab4cd8d45dc76626f0f6da3a/flutter_sound_core-8.4.1.aar' contains references to both AndroidX and old support library. This seems like the library is partially migrated. Jetifier will try to rewrite the library anyway.
 Example of androidX reference: 'androidx/arch/core/util/Function'
 Example of support library reference: 'android/support/v4/media/session/MediaSessionCompat$Callback'
  • Minor issue ? No - Major as adding Flutter_Sound_Lite increases RAM usage

Platforms you faced the error

  • Android
  • Real device ? Yes , Samsung S10+

Describe the bug When you run the sample audio service here and then interface with the front screen controls (press pause, stop button etc a few times) - app crashes and we get the stack trace shown.

To Reproduce Steps to reproduce the behavior:

  1. Fork or run the sample app of audio_service https://github.com/ryanheise/audio_service/blob/07313c1aaee4551333bac3d8a9ab6198ac495b81/audio_service/example/lib/example_multiple_handlers.dart
  1. Modify the pubspec.yaml to be :
  path_provider: ^2.0.1
  audio_session: 0.1.6+1
  just_audio: 0.9.12
  flutter_tts: 3.2.2
  rxdart: ^0.27.2
  audio_service:  0.18.0
  flutter_sound_lite: 8.4.1

i.e we have just added flutter_sound_lite 8.4.1

  1. Build and run your app for Android on Simulator or Live device.
  2. On the main screen of your devices, press the PAUSE button a few times and then STOP -> Your app crashes.

Logs!!!

We only include Flutter_sound_lite in pubspec.yaml. No class from Flutter_Sound_Lite is used or instantiated in this example.

About this issue

Most upvoted comments

@Larpoux In order to allow myself and others (@sallypeters) to continue working, it should be possible to easily create a fork of flutter_sound and remove the conflicting Android definitions for Lockscreen/notification. Can you please give some basic guidelines how to do it? I will share the fork here so others can use it until issue is resolved in the correct manner. Thanks!

@Larpoux I have just came across the same problem; Our project uses flutter_sound for recording (to a stream, not a file) and for ffmpeg operations (Audio conversions), and we wanted to introduce audio_service to support background playback (LockScreen/Notification functionality). To the best of my understanding it would be best if flutter_sound plugin can be split into smaller pieces, so the each developer can use those building blocks as he wishes. Does the latest version of flutter_sound support LockScreen/Notification functionality?

@Larpoux Thats actually excellent. Perhaps you should also include MAUI (Apparently touted as a Flutter-killer) to that list …

Hi @Larpoux

What I have done for just_audio is to make just_audio by itself only take care of playing audio, and then provided another package just_audio_background which optionally adds the background features (and internally depends on audio_service). That way the app developer can choose what to include.

just_audio_background is intended to be the easy option if the app developer doesn’t want to do much setup. It is actually just a federated plugin implementation (and for the moment, still beta). Now another way I could have done it is to make just_audio_background a normal plugin that replaces just_audio providing a new frontend that mimics the same API as just_audio but internally depends on audio_service. Anyway, there are several options to address the “ease of use” factor, and then of course I also leave open the advanced option for people to use use audio_service directly with just_audio if they want full control over the notification and media session.

  • Ok - can build but we get a warning on build :

I agree that it is not clean, but you can ignore this warning

We only include Flutter_sound_lite in pubspec.yaml. No class from Flutter_Sound_Lite is used or instantiated in this example.

Are you sure that the problem is in flutter_sound and not audio_service ?

@Alvarocda : This bug is interesting. It seems that there is an incompatibility between τ Sound and the plugin audio_service

This plugin seems interessant for us : its purpose is to be able to run an App in the background. Perhaps it is related to #255