After a certain time, I want to turn off audio_service in the background with the android_alarm_manager plugin
341
You have the right idea, although you have to wait until it is actually connected using "await":
await AudioService.connect();
AudioService.stop();
For this to work, you will also need to upgrade to release 0.9.0 or later which mentions in the CHANGELOG:
- Allow connections from background contexts (e.g. android_alarm_manager).
Author by
TurkC
Updated on December 19, 2022Comments
-
TurkC over 1 year
After a certain time, I want to turn off audio_service in the background with the android_alarm_manager plugin. How can I solve this problem?
When I try to do this, I get the following error.
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method stop on channel ryanheise.com/audioService)
My code is as follows.
action.dart
FlatButton( onPressed: () async { await AndroidAlarmManager.oneShot(Duration(minutes: _minute.floor().toInt()), 0, backgroundCallback); Navigator.pop(context); }, child: Text(S.of(context).set), )
main.dart
void backgroundCallback() async { AudioService.connect(); AudioService.stop(); } void main() async { WidgetsFlutterBinding.ensureInitialized(); init(); await sl<DotEnv>().load('.env'); //get theme bool _isDarkTheme = await sl<SharedPreferenceHelper>().isDarkTheme() ?? false; if (Platform.isAndroid) { AndroidAlarmManager.initialize(); } runApp(MainApp()); }
Flutter and Dart version:
[√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18362.720], locale tr-TR) • Flutter version 1.12.13+hotfix.9 at D:\flutter • Framework revision f139b11009 (11 days ago), 2020-03-30 13:57:30 -0700 • Engine revision af51afceb8 • Dart version 2.7.2