1 year ago
#317635
SePröbläm
How to play audio from an isolate?
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?
flutter
audio
dart-isolates
0 Answers
Your Answer