Skip to main content
Version: 5.x.x

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.

important

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 of MicrophoneDeviceInfo objects. Each one is an audio route.

  • The MicrophoneDeviceInfo object will contain the deviceId, kind and label for the device.

  • On Android, the devices are SPEAKER_PHONE, EARPIECE, WIRED_HEADSET and BLUETOOTH, as each one is connected. On iOS, the list holds Speaker and the microphones you can use, such as iPhone Microphone, Headphones for 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 MicrophoneDeviceInfo object to the setAudioDevice() method of the Room.

  • 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 a Future that throws a VideoSDKError if the switch fails.

  • VideoSDK.getSelectedAudioDevice() returns the audio device in use, from join on, including the one you pick with setAudioDevice() or changeMic(). It is null before 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 null to 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