Skip to content

Commit

Permalink
[Android] fix screen sharing crash on first boot (livekit#203)
Browse files Browse the repository at this point in the history
* fix screen sharing crash on first boot.

* update.

* update.

* Update controls.dart
  • Loading branch information
cloudwebrtc authored Feb 3, 2023
1 parent 8d911b1 commit 5d727be
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions example/lib/widgets/controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,35 @@ class _ControlsWidgetState extends State<ControlsWidget> {
}
if (WebRTC.platformIsAndroid) {
// Android specific
try {
requestBackgroundPermission([bool isRetry = false]) async {
// Required for android screenshare.
const androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: 'Screen Sharing',
notificationText: 'LiveKit Example is sharing the screen.',
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon:
AndroidResource(name: 'livekit_ic_launcher', defType: 'mipmap'),
);
await FlutterBackground.initialize(androidConfig: androidConfig);
await FlutterBackground.enableBackgroundExecution();
} catch (e) {
print('could not publish video: $e');
try {
bool hasPermissions = await FlutterBackground.hasPermissions;
if (!isRetry) {
const androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: 'Screen Sharing',
notificationText: 'LiveKit Example is sharing the screen.',
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: AndroidResource(
name: 'livekit_ic_launcher', defType: 'mipmap'),
);
hasPermissions = await FlutterBackground.initialize(
androidConfig: androidConfig);
}
if (hasPermissions &&
!FlutterBackground.isBackgroundExecutionEnabled) {
await FlutterBackground.enableBackgroundExecution();
}
} catch (e) {
if (!isRetry) {
return await Future<void>.delayed(const Duration(seconds: 1),
() => requestBackgroundPermission(true));
}
print('could not publish video: $e');
}
}

await requestBackgroundPermission();
}
if (WebRTC.platformIsIOS) {
var track = await LocalVideoTrack.createScreenShareTrack(
Expand Down

0 comments on commit 5d727be

Please sign in to comment.