Change Video Input Device - Flutter
During the room at any point a participant wishes to switch his/her input video device, it can be done using the below mentioned methods.
getCameras()
-
This method of the
VideoSDKclass will give you the list of all the available cameras which can be shown in a list. -
This method will return a list of
CameraDeviceInfowhich will contain thedeviceId,labelandkindof the camera input devices. On Android, labels look likeCamera 1 (Front Camera). On iOS, the label is the camera's name, such asFront Camera, anddeviceIdis the same string.
changeCam()
-
After selecting the desired device, you can switch the camera input by passing its
deviceIdtochangeCam(deviceId: ...). -
Call
changeCam()without a device to switch to the next camera, for example from the front camera to the back camera. -
To switch to a custom camera track instead, pass it as
changeCam(customCameraTrack: track). This also turns the camera on. -
changeCam()returns aFuturethat throws aVideoSDKErrorif the switch fails, for example3051when the camera is not available. The same error also reaches yourEvents.errorhandler. -
VideoSDK.getSelectedVideoDevice()returns the camera in use: the one the room opened when you joined, or the one you switched to. -
To choose the camera the room starts with, pass
defaultCameratoVideoSDK.createRoom():FacingMode.FRONT, the default, orFacingMode.BACK.
Future<void> switchToNextCamera() async {
try {
// No device: switch to the next camera
await _room.changeCam();
} on VideoSDKError catch (error) {
print("changeCam failed: ${error.code} ${error.name}");
}
}
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<CameraDeviceInfo>? cameras = [];
@override
void initState() {
super.initState();
fetchCameras();
}
void fetchCameras() async {
cameras = await VideoSDK.getCameras();
setState(() {});
}
Future<void> changeCam(CameraDeviceInfo device) async {
try {
await _room.changeCam(deviceId: device.deviceId);
} on VideoSDKError catch (error) {
print("changeCam failed: ${error.code} ${error.name}");
}
}
@override
Widget build(BuildContext context) {
return Column(
children: [
ElevatedButton(
child: const Text("Change Camera"),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Select Camera Device"),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SingleChildScrollView(
reverse: true,
child: cameras != null && cameras!.isNotEmpty
? Column(
children: cameras!.map(
(device) => ElevatedButton(
child: Text(device.label),
onPressed: () async {
Navigator.pop(context);
await changeCam(device);
},
),
).toList(),
)
: const Text("No Camera Devices found."),
),
],
),
),
);
},
),
],
);
}
}
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

