400-881-9892

文档中心

官方文档,可查阅产品介绍、快速入门、用户指南、开发指南、API参考、SDK参考、帮助等信息。

文档中心 互动课堂

Local Device Management

    Local Device Management

    DeviceMgr
    • Return a device management class object
    • Method example
    1
    var deviceMgr = TK.DeviceMgr
    
    getDevices
    • This method enumerates the cameras, microphones, and speakers in the system
    Parameter name Required Type Description
    callback Yes Function If the method is called successfully,the callback parameter object will include the devices object containing system device information and the useDevices object containing information about the devices currently in use by the system. If the call fails, the device information in the devices and useDevices objects will be empty
    • The devices object contains information objects for cameras (videoinput), microphones (audioinput), and speakers (audiooutput). Each device object includes three properties:
    Parameter name Type Description
    deviceId String The unique device ID for that particular device
    label String A label used to distinguish devices. If the user does not have permission to access the camera and microphone, the label will be named in a specific format with an index
    kind String Device type, which includes three types based on the selected audio and video devices: videoinput, audioinput, and audiooutput
    • The userDevices object contains the device IDs of the cameras (videoinput), microphones (audioinput), and speakers (audiooutput) that the system is currently using
    • Method example

    1
    2
    3
    4
    5
    6
    7
    TK.DeviceMgr.getDevices(function (deviceInfo) {
      var devices = deviceInfo.devices;
      var usingDevices = deviceInfo. useDevices;
      var audioInputDevArr = devices.audioinput;
      var audioInputDevCount = audioInputDevArr.length;
      var usingAudioInputId = usingDevices. audioinput;
    }
    
    - This method sets the cameras, microphones, and speakers that the system will use - When setDevices sets videoinput in multi-video mode, it refers to setting the primary camera. Setting the primary camera means associating the audio with the primary camera. Since there is no switching issue for the primary camera in multi-video mode, the videoinput switched by this interface is the primary camera (i.e., the camera associated with the audio)

    Parameter name Required Type Description
    deviceIdMap Yes Object An object containing the device ID. The device ID is obtained through the getDevices method
    onFailure No Function Failure callback
    onSuccess No Function Success callback
    • Method example
    1
    2
    3
    4
    5
    6
    7
    TK.DeviceMgr.getDevices(function (deviceInfo) {
        var devices = deviceInfo.devices;
        var usingDevices = deviceInfo. useDevices;
        var audioInputDevArr = devices.audioinput;
        var audioInputDevCount = audioInputDevArr.length;
        var usingAudioInputId = usingDevices. audioinput;
     }
    
    setDevices
    • This method sets the cameras, microphones, and speakers that the system will use
    • When setDevices sets videoinput in multi-video mode, it refers to setting the primary camera. Setting the primary camera means associating the audio with the primary camera. Since there is no switching issue for the primary camera in multi-video mode, the videoinput switched by this interface is the primary camera (i.e., the camera associated with the audio)
    Parameter name Required Type Description
    deviceIdMap Yes Object An object containing the device ID. The device ID is obtained through the getDevices method
    onFailure No Function Failure callback
    onSuccess No Function Success callback
    • Method example
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    function setLocalDevices(videoInputId, audioInputId, audioOutputId) {
      var deviceIdMap = {
          videoinput : videoInputId,
          audioinput : audioInputId,
          audiooutput : audioOutputId};TK.DeviceMgr.setDevices(deviceIdMap, function(error) {
        console.error(set devices failed, error);
    }, function() {
      console.log("set devices success")
    });
    }
    
    getCameras
    • This method enumerates the camera devices in the system
    Parameter name Required Type Description
    callback Yes Function If the method call is successful, the callback parameter will be an array containing information about the system's camera objects. If the call fails or no devices are available, an empty array will be returned
    • Method example
    1
    2
    3
    TK.DeviceMgr.getCameras(function (cameras) {
      console.log("camera data array: ", cameras)
    })
    
    setCamera
    • This method sets the camera devices in the system
    Parameter name Required Type Description
    deviceId Yes String Camera device ID
    onFailure No Function Failure callback
    isForce No Boolean Whether to force the setting,The default value is false
    onSuccess No Function Success callback
    • Method example
    1
    2
    3
    TK.DeviceMgr.setCamera("xxx", function(err) {
      console.error("setCamera error:", err)}, false, function() {
      console.log("set camera success")})
    
    getMicrophones
    • This method enumerates the microphone devices in the system
    Parameter name Required Type Description
    callback Yes Function If the method call is successful, the callback parameter will be an array containing information about the system's microphone objects. If the call fails or no devices are available, an empty array will be returned
    • Method example
    1
    2
    3
    TK.DeviceMgr.getMicrophones(function (mics) {
      console.log("mic data array: ", mics)
    })
    
    setMicrophone
    • This method sets the microphone device to be used by the system
    Parameter name Required Type Description
    deviceId Yes String Microphone device ID
    onFailure No Function Failure callback
    onSuccess No Function Success callback
    • Method example
    1
    2
    3
    TK.DeviceMgr.setMicrophone("xxx", function(err) {
      console.error("setMicrophone error:", err)}, function() {
      console.log("setMicrophone success")})
    
    getSpeakers
    • This method enumerates the speaker devices in the system
    Parameter name Required Type Description
    callback Yes Function If the method call is successful, the callback parameter will be an array containing information about the system's speaker objects. If the call fails or no devices are available, an empty array will be returned
    • Method example
    1
    2
    3
    TK.DeviceMgr.getSpeakers(function (outputs) {
      console.log("output data array: ", outputs)
    })
    
    setSpeaker
    • This method sets the speaker device to be used by the system
    Parameter name Required Type Description
    deviceId Yes String Speaker device ID
    onFailure No Function Failure callback
    onSuccess No Function Success callback
    • Method example
    1
    2
    3
    TK.DeviceMgr.setSpeaker("xxx", function(err) {
      console.error("setSpeaker error:", err)}, function() {
      console.log("setSpeaker success")})
    
    startVideoTest
    • This method begins to check whether the camera is available
    • If a camera is currently being tested, calling startVideoTest will automatically stop the ongoing test. However, to stop the last camera test, the stopVideoTest method must be called
    Parameter name Required Type Description
    deviceId Yes String The ID of the device to be tested. The device ID is obtained through the getDevices method
    elementId Yes String The ID of the DOM element. This DOM element is used to contain the video window corresponding to the camera
    options No Object Playback configuration options, which contain properties as described below
    onSuccess No Function The callback function after the video is in the canplay state
    onFailure No Function Method invocation/Video failure callback
    • options
    Parameter name Type Description
    mirror Boolean The video is displayed in mirror mode, default is false
    loader Boolean Indicates whether to display the "loading" style when video data is not loaded, default is true
    mode Number Used to indicate the video display mode (whether to crop), possible values can be found in the video display mode section, default is TK_VIDEO_MODE.APECT_RATIO_CONTAIN
    • Method example
    1
    2
    3
    4
    5
    function startVideoTest(videoInputId) {
       var container = document.createElement("div");
       container.id = " container ";
      document.body.appendChild(container);
      TK.DeviceMgr.startVideoTest(videoInputId, container.id);}
    
    stopVideoTest
    • This method stops the camera detection
    • To avoid potential device occupation issues and other problems that may arise when entering the classroom, it is necessary to promptly disable the detection.
    Parameter name Required Type Description
    deviceId No String To stop the detection of a specific device, provide the corresponding device ID used when starting the detection. If no device ID is provided, it will default to stopping all detections initiated by calling startVideoTest for the specified deviceid
    onSuccess No Function Success callback
    onFailure No Function Failure callback
    • Method example
    1
    2
    3
    TK.DeviceMgr.stopVideoTest("xxx", function() {
      console.log("stopVideoTest success")}, function(err) {
      console.error("stopVideoTest error: ", err)})
    
    startMicrophoneTest
    • This method starts to detect whether the microphone is available
    • If a microphone is currently being tested, calling startMicrophoneTest will automatically stop the ongoing detection process. However, to stop the last microphone detection, the stopMicrophoneTest method must be called
    Parameter name Required Type Description
    deviceId Yes String The device ID to be detected. The device ID is obtained through the getDevices method
    elementId Yes String The DOM element ID. This DOM element is used to contain the player for playing the microphone audio
    onSuccess No Function The success callback for the method call, carrying the current volume value captured by the microphone
    onFailure No Function The failure callback for the method call
    • Method example
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    function startMicrophoneTest(audioInputId) {
      var container = document.createElement("div");
       container.id = container;
      document.body.appendChild(container);
      var onSuccess = function(vol) {
          console.log(microphone volume: , vol);
      }
      var onFailure = function(err) {
          console.log(microphone test failed. , err);
      }
      TK.DeviceMgr.startMicrophoneTest(audioInputId, container.id, onSuccess, onFailure);}
    
    stopMicrophoneTest
    • This method stops the microphone detection
    Parameter name Required Type Description
    onSuccess No Function Success callback
    • Method example
    1
    2
    TK.DeviceMgr.stopMicrophoneTest(function() {
      console.log("stopMicrophoneTest success")})
    
    startSpeakerTest
    • This method starts to detect whether the speaker is available
    • If a speaker is currently being tested, calling startSpeakerTest will automatically stop the ongoing detection process. However, to stop the last speaker detection, the stopSpeakerTest method must be
    Parameter name Required Type Description
    deviceId Yes String The device ID to be detected. The device ID is obtained through the getDevices method
    audioUrl Yes String The absolute URL of the audio file. It is recommended to use a file in WAV format
    onFailure No Function Failure callback
    onSuccess No Function The success callback, which will be triggered periodically to return the current volume of the audio media file
    • Method example
    1
    2
    3
    4
    TK.DeviceMgr.startSpeakerTest(audioInputId, audioUrl, function(err){
      console.log(“speaker test failed. ”, err);},function(volume) {
      console.log("speaker test success")
      console.log("speaker media current volume: ", volume)});
    
    stopSpeakerTest
    • This method stops the speaker detection
    Parameter name Required Type Description
    onSuccess No Function Success callback
    • Method example
    1
    2
    TK.DeviceMgr.stopSpeakerTest(function(){
      console.log("stopSpeakerTest success")})
    
    startNetworkTest
    • This method starts the network quality detection
    • After the API call is made, network speed testing will be performed in a polling manner, and data will be continuously returned through the onSuccess callback
    Parameter name Required Type Description
    onSuccess Yes Function The success callback. The first parameter of the callback indicates the network quality (possible values can be found in the Network Quality section); the second parameter represents the latency (measured in milliseconds); the third parameter indicates the detailed uplink and downlink network quality (explained as follows)
    onFailure No Function Failure callback
    • networkQualityDetail
    Parameter name Type Description
    downlinkDelayLevel Number Downlink network quality (possible values can be found in the Network Quality section)
    uplinkDelayLevel Number Uplink network quality (possible values can be found in the Network Quality section)
    downlinkDelay Number Downlink network latency (measured in milliseconds)
    uplinkDelay Number Uplink network latency (measured in milliseconds)
    • Method example
    1
    2
    TK.DeviceMgr.startNetworkTest(function(delayLevel,currentDelay, { downlinkDelayLevel, uplinkDelayLevel, downlinkDelay, uplinkDelay }){
      //The code that implements the business logic});
    
    stopNetworkTest
    • This method stops the network quality detection
    • Method example
    1
    TK.DeviceMgr.stopNetworkTest()
    
    registerDeviceChangeListener
    • This method adds a listener for device changes (plugging in or unplugging)
    • Only one listener can be added. If added repeatedly, the previously added listener will become invalid
    • After testing, it has been found that the behavior of an extremely small number of devices is abnormal. The callback function will be triggered continuously even without any plug-in or plug-out operations on the devices
    Parameter name Required Type Description
    listener Yes Function The callback function, which will be called when a device change occurs. The parameters of the callback are described as follows
    Parameter name Type Description
    changed Object The information of the changed device. Example: {videoinput:[{kind:’videoinput’,label:’Video device label’,deviceId:’Video device ID’}]}
    type Number The type of change, with possible values being:(Device added: TK.DEVICE_STATE.DEVICE_ADD)(Device removed: TK.DEVICE_STATE.DEVICE_REMOVE)
    • Method example

    1
    2
    3
    4
    5
    6
    var deviceChangeListener = (changed,type) => {
      if (type == TK.DEVICE_STATE.DEVICE_ADD) {
          console.log("device add, deviceInfo: ", changed)
      } else if (type == TK.DEVICE_STATE.DEVICE_REMOVE) {
          console.log("device remove, deviceInfo: ", changed)
      }};TK.DeviceMgr.registerDeviceChangeListener(deviceChangeListener);
    
    - Remove the device listener

    1
    TK.DeviceMgr.registerDeviceChangeListener(() => {});
    
    getAudioMediaStream
    • This method retrieves the raw audio data of the specified user
    • This interface can only be used to obtain the audio data of users who have already joined the stage. When obtaining your own audio data, you do not need to be on stage
    • The audio data obtained through the success callback is in the format of 16-bit, 16000 Hz, mono-channel arrayBuffer data
    Parameter name Required Type Description
    userid Yes String User id
    option No Object Optional configuration. Currently, only user audio data is supported, and no values should be passed
    onSuccess Yes Function Success callback
    onFailure No Function Failure callback
    • Method example
    1
    2
    // The callback will be triggered frequently because the audio data is returned in real-time var onGetAudioData = (arrayBufferData) => {
      console.log("audio data:", arrayBufferData)};TK.DeviceMgr.getAudioMediaStream(("xxx", {}, onGetAudioData));