VikR
VikR

Reputation: 5142

WebRTC with Agora.io: SwitchCamera?

I'm building a web app using Agora.io. I'd like to be able to switch from the front-facing to the rear-facing camera on a client's smartphone. I've seen agora docs for switchCamera. I've tried the following:

switchCamera();
myAgoraClient.switchCamera();
localStream.switchCamera();

All of them throw a no such function error.

What is the correct way to switch the camera in agora.io for web?

Upvotes: 3

Views: 4895

Answers (4)

Azhar Rasheed
Azhar Rasheed

Reputation: 51

here is a simple way to do it on react native with the latest version

_engine && _engine.current?.switchCamera();

make sure you have correctly initialized the agora engine before executing this code.

Upvotes: 0

Akhilendra Singh
Akhilendra Singh

Reputation: 769

Below code will work in agora web 4.2+. We get the list of devices available on button click and then switch camera. I do get mirroring on back camera. Not sure how to solve it. But camera flip works fine.

 async function initDevices() {
    cams = await AgoraRTC.getCameras();
    const videoTrackLabel = localTracks.videoTrack.getTrackLabel();
    currentCam = cams.find(item => item.label === videoTrackLabel);
    var cameraSwitched=false;
    cams.forEach(cam => {
        if(cam.label !== currentCam.label && !cameraSwitched){
            switchCamera(cam.label);
            cameraSwitched=true;
        }
    });
}
async function switchCamera(label) {
    currentCam = cams.find(cam => cam.label === label);
    await localTracks.videoTrack.setDevice(currentCam.deviceId);
}
$('#flip-camera-btn').click(function (e) {
    initDevices();
})

Upvotes: 1

siniradam
siniradam

Reputation: 2929

You can do this with switchDevice method in 2.5 SDK

switchDevice(type: string, deviceId: string, onSuccess: function, onFailure: function): void

For example;

localStream.switchDevice("video", "<deviceid>", console.log,console.log)

Should do the work. I was trying same thing but examples contains old 2.4 js sdk, you need to specifically download ^2.5

Upvotes: 3

Galaxy
Galaxy

Reputation: 1279

I don't think the latest version of Agora Web SDK have these methods out of box.

However, you can create a camera list through:

AgoraRTC.getDevices(function(devices){
  cameras = devices.filter(device => device.kind === 'videoinput');
});

Then give switchCamera an implementation:

function switchCamera(){
    cameraIndex = (++cameraIndex) % cameras.length;
}

Use cameras[cameraIndex].deviceId as cameraId to create a new local video stream and publish it out.

Upvotes: 1

Related Questions