2

The requirement is simple: An app (for both mobile and desktop) that continues to run in background and periodically plays some audio and/or vibrates if some events happen. An isolate seemed to be the obvious choice. Problem is, as soon as the isolate tries to load an audio file, an Unhandled exceptin gets thrown:

E/flutter (15600): [ERROR:flutter/runtime/dart_isolate.cc(1111)] Unhandled exception:
E/flutter (15600): 'package:flutter/src/services/platform_channel.dart': Failed assertion: line 397 pos 7: '_binaryMessenger != null || ServicesBinding.instance != null': Cannot set the method call handler before the binary messenger has been initialized. 
This happens when you call setMethodCallHandler() before the WidgetsFlutterBinding has been initialized. 
You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel().
package:flutter/…/services/platform_channel.dart:397

Switching to another audio plugin didn't help. At the time the exception is thrown, the app is already up and running and WidgetsFlutterBinding.ensureInitialized() (as recommended by the error message) has already been called.

Now I'm wondering what causes the issue, and how would you workaround or fix it?

SePröbläm
  • 5,142
  • 6
  • 31
  • 45
  • Did you ever find a solution for this? – kargirwar Jun 10 '22 at 10:54
  • @kargirwar no, audio wasn't working in an isolate. The solution was to keep the whole app running while in background and play the audio at the app. – SePröbläm Jun 12 '22 at 00:26
  • @SePröbläm any tips on how to make the app run in the background on iOS? I have a very similar use case - basically, the requirement is that the app is able to beep a few times while in the background. Can't find any documentation on how to do this... – Alex Lomia May 10 '23 at 04:40
  • 1
    @AlexLomia have a look at the audio_service plugin. It's running well on iOS, causes some rare crashes on Android though. What you're looking for is called a foreground service. – SePröbläm May 10 '23 at 08:35

0 Answers0