Demian
Demian

Reputation: 1

How to start the audio_service plugin in the background after restarting an Android phone in Flutter?

I have an app I'm working on, similar to Spotify, made with Flutter/Dart. It uses the plugins audio_service and just_audio to work in the background and for audio playback.

Currently, I am trying to achieve that, upon restarting the phone, on Android devices, the app automatically starts in the background using audio_service.

At the moment, I am only able to get the app to start on reboot but in the foreground. Any thoughts?

Here is some of the code:

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
            if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
            val i = Intent(context, com.ryanheise.audioservice.AudioServiceActivity::class.java)
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
            context.startActivity(i)
);

Here, if I start the audio_service activity, the app visibly starts for the user and plays music in the foreground. I thought that calling the plugin service instead of the activity would achieve the result, but it doesn't work.

This code is from the audio_service Android native code.

public class AudioServiceActivity extends FlutterActivity {
    @Override
    public FlutterEngine provideFlutterEngine(@NonNull Context context) {
        return AudioServicePlugin.getFlutterEngine(context);
    }
}
public static synchronized FlutterEngine getFlutterEngine(Context context) {
    FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get(flutterEngineId);
    if (flutterEngine == null) {
        // XXX: The constructor triggers onAttachedToEngine so this variable doesn't help us.
        // Maybe need a boolean flag to tell us we're currently loading the main flutter engine.
        flutterEngine = new FlutterEngine(context.getApplicationContext());
        String initialRoute = null;
        if (context instanceof FlutterActivity) {
            final FlutterActivity activity = (FlutterActivity)context;
            initialRoute = activity.getInitialRoute();
            if (initialRoute == null) {
                if (activity.shouldHandleDeeplinking()) {
                    Uri data = activity.getIntent().getData();
                    if (data != null) {
                        initialRoute = data.getPath();
                        if (data.getQuery() != null && !data.getQuery().isEmpty()) {
                            initialRoute += "?" + data.getQuery();
                        }
                    }
                }
            }
        }
        if (initialRoute == null) {
            initialRoute = "/";
        }

        Log.d("GASPI", "Initial route: " + initialRoute);


        flutterEngine.getNavigationChannel().setInitialRoute(initialRoute);
        flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
        FlutterEngineCache.getInstance().put(flutterEngineId, flutterEngine);
    }
    return flutterEngine;
}

I tried starting the service instead of the activity in the broadcast, but the service's onStartCommand does not start. Therefore, it fails because it needs to start within the first 5 seconds of being called.

Upvotes: 0

Views: 60

Answers (0)

Related Questions