-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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