功能描述
本文主要介绍如何检测用户的设备插入、拔出、启动的行为。
实现流程
检测设备插入、拔出
avdEngine.initDevice().then(function(result){
    avdEngine.addCallback(EngineCallback.device_camera_change, onCameraDeviceChangeHandle);
    avdEngine.addCallback(EngineCallback.device_microphone_change, onMicrophoneDeviceChangeHandle);
}).otherwise(function(error){
});
/**
 * changeTypeEnum.add:插入; changeTypeEnum.remove:拔出
 */
function onCameraDeviceChangeHandle(changeType, deviceIdList) {
}
function onMicrophoneDeviceChangeHandle(changeType, deviceIdList) {
    console.log:(`麦克风设备有${changeType == changeTypeEnum.add ? '新增' : '移除'},请刷新设备!`);
}判断是否当前正在使用的设备被拔出
avdEngine.initDevice().then(function(result){
    avdEngine.addCallback(EngineCallback.device_camera_change, onCameraDeviceChangeHandle);
    avdEngine.addCallback(EngineCallback.device_microphone_change, onMicrophoneDeviceChangeHandle);
}).otherwise(function(error){
});
function onCameraDeviceChangeHandle(changeType, deviceIdList) {
    if(changeType == changeTypeEnum.remove){
        for(var key in deviceIdList) {
            var deviceId = deviceIdList[key];
            console.log("被拔出的摄像头ID:"+deviceId);
            var videos = room.selfUser.videos;
            for(var key1 in videos) {
                var video = videos[key1];
                if(video.status != StreamStatus.init && video.id == deviceId){
                    console.log("当前正在使用的摄像头设备被拔出,摄像头ID:"+deviceId);
                }
            }
        }
    }
}
function onMicrophoneDeviceChangeHandle(changeType, deviceIdList) {
    if(changeType == changeTypeEnum.remove){
        for(var key in deviceIdList) {
            var deviceId = deviceIdList[key];
            console.log("被拔出的麦克风ID:"+deviceId);
            var audio = room.selfUser.audio;
            if(audio.status != StreamStatus.init && audio.id == deviceId){
                console.log("当前正在使用的麦克风设备被拔出,麦克风ID:"+deviceId);
            }
        }
    }
}在用户插入新设备时,自动打开使用新设备。
avdEngine.initDevice().then(function(result){
    avdEngine.addCallback(EngineCallback.device_camera_change, onCameraDeviceChangeHandle);
}).otherwise(function(error){
});
function onCameraDeviceChangeHandle(changeType, deviceIdList) {
    if(changeType == changeTypeEnum.add){
        var deviceId = deviceIdList[0];
        var video = room.selfUser.getVideo(cameraId1);  
        video.previewAndPublish(videoElement);
    }
}