0

I'm using LiveKit Flutter SDK to implement screen sharing in my Flutter app. On Android 14 (SDK 34 and above), the app crashes every time I try to enable screen sharing with:

await room!.localParticipant?.setScreenShareEnabled(true);

The crash log shows:

SecurityException: Starting FGS with type mediaProjection requires android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION
at android.app.ContextImpl.startForegroundServiceCommon
at android.app.ContextImpl.startForegroundService
...

This happens every time I try to start screen sharing. It looks like the app (or flutter_background service) tries to start a Foreground Service with MediaProjection before user consent is granted.

I temporarily avoided the crash by wrapping startForeground() in a try/catch inside IsolateHolderService.kt, but that only hides the issue — it doesn’t fix it.

What I tried

  • Updated both livekit_client and flutter_background to their latest versions.

  • Added the required permission: android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION.

  • Added foregroundServiceType="mediaProjection" to services.

  • Verified flutter_background is initialized after user consent.

  • Tried delaying screen share start — still crashed every time.

Question

How do I properly request MediaProjection permission before enabling screen share in Flutter using LiveKit, so it works correctly on Android 14+ without crashing?

1 Answer 1

0

Root Cause

On Android 14+, starting a Foreground Service with mediaProjection type requires:

  1. The android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION permission, and

  2. The MediaProjection consent from the user before starting the service.

If your app (or the plugin) starts the service before the user grants permission, Android throws a SecurityException.

The flutter_webrtc package (used internally by LiveKit) now exposes a helper that properly requests the screen capture permission before screen sharing starts.

You just need to call this before enabling screen share:

import 'package:flutter_webrtc/flutter_webrtc.dart';

bool hasCapturePermission = await Helper.requestCapturePermission();
if (!hasCapturePermission) {
  // User denied or system error — don't proceed
  return;
}

// Now it’s safe to enable screen sharing
await room!.localParticipant?.setScreenShareEnabled(true);

Required AndroidManifest.xml

Make sure you have the following entries:

(Only keep the service types that apply to your app — mediaProjection is required for screen capture.)

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />

<service
    android:name=".IsolateHolderService"
    android:foregroundServiceType="mediaProjection|dataSync|location" />

Why This Works

Android 14+ enforces strict order:

  1. Request MediaProjection consent first (Helper.requestCapturePermission()).

  2. Then start the foreground service (done internally by LiveKit or flutter_background).

  3. The OS now allows MediaProjection-type FGS without throwing SecurityException.

References

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.