0

I am working on a rather simple alarm app that uses android_alarm_manager to schedule the alarms and flutter_audio_query to gather information about the music I need to make things work. When the alarm goes off I need to have flutter_audio_query loaded to be able to access the file paths of the sound files I want to use.

The issue I am facing currently is the plugin not being loaded inside the callback, causing a MissingPluginException. This does not appear to be the issue with the PathProvider plugin for example (which I initially intended to use for the demo below).

MCVE attempt (created a new Flutter project with Kotlin Support, AndroidX under Flutter Version 1.12.13+hotfix.8; full repository here):

main.dart:

import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_audio_query/flutter_audio_query.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AndroidAlarmManager.initialize();
  runApp(MyApp());
  Scheduler().setupAlarm();

  // Runs because the FAQ plugin is loaded correctly.
  final demoSongList = await FlutterAudioQuery().getSongs();
  print('Main function: ${demoSongList.length}');
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Sample Text', textDirection: TextDirection.ltr,));
  }
}

class Scheduler {
  setupAlarm() async {
    print('Setting up alarm!');
    final success = await AndroidAlarmManager.oneShot(Duration(seconds: 3), 42, callback);
    print(success);
  }

  static callback() async {
    // Requires FAQ plugin to be loaded which it isn't.
    final songs = await FlutterAudioQuery().getSongs();
    print('Callback ${songs.length}!');
  }
}

pubspec.yaml

name: stack_overflow_demo
description: A Demo Application demonstrating the issue with android_alarm_manager and plugins

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  android_alarm_manager: ^0.4.5+3
  flutter_audio_query: ^0.3.3

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.github.geisterfurz007.stack_overflow_demo">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="stack_overflow_demo"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

        <service
            android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

If more files are required, let me know; I will add them as soon as I can!
From my understanding with Android Embedding V2 (as declared in the AndroidManifest.xml), all plugins should be loaded through reflection and creating my own Application to load the plugins should not be required.

Now for the actual question of this question: What causes the FlutterAudioQuery Plugin to not be loaded in the callback and how do I change this so I can use it when the alarm runs?

For transparency: After rather low views on this question after a day, I decided to open up a Github issue in the flutter repo here.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54

2 Answers2

0

I think the problem is that flutter_audio_query does not support the new plugin APIs yet. Check this page in the flutter documentation.

I had a quick look at the flutter_audio_query source and it doesn´t seem to implement the FlutterPlugin interface (Upgrade Step 1 in the documentation).

Josh
  • 991
  • 6
  • 14
  • Good idea! There is one thing that makes me have doubts about this however. Looking at [audioplayers](https://pub.dev/packages/audioplayers) the library also doesn't implement Android Embedding V2, playing a static file in the callback however works perfectly fine. I did however open an [issue](https://github.com/sc4v3ng3r/flutter_audio_query/issues/14#issuecomment-589835933) for flutter_audio_query, maybe this is indeed the issue! – geisterfurz007 Feb 21 '20 at 21:38
  • Hey again! The developer of flutter_audio_query did make an update and the library now supports Embedding V2; unfortunately the example still fails to run properly with the same error after updating to 0.3.4+1. – geisterfurz007 Feb 22 '20 at 17:51
0

There is a new version of audio_query_plugin available and appears the new Flutter V2 embedding API is working with.

By CHANGELOG.md

Marcos Boaventura
  • 4,641
  • 1
  • 20
  • 27