Closed ronaldG19 closed 4 years ago
If I understand correctly, the supplied example project already exhibits the problem, so I will treat that as your minimal reproduction project.
I tried switching to the dev channel to match your flutter doctor output and ran the example project. I noticed that the notification appears without any controls, but I also noticed this in the console which helps diagnose the problem:
E/flutter ( 4805): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 4805): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 4805): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 4805): #0 defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:73:7)
E/flutter ( 4805): #1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:86:4)
E/flutter ( 4805): #2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 4805): #3 MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:368:5)
E/flutter ( 4805): #4 new AudioPlayer (package:audioplayer/audioplayer.dart:42:14)
E/flutter ( 4805): #5 new CustomAudioPlayer (package:audio_service_example/main.dart:175:34)
E/flutter ( 4805): #6 _backgroundAudioPlayerTask (package:audio_service_example/main.dart:162:30)
E/flutter ( 4805): <asynchronous suspension>
E/flutter ( 4805): #7 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:231:25)
E/flutter ( 4805): #8 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 4805): #9 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 4805): #10 _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 4805): #11 runZoned (dart:async/zone.dart:1500:12)
E/flutter ( 4805): #12 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:223:5)
E/flutter ( 4805): #13 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:19)
E/flutter ( 4805): #14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
Did you not also get the same error, or are you having a different issue? In the mean time, I'll see if I can fix the above error.
I already resolved that error by initializing WidgetsFlutterBinding.ensureInitialized(), resulted from WidgetBinding. Notification problem occurred later
I don't see the call to 'ensureInitialized' in your minimal reproduction project. audio_service does call this, but since the latest commit on dev, we can see that audio_service does not call this soon enough. You can work around it by calling ensureInitialized yourself as the first line in your background task entrypoint function, but I am working on a proper fix in #86.
But looking at your example code, you need to call the start
method with a top level function, not with something like () async { ... }. Documentation here:
https://pub.dev/documentation/audio_service/latest/audio_service/AudioService/start.html
The background task is specified by backgroundTask which will be run within a background isolate. This function must be a top-level or static function
@ronaldG19 I've just published 0.4.0 which addresses the ensureInitialized correctly (See #86 and note the changed API). Also let me know after trying it and also making your background task entrypoint into a top level function whether you still get any errors.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.
Describe the bug Cloned the example project, it only showed the notification without any controls and the music was not played. Integrated into my own project after 3 days of peeking there is no nay notification bar shown or music played.
Minimal reproduction project
To Reproduce Steps to reproduce the behavior:
then
after running flutter run -v
Runtime Environment (please complete the following information):
Dependencies used
Flutter SDK version