Skip to main content
Version: 2.x.x

Methods returned by useMediaDevice Hook - React Native

useMediaDevice() returns the methods below. They work outside RoomProvider.

requestPermission()​

  • The requestPermission() method prompts the user for permission to use a camera and mic. It returns a Promise that resolves to a Map<string, boolean> object.
note

On Android, the SDK does not ask for camera and microphone access by itself. Call requestPermission() before you join. It works outside RoomProvider, so you can ask on your join screen. iOS asks on its own when the room turns the camera or mic on.

createCameraVideoTrack(), createMicrophoneAudioTrack() and runPreCallTest() never ask, on Android or iOS. Call requestPermission() before you use them.

Parameters​

  • Permission
    • A string specifying the specific kinds of media, that you want to request.
    • Optional
    • Allow Values : audio ,video,audio_video (Constants.permission.AUDIO, Constants.permission.VIDEO, Constants.permission.AUDIO_AND_VIDEO)
    • Default : audio_video

Returns​

  • Promise<Map<string, boolean>>

Example​

import { Constants, useMediaDevice } from "@videosdk.live/react-native-sdk";

const { requestPermission } = useMediaDevice();

const onPress = async () => {
try {
const requestAudioVideoPermission = await requestPermission(
Constants.permission.AUDIO_AND_VIDEO
);
console.log(
"Request Audio and Video Permissions",
requestAudioVideoPermission.get(Constants.permission.AUDIO),
requestAudioVideoPermission.get(Constants.permission.VIDEO)
);
} catch (ex) {
console.log("Error in requestPermission ", ex);
}
};
tip

requestPermission() does not throw when the user refuses access. Check the returned values instead: a permission that was not granted is false.


checkPermission()​

  • The checkPermission() method checks for permission to use a camera and mic. It returns a Promise that resolves to a Map<string, boolean> object.

Parameters​

  • Permission
    • A string specifying the types of media to check.
    • Optional
    • Allow Values : audio ,video,audio_video (Constants.permission.AUDIO, Constants.permission.VIDEO, Constants.permission.AUDIO_AND_VIDEO)
    • Default : audio_video

Returns​

  • Promise<Map<string, boolean>>

Example​

import { Constants, useMediaDevice } from "@videosdk.live/react-native-sdk";

const { checkPermission } = useMediaDevice();

const onPress = async () => {
try {
const checkAudioVideoPermission = await checkPermission();
console.log(
"check Audio and Video Permissions",
checkAudioVideoPermission.get(Constants.permission.AUDIO),
checkAudioVideoPermission.get(Constants.permission.VIDEO)
);
} catch (ex) {
console.log("Error in checkPermission ", ex);
}
};
tip

checkPermission() only reads the current permission state. It never prompts the user.


checkBlueToothPermission()​

  • The checkBlueToothPermission() method checks if the application has permission to access Bluetooth on the device. It returns a Promise that resolves to a boolean value indicating whether Bluetooth permission is granted.

Returns​

  • Promise<boolean>
info
  • This method is only supported on Android devices running Android 12 or later.
  • On iOS the returned Promise rejects.

Example​

import { Constants, useMediaDevice } from "@videosdk.live/react-native-sdk";

const { checkBlueToothPermission } = useMediaDevice();

const onPress = async () => {
try {
const checkBTPermission = await checkBlueToothPermission();
console.log("Check BT Permission:", checkBTPermission);
} catch (ex) {
console.log("Error in checkBTPermission ", ex);
}
};

requestBluetoothPermission()​

  • The requestBluetoothPermission() method requests permission to access Bluetooth on the device. It returns a Promise that resolves to a boolean value indicating whether the permission request was successful.

Returns​

  • Promise<boolean>
info
  • This method is only supported on Android devices running Android 12 or later.
  • On iOS the returned Promise rejects.

Example​

import { Constants, useMediaDevice } from "@videosdk.live/react-native-sdk";

const { requestBluetoothPermission } = useMediaDevice();

const onPress = async () => {
try {
const checkBTPermission = await requestBluetoothPermission();
console.log("Check BT Permission:", checkBTPermission);
} catch (ex) {
console.log("Error in checkBTPermission ", ex);
}
};

getCameras()​

  • Resolves the cameras the SDK can open, as CameraDeviceInfo objects.

  • Each CameraDeviceInfo has:

    1. deviceId: an identifier for the camera. Pass it to changeCam() or as cameraId to createCameraVideoTrack().
    2. kind: videoinput.
    3. label: a name for the camera.
    4. facingMode: "front", "back" or null when the platform does not say.

Returns​

  • Promise<Array<CameraDeviceInfo>>

Example​

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

const { getCameras } = useMediaDevice();

const onPress = async () => {
const cameras = await getCameras();
console.log("Cameras:", cameras);
};

getMics()​

  • Resolves the audio devices the SDK can route audio to, as MicrophoneDeviceInfo objects.

  • Each MicrophoneDeviceInfo has:

    1. deviceId: an identifier for the device. Pass it to changeMic(), to setAudioDevice(), or as microphoneId to createMicrophoneAudioTrack().
    2. kind: audioinput.
    3. label: a name for the device.

Returns​

  • Promise<Array<MicrophoneDeviceInfo>>

Example​

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

const { getMics } = useMediaDevice();

const onPress = async () => {
const mics = await getMics();
console.log("Audio devices:", mics);
};

getDevices()​

  • Resolves the audio devices from getMics(), followed by the cameras from getCameras(), in one array.
  • Each DeviceInfo has deviceId, label and kind (videoinput or audioinput). Cameras also have facingMode.

Returns​

  • Promise<Array<DeviceInfo>>

Example​

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

const { getDevices } = useMediaDevice();

const onPress = async () => {
const devices = await getDevices();
console.log("Devices:", devices);
};

getSelectedVideoDevice()​

  • Returns the camera you asked for by deviceId, for example with changeCam(), as { deviceId }. Returns null when the SDK chose the camera or you use a custom track.

Returns​

  • { deviceId: string } | null

getSelectedAudioDevice()​

  • Returns the audio device in use, as { deviceId }, or null when the SDK chose it.

Returns​

  • { deviceId: string } | null

Example​

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

function SelectedDevices() {
const { getSelectedVideoDevice, getSelectedAudioDevice } = useMediaDevice();

console.log("Camera:", getSelectedVideoDevice()?.deviceId);
console.log("Audio device:", getSelectedAudioDevice()?.deviceId);

return null;
}
tip

To route audio to another device, call setAudioDevice() from useRoom(). To follow devices being connected or removed, pass onAudioDeviceChanged to useMediaDevice().

Got a Question? Ask us on discord