Agora supports the media device test and selection feature, allowing you to check if a camera or an audio device (a headset, microphone, or speaker) works properly.
You can use the media device test and selection feature in the following scenarios:
Before proceeding, ensure that you have implemented basic real-time functions in your project. See Start a Call or Start Live Interactive Streaming for details.
Refer to the following steps to test the microphone and camera:
AgoraRTC.getDevices
method to get available devices.AgoraRTC.createStream
method to create an audio or video stream. In this method, fill the microphoneId
or cameraId
parameter with the device ID to specify the device to be tested.Stream.init
method to initialize the stream. In the onSuccess
callback function, play the stream.Stream.getAudioLevel
method to get the current audio level. The audio level is greater than 0 if the microphone is working.// Find all audio and video devices
AgoraRTC.getDevices(function(devices){
var audioDevices = devices.filter(function(device){
return device.kind === "audioinput";
});
var videoDevices = devices.filter(function(device){
return device.kind === "videoinput";
});
var uid = Math.floor(Math.random()*10000);
var selectedMicrophoneId = ...;
var selectedCameraId = ...;
var stream = AgoraRTC.createStream({
streamID: uid,
// Set audio to true if testing microphone
audio: true,
microphoneId: selectedMicrophoneId,
// Set video to true if testing camera
video: true,
cameraId: selectedCameraId,
screen: false
});
// Initialize the stream
stream.init(function(){
stream.play("mic-test");
// Print the audio level every 1000 ms
setInterval(function(){
console.log(`Local Stream Audio Level ${stream.getAudioLevel()}`);
}, 1000);
})
});
Due to browser security limitations, the Agora Web SDK does not support playing local audio files. You can use the following method to test the audio playback devices:
Use the HTML5 <audio>
element to create an audio player on the web page for users to play an online audio file and confirm whether they can hear the sound.
stream.init
, see the API Reference for details.getDevices
method every time before testing the devices.