Falfit
Falfit

Reputation: 1

Error Setting Up Audio-Service Flutter Package

I am trying to understand how to set up the flutter package audio_service. To not get all confused, I tested it on an empty new flutter project, and followed the basic guide to set up the package. However, I get this error when running the app:

W/System.err(14150): java.lang.IllegalStateException: The Activity class declared in your AndroidManifest.xml is wrong or has not provided the correct FlutterEngine. Please see the README for instructions.

Here is my main.dart:

import 'package:audante/audio_service_handler.dart';
import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';

late MyAudioHandler _audioHandler;
Future<void> main() async {
  _audioHandler = await AudioService.init(
    builder: () => MyAudioHandler(),
    config: const AudioServiceConfig(
      androidNotificationChannelId: 'com.example.audante',
      androidNotificationChannelName: 'Music playback',
    )
  );
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

Here is my MyAudioHandler() file: (it's the basic setup shown on the README)

import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';

class MyAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
  final _player = AudioPlayer();

  Future<void> play() => _player.play();
  Future<void> pause() => _player.pause();
  Future<void> stop() => _player.stop();
  Future<void> seek(Duration position) => _player.seek(position);
  Future<void> skipToQueueItem(int i) => _player.seek(Duration.zero,index:i);
}

Here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- ADD THESE TWO PERMISSIONS -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <!-- ALSO ADD THIS PERMISSION IF TARGETING SDK 34 -->
    <uses-permission android:name="com.ryanheise.audioservice.AudioServiceActivity"/>

    <application
        android:label="audante"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            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">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

            <!-- ADD THIS "SERVICE" element -->
        <service android:name="com.ryanheise.audioservice.AudioService"
            android:foregroundServiceType="mediaPlayback"
            android:exported="true">
            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>
        
        <!-- ADD THIS "RECEIVER" element -->
        <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver> 

        <!-- 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" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility?hl=en and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

And here is my MainActivity.kt if needed:

package com.example.audante

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()

I hope i did not forget any other usefull elements.

I tried using a custom MainActivity but it did not work. I have been looking at different documentations for hours but I can't seem to grasp the solution. I feel dumb because it's just the set up and no one else seems to have this problem.

Edit: I think I solved the issue. It was not a problem with my code, it's that I had launched the emulator and then tried to implement the audio_service package by modifying the AndroidManifest, and I did so by using the Hot Restart feature of flutter. However, I believe the Hot Restart rebuilds the app but not the apk build, therefore the AndroidManifest was considered untouched and it sent me an error. By just stopping the emulation and relaunching it, it fixed itself.

Upvotes: 0

Views: 170

Answers (0)

Related Questions