Skip to content

Instantly share code, notes, and snippets.

@digitallysavvy
Created August 1, 2019 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitallysavvy/5db7b583b09dcc29b4be29d5fb77c21b to your computer and use it in GitHub Desktop.
Save digitallysavvy/5db7b583b09dcc29b4be29d5fb77c21b to your computer and use it in GitHub Desktop.
A code snippet implementing the stream.switchDevice method within the Agora.io SDK
// user clicks on an element within the camera list
$('#camera-list a').click(function(event) {
var index = event.target.id.split('_')[1];
changeStreamSource (index, "video");
});
// user clicks on an element within the mic list
$('#mic-list a').click(function(event) {
var index = event.target.id.split('_')[1];
changeStreamSource (index, "audio");
});
// switch the input device
function changeStreamSource (deviceIndex, deviceType) {
console.log('Switching stream sources for: ' + deviceType);
var deviceId;
var existingStream = false;
if (deviceType === "video") {
deviceId = devices.cameras[deviceIndex].deviceId
}
if(deviceType === "audio") {
deviceId = devices.mics[deviceIndex].deviceId;
}
localStreams.camera.stream.switchDevice(deviceType, deviceId, function(){
console.log('successfully switched to new device with id: ' + JSON.stringify(deviceId));
// set the active device ids
if(deviceType === "audio") {
localStreams.camera.micId = deviceId;
} else if (deviceType === "video") {
localStreams.camera.camId = deviceId;
} else {
console.log("unable to determine deviceType: " + deviceType);
}
}, function(){
console.log('failed to switch to new device with id: ' + JSON.stringify(deviceId));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment