Skip to content

Commit 89550a8

Browse files
authored
fixes foreground service error when mic permission is not granted (#751)
1 parent 6b10755 commit 89550a8

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

docusaurus/docs/Flutter/05-advanced/07-background-modes.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ By default, the SDK handles button tap by canceling the call when the user taps
7979

8080
We require the following permissions to create an appropriate foreground service: `FOREGROUND_SERVICE`, `FOREGROUND_SERVICE_PHONE_CALL`, and `FOREGROUND_SERVICE_MICROPHONE`. Additionally, the `FOREGROUND_SERVICE_MEDIA_PROJECTION` permission is necessary for the screen sharing functionality. They are added out-of-the-box as part of our `stream_video_flutter` package.
8181

82+
:::note
83+
Because of the foreground service type it requires a microphone (`RECORD_AUDIO`) permission to be enabled. Make sure the user grants the permission before beginning the call, otherwise the service will not be able to start.
84+
:::
85+
8286
## iOS
8387

8488
iOS background modes provide the necessary framework to maintain connectivity, handle incoming calls, and manage ongoing calls even when the app isn't in the foreground. By using the appropriate background modes, your app can stay responsive to call events without being suspended by the system.

dogfooding/lib/screens/home_screen.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:flutter_dogfooding/widgets/stream_button.dart';
1212
// 📦 Package imports:
1313
import 'package:stream_video_flutter/stream_video_flutter.dart';
1414
import 'package:stream_video_flutter/stream_video_flutter_background.dart';
15+
import 'package:permission_handler/permission_handler.dart';
1516

1617
import '../app/user_auth_controller.dart';
1718
import '../di/injector.dart';
@@ -36,6 +37,12 @@ class _HomeScreenState extends State<HomeScreen> {
3637

3738
@override
3839
void initState() {
40+
[
41+
Permission.notification,
42+
Permission.camera,
43+
Permission.microphone,
44+
].request();
45+
3946
StreamBackgroundService.init(
4047
StreamVideo.instance,
4148
onButtonClick: (call, type, serviceType) async {
@@ -49,6 +56,7 @@ class _HomeScreenState extends State<HomeScreen> {
4956
}
5057
},
5158
);
59+
5260
super.initState();
5361
}
5462

packages/stream_video_flutter/lib/src/call_background/background_service.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:permission_handler/permission_handler.dart';
34
import 'package:stream_video/stream_video.dart';
45

56
import '../../stream_video_flutter_background.dart';
@@ -112,13 +113,22 @@ class StreamBackgroundService {
112113
OnUiLayerDestroyed? onUiLayerDestroyed,
113114
}) async {
114115
try {
116+
if (!await Permission.microphone.isGranted) {
117+
_logger.d(
118+
() =>
119+
'[onConnected] cannot start the service, microphone permission is not granted',
120+
);
121+
return;
122+
}
123+
115124
final result = await StreamVideoFlutterBackground.startService(
116125
NotificationPayload(
117126
callCid: call.callCid.value,
118127
options: optionsBuilder.call(call),
119128
),
120129
ServiceType.call,
121130
);
131+
122132
_logger.d(() => '[onConnected] call service start result: $result');
123133
if (result) {
124134
StreamVideoFlutterBackground.setOnNotificationContentClick(

packages/stream_video_flutter/pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies:
1717
flutter:
1818
sdk: flutter
1919
flutter_webrtc: ^0.11.6
20+
permission_handler: ^11.3.1
2021
plugin_platform_interface: ^2.1.7
2122
rate_limiter: ^1.0.0
2223
stream_video: ^0.5.1
@@ -34,11 +35,11 @@ dev_dependencies:
3435
flutter:
3536
assets:
3637
- images/
37-
38+
3839
fonts:
39-
- family: StreamIcons
40+
- family: StreamIcons
4041
fonts:
41-
- asset: fonts/StreamIcons.ttf
42+
- asset: fonts/StreamIcons.ttf
4243

4344
uses-material-design: true
4445

0 commit comments

Comments
 (0)