From 2a158a63cca31ac9ce9f31856998a1536d6ab225 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 16 Aug 2023 10:23:51 +0800 Subject: [PATCH] fix: set SpeakerPhone on but prefer Bluetooth. (#333) * fix: set SpeakerPhone on but prefer Bluetooth. * update. * update. * update. * update deps. * Update pubspec.yaml --- example/lib/widgets/controls.dart | 83 ++++++++++++++++------------- lib/src/hardware/hardware.dart | 13 +++-- lib/src/track/audio_management.dart | 2 +- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/example/lib/widgets/controls.dart b/example/lib/widgets/controls.dart index d59b93e5..e7a1b190 100644 --- a/example/lib/widgets/controls.dart +++ b/example/lib/widgets/controls.dart @@ -280,51 +280,58 @@ class _ControlsWidgetState extends State { tooltip: 'Unpublish all', ), if (participant.isMicrophoneEnabled()) - PopupMenuButton( - icon: const Icon(Icons.settings_voice), - itemBuilder: (BuildContext context) { - return [ - PopupMenuItem( - value: null, - onTap: isMuted ? _enableAudio : _disableAudio, - child: const ListTile( - leading: Icon( - EvaIcons.micOff, - color: Colors.white, + if (lkPlatformIs(PlatformType.android)) + IconButton( + onPressed: _disableAudio, + icon: const Icon(EvaIcons.mic), + tooltip: 'mute audio', + ) + else + PopupMenuButton( + icon: const Icon(Icons.settings_voice), + itemBuilder: (BuildContext context) { + return [ + PopupMenuItem( + value: null, + onTap: isMuted ? _enableAudio : _disableAudio, + child: const ListTile( + leading: Icon( + EvaIcons.micOff, + color: Colors.white, + ), + title: Text('Mute Microphone'), ), - title: Text('Mute Microphone'), ), - ), - if (_audioInputs != null) - ..._audioInputs!.map((device) { - return PopupMenuItem( - value: device, - child: ListTile( - leading: (device.deviceId == - widget.room.selectedAudioInputDeviceId) - ? const Icon( - EvaIcons.checkmarkSquare, - color: Colors.white, - ) - : const Icon( - EvaIcons.square, - color: Colors.white, - ), - title: Text(device.label), - ), - onTap: () => _selectAudioInput(device), - ); - }).toList() - ]; - }, - ) + if (_audioInputs != null) + ..._audioInputs!.map((device) { + return PopupMenuItem( + value: device, + child: ListTile( + leading: (device.deviceId == + widget.room.selectedAudioInputDeviceId) + ? const Icon( + EvaIcons.checkmarkSquare, + color: Colors.white, + ) + : const Icon( + EvaIcons.square, + color: Colors.white, + ), + title: Text(device.label), + ), + onTap: () => _selectAudioInput(device), + ); + }).toList() + ]; + }, + ) else IconButton( onPressed: _enableAudio, icon: const Icon(EvaIcons.micOff), tooltip: 'un-mute audio', ), - if (!lkPlatformIsMobile()) + if (!lkPlatformIs(PlatformType.iOS)) PopupMenuButton( icon: const Icon(Icons.volume_up), itemBuilder: (BuildContext context) { @@ -362,7 +369,7 @@ class _ControlsWidgetState extends State { ]; }, ), - if (!kIsWeb && lkPlatformIsMobile()) + if (!kIsWeb && lkPlatformIs(PlatformType.iOS)) IconButton( disabledColor: Colors.grey, onPressed: Hardware.instance.canSwitchSpeakerphone diff --git a/lib/src/hardware/hardware.dart b/lib/src/hardware/hardware.dart index 805d6898..7256c886 100644 --- a/lib/src/hardware/hardware.dart +++ b/lib/src/hardware/hardware.dart @@ -144,14 +144,13 @@ class Hardware { bool get preferSpeakerOutput => _preferSpeakerOutput; bool get canSwitchSpeakerphone => - (lkPlatformIs(PlatformType.iOS) && - !_preferSpeakerOutput && - [AudioTrackState.localOnly, AudioTrackState.localAndRemote] - .contains(audioTrackState)) || - lkPlatformIs(PlatformType.android); + lkPlatformIs(PlatformType.iOS) && + !_preferSpeakerOutput && + [AudioTrackState.localOnly, AudioTrackState.localAndRemote] + .contains(audioTrackState); Future setSpeakerphoneOn(bool enable) async { - if (lkPlatformIsMobile()) { + if (lkPlatformIs(PlatformType.iOS)) { speakerOn = enable; if (canSwitchSpeakerphone) { await rtc.Helper.setSpeakerphoneOn(enable); @@ -159,7 +158,7 @@ class Hardware { logger.warning('Can\'t switch speaker/earpiece'); } } else { - logger.warning('setSpeakerphoneOn only support on iOS/Android'); + logger.warning('setSpeakerphoneOn only support on iOS'); } } diff --git a/lib/src/track/audio_management.dart b/lib/src/track/audio_management.dart index f1fcc3ee..bfac47a6 100644 --- a/lib/src/track/audio_management.dart +++ b/lib/src/track/audio_management.dart @@ -125,7 +125,7 @@ Future _onAudioTrackCountDidChange() async { } } - if (lkPlatformIsMobile()) { + if (lkPlatformIs(PlatformType.iOS)) { if (Hardware.instance.speakerOn != null && Hardware.instance.canSwitchSpeakerphone) { await rtc.Helper.setSpeakerphoneOn(Hardware.instance.speakerOn!);