Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After a certain time, I want to turn off audio_service in the background with the android_alarm_manager plugin. #254

Closed
aydinfatih opened this issue Apr 10, 2020 · 8 comments
Assignees
Labels

Comments

@aydinfatih
Copy link

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.

FlatButton(
     onPressed: () async {
          await AndroidAlarmManager.oneShot(Duration(minutes: _minute.floor().toInt()), 0, backgroundCallback);
          Navigator.pop(context);
     },
     child: Text(S.of(context).set),
)
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 (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
@ryanheise ryanheise self-assigned this Apr 11, 2020
@ryanheise
Copy link
Owner

Hi @FatihAydin52

This was discussed in #145 . You can try @rohansohonee 's suggestion:

Visit Upgrading-pre-1.12-Android-projects and follow the Full-Flutter app migration steps.
Also use android_alarm_manager plugin as it has v2 support.

MissingPluginException might be fixed if you add this to your MainActivity.java:

public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
   GeneratedPluginRegistrant.registerWith(flutterEngine);
}

The step of updating your project to Flutter 1.12 is only relevant if you have a pre-1.12 project structure.

Please let me know if this works for you and I'll add the suggestion to the FAQ.

@aydinfatih
Copy link
Author

The mainActivity.java class is below. I also use Application.java file. It didn't work that way.

MainActivity.java

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }

    @Override
    public FlutterEngine provideFlutterEngine(Context context) {
        // Instantiate a FlutterEngine.
        FlutterEngine flutterEngine = new FlutterEngine(context.getApplicationContext());

        // Start executing Dart code to pre-warm the FlutterEngine.
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );

        return flutterEngine;
    }
}

Application.java


import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

@ryanheise
Copy link
Owner

I don't think in the previous times this issue was raised that anyone supplied a minimal reproduction project. Would you be able to do so? Essentially, make a fork of this project, and edit the example in a minimal way to reproduce the error.

@ryanheise ryanheise added the bug Something isn't working label Apr 19, 2020
@aydinfatih
Copy link
Author

I quickly created a project from scratch and repeated the error. You can reach the project from the link below.

https://github.com/FatihAydin52/audio-service-demo

@ryanheise
Copy link
Owner

Thanks!

@ryanheise
Copy link
Owner

Hi @FatihAydin52 I responded (I believe) to you on another issue and it appears to be the same example project. Are these two issues linked?

@ryanheise
Copy link
Owner

Closing due to inactivity.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants