Sajjad
Sajjad

Reputation: 3218

MissingPluginException when using a plugins With Alarm Manager Call Back

I use android_alarm_manager_plus 2.0.2 plugin in my project and on Call back I use A Function And I Play a Sound With assets_audio_player_3.0.6.

But I Faced with This Error

MissingPluginException(No implementation found for method stop on channel assets_audio_player)
MissingPluginException(No implementation found for method open on channel assets_audio_player)
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method open on channel assets_audio_player)

this Error reported on this Places But I cant Find Any solutoin

MissingPluginException When Using Plugins in Flutter Alarm Manager Callbacks

Flutter-AssetsAudioPlayer/issues/523

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version 10.0.19043.1237], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[√] Chrome - develop for the web
[√] Android Studio (version 4.2)
[√] Connected device (3 available)

this code produce this Error

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void printHello() async{
  final DateTime now = DateTime.now();
  print("[$now] Hello, world! function='$printHello'");

  ///play audio
  AssetsAudioPlayer assetsAudioPlayer =
  AssetsAudioPlayer.withId("App_ID");

  var audio = Audio(
    "assets/audios/music.mp3",
  );

  assetsAudioPlayer.open(audio,
      autoStart: true,
      showNotification: true,
  );
}


main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AndroidAlarmManager.initialize();
  runApp(MyApp());
  await AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 15)), 123456789, printHello);
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

Result

[2021-10-11 13:35:27.441400] Hello, world! function='Closure: () => void from Function 'printHello': static.'
 MissingPluginException(No implementation found for method stop on channel assets_audio_player)
 MissingPluginException(No implementation found for method open on channel assets_audio_player)
 [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method open on channel assets_audio_player)

Upvotes: 0

Views: 1011

Answers (2)

Matthias Schicker
Matthias Schicker

Reputation: 653

For anyone finding this: I'm pretty sure that that's a bug in the assert_audio_player library: At this point it does not play well with multiple isolates (which happens when you try to do stuff in the background).

I've documented my findings in the plugin's repository:

https://github.com/florent37/Flutter-AssetsAudioPlayer/issues/523

Hoperfully, it will be fixed by the author.

Upvotes: 1

José David Ortega
José David Ortega

Reputation: 434

You need to move the three variables into the main method. something like this:

void printHello() async {
  final DateTime now = DateTime.now();
  print("[$now] Hello, world! function='$printHello'");
}

main() async {
  WidgetsFlutterBinding.ensureInitialized();
  AssetsAudioPlayer assetsAudioPlayer = AssetsAudioPlayer.withId('1234');
  var audio = Audio(
    "assets/audio/music.mp3",
  );
  await assetsAudioPlayer.open(
    audio,
    autoStart: true,
    showNotification: true,
  );
  await AndroidAlarmManager.initialize();
  runApp(MyApp());
  await AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 15)), 123456789, printHello);
}

class MyApp extends StatelessWidget {
...
}

Upvotes: 0

Related Questions