Skip to content

Commit da2f881

Browse files
Brazolrenefloor
andauthored
feat(llc, ui): added method with callbacks to handling audio interruptions (iOS, Android) (#984)
* handling audio interruptions * fix * Update dogfooding/lib/screens/home_screen.dart Co-authored-by: Rene Floor <[email protected]> * webrtc version bump * Update dogfooding/lib/app/app_content.dart Co-authored-by: Rene Floor <[email protected]> * fix * fix * changelogs --------- Co-authored-by: Rene Floor <[email protected]>
1 parent 426e1c7 commit da2f881

File tree

12 files changed

+81
-7
lines changed

12 files changed

+81
-7
lines changed

dogfooding/android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
1212
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
1313
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
14+
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
15+
<uses-permission android:name="android.permission.READ_CALL_STATE" />
1416

1517
<application
1618
android:label="Stream Video Calls (Flutter)"

dogfooding/lib/app/app_content.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class _StreamDogFoodingAppContentState
8686
late final _router = initRouter(_userAuthController);
8787

8888
final _compositeSubscription = CompositeSubscription();
89+
bool? _microphoneEnabledBeforeInterruption;
8990

9091
@override
9192
void initState() {
@@ -105,6 +106,7 @@ class _StreamDogFoodingAppContentState
105106
});
106107

107108
_tryConsumingIncomingCallFromTerminatedState();
109+
_handleMobileAudioInterruptions();
108110
}
109111

110112
void initPushNotificationManagerIfAvailable() {
@@ -120,6 +122,26 @@ class _StreamDogFoodingAppContentState
120122
_observeFcmMessages();
121123
}
122124

