Skip to main content
Version: 5.x.x

Change Audio Input Device - Flutter

During the room at any point a participant wishes to switch his/her input audio device, it can be done using the below mentioned methods.

important

On Android and iOS, the microphone and the speaker share a single audio route. Changing the microphone with changeMic() also changes where you hear audio, and setAudioDevice() changes both too.

getMics()​

  • The getMics() method of the VideoSDK class lists 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, label and kind for the device.

  • On Android, the devices are SPEAKER_PHONE, EARPIECE, WIRED_HEADSET and BLUETOOTH, as each one is connected. On iOS, the list starts with Speaker, which is not a microphone, followed by the microphones you can use, such as iPhone Microphone, Headphones for a wired headset, or the name of a Bluetooth headset.

changeMic()​

  • After selecting the desired device, you can switch the audio input by passing its deviceId to changeMic(deviceId: ...).

  • During a room, calling changeMic() without a device switches to the next device in the list.

  • To switch to a custom microphone track instead, pass it as changeMic(customMicrophoneTrack: track). This also unmutes the microphone.

  • A muted microphone stays muted. If the microphone is on, it is turned off and on again on the new device, so you get Events.streamDisabled and then Events.streamEnabled for your audio stream.

  • You can call changeMic() with a device before you join. The room uses that device when you join.

  • changeMic() returns a Future that throws a VideoSDKError if the switch fails, for example 3059 when the device is not available. The same error also reaches your Events.error handler.

  • VideoSDK.getSelectedAudioDevice() returns the audio device in use, from join on, including the one you pick with changeMic() or setAudioDevice(). It is null before join.

Example​

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> mics = [];

@override
void initState() {
super.initState();
fetchMics();
}

void fetchMics() async {
List<MicrophoneDeviceInfo>? audioDevices = await VideoSDK.getMics();
setState(() {
// On iOS, "Speaker" is not a microphone.
mics = (audioDevices ?? [])
.where((device) => device.label != 'Speaker')
.toList();
});
}

Future<void> changeMic(MicrophoneDeviceInfo device) async {
try {
await _room.changeMic(deviceId: device.deviceId);
} on VideoSDKError catch (error) {
print("changeMic failed: ${error.code} ${error.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
child: Text("Change Input 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: mics.isNotEmpty
? Column(
children: mics
.map(
(device) => ElevatedButton(
child: Text(device.label),
onPressed: () async {
Navigator.pop(context);
await changeMic(device);
},
),
)
.toList(),
)
: const Text("No Microphone Devices found."),
)
],
),
),
);
}),
]);
}
}

API Reference​

The API references for all the methods and events utilized in this guide are provided below.

Got a Question? Ask us on discord