Change Audio Output Device - Flutter
During the meeting 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.
important
This feature works for input and output device on Mobile apps, and for output device on Web and Desktop apps.
getAudioDevices()
-
The
getAudioDevices()method will help you to list down all possible connected audio output devices which will return a list ofAudioDeviceInfoobjects. -
The
AudioDeviceInfoobject will contain thedeviceId,kind,groupIdandlabelfor the device.
switchAudioDevice()
- After selecting the desired device, you can switch the audio output by passing the corresponding
AudioDeviceInfoobject as a parameter to theswitchAudioDevice()method.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class MeetingScreen extends StatefulWidget {
const MeetingScreen({Key? key}) : super(key: key);
@override
_MeetingScreenState createState() => _MeetingScreenState();
}
class _MeetingScreenState extends State<MeetingScreen> {
late Room _room;
List<AudioDeviceInfo>? speakers = [];
@override
void initState() {
super.initState();
fetchSpeakers();
}
void fetchSpeakers() async {
List<AudioDeviceInfo>? audioDevices = await VideoSDK.getAudioDevices();
for (AudioDeviceInfo device in audioDevices!) {
if (device.kind == 'audiooutput') {
speakers?.add(device);
}
}
setState(() {});
}
@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 != null && speakers!.isNotEmpty
? Column(
children: speakers!
.map(
(device) => ElevatedButton(
child: Text(device.label),
onPressed: () {
_room.switchAudioDevice(device);
Navigator.pop(context);
},
),
)
.toList(),
)
: const Text("No Speaker Devices found."),
)
],
),
),
)
}),
]);
}
}
note
For iOS devices:
EARPIECEis not supported wheneverWIRED_HEADSETorBLUETOOTHdevice is connected.WIRED_HEADSETandBLUETOOTHdevices are not supported simultaneously. Priority is given to the most recently connected device.
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