125+
void _handleMobileAudioInterruptions() {
126+
if (!CurrentPlatform.isMobile) return;
127+
128+
RtcMediaDeviceNotifier.instance.handleCallInterruptionCallbacks(
129+
onInterruptionStart: () {
130+
final call = StreamVideo.instance.activeCall;
131+
_microphoneEnabledBeforeInterruption =
132+
call?.state.value.localParticipant?.isAudioEnabled;
133+
134+
call?.setMicrophoneEnabled(enabled: false);
135+
},
136+
onInterruptionEnd: () {
137+
if (_microphoneEnabledBeforeInterruption == true) {
138+
StreamVideo.instance.activeCall?.setMicrophoneEnabled(enabled: true);
139+
}
140+
_microphoneEnabledBeforeInterruption = null;
141+
},
142+
);
143+
}
144+
123145
void _tryConsumingIncomingCallFromTerminatedState() {
124146
if (!CurrentPlatform.isAndroid) return;
125147

dogfooding/lib/screens/home_screen.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class _HomeScreenState extends State<HomeScreen> {
4747
Permission.notification,
4848
Permission.camera,
4949
Permission.microphone,
50+
if (CurrentPlatform.isAndroid) Permission.phone,
5051
].request();
5152

5253
StreamVideoPushNotificationManager.ensureFullScreenIntentPermission();

melos.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ command:
2222
device_info_plus: ">=10.1.2 <12.0.0"
2323
share_plus: ^11.0.0
2424
stream_chat_flutter: ^9.8.0
25-
stream_webrtc_flutter: ^1.0.6
25+
stream_webrtc_flutter: ^1.0.7
2626

2727
scripts:
2828
postclean:

packages/stream_video/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Unreleased
2+
* Added `handleCallInterruptionCallbacks` method to `RtcMediaDeviceNotifier` that provides an option to handle system audio interruption like incoming calls, or other media playing
3+
14
## 0.9.5
25

36
✅ Added

packages/stream_video/lib/src/webrtc/rtc_media_device/rtc_media_device_notifier.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import '../../utils/extensions.dart';
99
import '../../utils/result.dart';
1010
import 'rtc_media_device.dart';
1111

12+
abstract class InterruptionEvent {}
13+
14+
class InterruptionBeginEvent extends InterruptionEvent {}
15+
16+
class InterruptionEndEvent extends InterruptionEvent {}
17+
1218
class RtcMediaDeviceNotifier {
1319
RtcMediaDeviceNotifier._internal() {
1420
// Debounce call the onDeviceChange callback.
@@ -22,6 +28,43 @@ class RtcMediaDeviceNotifier {
2228
Stream<List<RtcMediaDevice>> get onDeviceChange => _devicesController.stream;
2329
final _devicesController = BehaviorSubject<List<RtcMediaDevice>>();
2430

31+
/// Allows to handle call interruption callbacks.
32+
/// [onInterruptionStart] is called when the call interruption begins.
33+
/// [onInterruptionEnd] is called when the call interruption ends.
34+
/// [androidInterruptionSource] specifies the source of the interruption on Android.
35+
///
36+
/// On iOS, interruptions can occur due to:
37+
/// - Incoming phone calls
38+
/// - Siri activation
39+
/// - Alarm or timer sounds
40+
/// - Audio from other apps taking over (e.g., voice memo, navigation)
41+
///
42+
/// On Android, interruption sources depend on the [androidInterruptionSource]:
43+
/// - With audioFocus:
44+
/// - Other media apps interrupting (e.g., Spotify)
45+
/// - Assistant voice prompts (e.g., Google Assistant)
46+
/// - Alarms and notifications
47+
/// - With telephony:
48+
/// - Phone calls (requires READ_PHONE_STATE permission)
49+
///
50+
/// This method allows you to pause the call, mute the audio, or perform any other
51+
/// necessary actions when interruptions occur.
52+
///
53+
/// For more details on handling call interruptions, refer to the
54+
/// [Stream Video documentation](https://getstream.io/video/docs/flutter/advanced/handling-system-audio-interruptions/).
55+
Future<void> handleCallInterruptionCallbacks({
56+
void Function()? onInterruptionStart,
57+
void Function()? onInterruptionEnd,
58+
rtc.AndroidInterruptionSource androidInterruptionSource =
59+
rtc.AndroidInterruptionSource.audioFocusAndTelephony,
60+
}) {
61+
return rtc.handleCallInterruptionCallbacks(
62+
onInterruptionStart,
63+
onInterruptionEnd,
64+
androidInterruptionSource: androidInterruptionSource,
65+
);
66+
}
67+
2568
Future<void> _onDeviceChange(_) async {
2669
final devicesResult = await enumerateDevices();
2770
final devices = devicesResult.getDataOrNull();

packages/stream_video/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies:
3131
rxdart: ^0.28.0
3232
sdp_transform: ^0.3.2
3333
state_notifier: ^1.0.0
34-
stream_webrtc_flutter: ^1.0.6
34+
stream_webrtc_flutter: ^1.0.7
3535
synchronized: ^3.1.0
3636
system_info2: ^4.0.0
3737
tart: ^0.5.1

packages/stream_video_flutter/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Unreleased
2+
* Added `handleCallInterruptionCallbacks` method to `RtcMediaDeviceNotifier` that provides an option to handle system audio interruption like incoming calls, or other media playing
3+
14
## 0.9.5
25

36
✅ Added

packages/stream_video_flutter/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies:
3030
stream_video: ^0.9.5
3131
stream_video_flutter: ^0.9.5
3232
stream_video_push_notification: ^0.9.5
33-
stream_webrtc_flutter: ^1.0.6
33+
stream_webrtc_flutter: ^1.0.7
3434

3535
dependency_overrides:
3636
stream_video:

packages/stream_video_flutter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ dependencies:
2323
plugin_platform_interface: ^2.1.8
2424
rate_limiter: ^1.0.0
2525
stream_video: ^0.9.5
26-
stream_webrtc_flutter: ^1.0.6
26+
stream_webrtc_flutter: ^1.0.7
2727
visibility_detector: ^0.4.0+2
2828

2929
dev_dependencies:
30-
alchemist: '>=0.11.0 <0.13.0'
30+
alchemist: ">=0.11.0 <0.13.0"
3131
flutter_test:
3232
sdk: flutter
3333
mocktail: ^1.0.0

0 commit comments

Comments
 (0)