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

Audio capturing is not working in the background #645

Open
ashifali3147 opened this issue Nov 26, 2024 · 1 comment
Open

Audio capturing is not working in the background #645

ashifali3147 opened this issue Nov 26, 2024 · 1 comment

Comments

@ashifali3147
Copy link

I am working on a LiveKit project and using the example app as a reference. When I go to the background, audio is not being captured. However, when I open the app again, it works fine. The audio capture does not work when the app is in the background. Can someone guide me on how to fix this?

@BIBiBI12
Copy link

BIBiBI12 commented Dec 10, 2024

Flutter pub add flutter_background


startBackgroundMode() async {
print('debug == Android Platform startBackgroundMode1 ');
if (!lkPlatformIs(PlatformType.android)) {
print('debug == Android Platform startBackgroundMode2 ');
return;
}
// Android specific
print('debug == Android Platform startBackgroundMode');

requestBackgroundPermission([bool isRetry = false]) async {
  // Required for android screenshare.
  try {
    bool hasPermissions = await FlutterBackground.hasPermissions;
    if (!isRetry) {
      const androidConfig = FlutterBackgroundAndroidConfig(
        notificationTitle: 'xxxxx',
        notificationText: 'xxxxx.',
        notificationImportance: AndroidNotificationImportance.normal,
        notificationIcon:
            AndroidResource(name: 'ic_launcher', defType: 'mipmap'),
      );
      hasPermissions =
          await FlutterBackground.initialize(androidConfig: androidConfig);
    }
    if (hasPermissions && !FlutterBackground.isBackgroundExecutionEnabled) {
      await FlutterBackground.enableBackgroundExecution();
      print('debug == Android Platform startBackgroundMode 启动成功');
    }
  } catch (e) {
    if (!isRetry) {
      return await Future<void>.delayed(const Duration(seconds: 1),
          () => requestBackgroundPermission(true));
    }
    print('could not publish video: $e');
  }
}

await requestBackgroundPermission();

}

endBackgroundMode() async {
if (!lkPlatformIs(PlatformType.android)) {
return;
}
await FlutterBackground.disableBackgroundExecution();
logger.info('Android Platform endBackgroundMode');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants