setDefaultSpeaker method
Sets the default audio output device as the used one.
Only meaningful on mobile devices.
Implementation
Future<void> setDefaultSpeaker() async {
if (_isMobile) {
final AudioSession session = await AudioSession.instance;
final Set<AudioDevice> devices = await session.getDevices(
includeInputs: false,
);
final bool hasHeadphones = devices.any(
(e) =>
e.type == AudioDeviceType.wiredHeadset ||
e.type == AudioDeviceType.wiredHeadphones ||
e.type == AudioDeviceType.bluetoothA2dp ||
e.type == AudioDeviceType.bluetoothLe ||
e.type == AudioDeviceType.bluetoothSco ||
e.type == AudioDeviceType.usbAudio,
);
if (hasHeadphones) {
await setSpeaker(AudioSpeakerKind.headphones);
} else {
await setSpeaker(AudioSpeakerKind.speaker);
}
if (PlatformUtils.isAndroid) {
await AndroidAudioManager().setMode(AndroidAudioHardwareMode.normal);
} else if (PlatformUtils.isIOS) {
await AVAudioSession().setCategory(
AVAudioSessionCategory.playAndRecord,
AVAudioSessionCategoryOptions.allowBluetooth,
AVAudioSessionMode.defaultMode,
);
}
}
}