Change Audio Output Device - Flutter
During the room at any point a participant wishes to switch his/her output audio like from headphones to speaker, it can be done using the below mentioned methods.
On Android and iOS, the microphone and the speaker share a single audio route, so setAudioDevice() changes both. To choose the microphone, see Change Audio Input Device.
getMics()
-
The
VideoSDK.getMics()method lists all the audio devices you can switch to, as a list ofMicrophoneDeviceInfoobjects. Each one is an audio route. -
The
MicrophoneDeviceInfoobject will contain thedeviceId,kindandlabelfor the device. -
On Android, the devices are
SPEAKER_PHONE,EARPIECE,WIRED_HEADSETandBLUETOOTH, as each one is connected. On iOS, the list holdsSpeakerand the microphones you can use, such asiPhone Microphone,Headphonesfor a wired headset, or the name of a Bluetooth headset.
setAudioDevice()
-
After selecting the desired device, you can switch the audio output by passing the corresponding
MicrophoneDeviceInfoobject to thesetAudioDevice()method of theRoom. -
The microphone stays muted or unmuted as it was.
-
You can call
setAudioDevice()before you join. The room uses that device when you join. -
setAudioDevice()returns aFuturethat throws aVideoSDKErrorif the switch fails. -
VideoSDK.getSelectedAudioDevice()returns the audio device in use, from join on, including the one you pick withsetAudioDevice()orchangeMic(). It isnullbefore join.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class RoomScreen extends StatefulWidget {
const RoomScreen({Key? key}) : super(key: key);
@override
_RoomScreenState createState() => _RoomScreenState();
}
class _RoomScreenState extends State<RoomScreen> {
late Room _room;
List<MicrophoneDeviceInfo> speakers = [];
@override
void initState() {
super.initState();
fetchSpeakers();
}
void fetchSpeakers() async {
List<MicrophoneDeviceInfo>? audioDevices = await VideoSDK.getMics();
setState(() => speakers = audioDevices ?? []);
}
Future<void> setAudioDevice(MicrophoneDeviceInfo device) async {
try {
await _room.setAudioDevice(device);
} on VideoSDKError catch (error) {
print("setAudioDevice failed: ${error.code} ${error.name}");
}
}
@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
child: const Text("Change Audio Device"),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Select Audio Device"),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SingleChildScrollView(
reverse: true,
child: speakers.isNotEmpty
? Column(
children: speakers
.map(
(device) => ElevatedButton(
child: Text(device.label),
onPressed: () async {
Navigator.pop(context);
await setAudioDevice(device);
},
),
)
.toList(),
)
: const Text("No Speaker Devices found."),
)
],
),
),
);
}),
]);
}
}
setAudioDeviceChangeListener()
-
The audio route changes on its own when a headset or Bluetooth device connects or disconnects.
-
Pass a listener to
room.setAudioDeviceChangeListener()to hear about it. It receives the device now in use and every device available. -
Pass
nullto stop listening.
_room.setAudioDeviceChangeListener(
(MicrophoneDeviceInfo? selectedDevice, List<MicrophoneDeviceInfo> devices) {
print("Audio now on ${selectedDevice?.label}");
setState(() => speakers = devices);
},
);
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

