Skip to main content
Version: 5.x.x

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 VideoSDK class will give you the list of all the available cameras which can be shown in a list.

  • This method will return a list of CameraDeviceInfo which will contain the deviceId, label and kind of the camera input devices. On Android, labels look like Camera 1 (Front Camera). On iOS, the label is the camera's name, such as Front Camera, and deviceId is the same string.

changeCam()​

  • After selecting the desired device, you can switch the camera input by passing its deviceId to changeCam(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 a Future that throws a VideoSDKError if the switch fails, for example 3051 when the camera is not available. The same error also reaches your Events.error handler.

  • 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 defaultCamera to VideoSDK.createRoom(): FacingMode.FRONT, the default, or FacingMode.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