TXDeviceManager

TXDeviceManager

设备管理接口。主要用于管理摄像头、⻨克⻛设备。如果开启了本地混流功能,需要传入对应的流 id 进行操作。

通过 TXLivePusher 方法 getDeviceManager() 来获取对象实例。

const deviceManager = livePusher.getDeviceManager();

Methods

(async) getDevicesList(typeopt) → {Promise.<Array.<TXDeviceManager~TXMediaDeviceInfo>>}

获取设备列表。

说明:

  1. 该接口不支持在 http 协议下使用,请使用 https 协议部署您的网站。
  2. 浏览器出于安全的考虑,在用户未授权摄像头或⻨克⻛访问权限前,deviceId 及 deviceName 字段可能都是空的。因此建议在用户授权访问后,再调用该接口获取设备详情。
Example
deviceManager.getDevicesList().then((data) => {
  data.forEach((item) => {
    console.log(item.type + ' device: ' + item.deviceName + ' ' + item.deviceId);
  });
});
Parameters:
Name Type Attributes Description
type string <optional>

取值 'video' 或者 'audio',可选参数。不传返回所有设备列表,传 video 返回摄像头设备列表,传 audio 返回⻨克⻛设备列表。

Returns:

返回 Promise 对象,其中设备信息结构请参考 TXMediaDeviceInfo

Type
Promise.<Array.<TXDeviceManager~TXMediaDeviceInfo>>

(async) getCurrentDevice(type, streamIdopt) → {Promise.<TXDeviceManager~TXMediaDeviceInfo>}

获取当前流的设备信息。如果启用了本地混流,必须指定流 id。

Example
deviceManager.getCurrentDevice('video').then((data) => {
  if (data) {
    console.log('current device is ' + data.deviceName + ' ' + data.deviceId);
  }
});
Parameters:
Name Type Attributes Description
type string

设备类型,video - 摄像头设备,audio - ⻨克⻛设备。

streamId string <optional>

流 id,指定要获取设备信息的流,启用本地混流后必须传,对应的流必须是摄像头或者麦克风设备采集的流。

Returns:

返回 Promise 对象,其中设备信息结构请参考 TXMediaDeviceInfo

Type
Promise.<TXDeviceManager~TXMediaDeviceInfo>

(async) switchDevice(type, deviceId, streamIdopt) → {Promise.<void>}

切换当前正在使用的设备。如果启用了本地混流,必须指定流 id。

说明:

  1. 该方法仅适用于从摄像头和⻨克⻛采集音视频时调用,其他采集方式采集的流不支持调用该接口。
  2. 如果还没开始推流,则只更新本地流;如果已经开始推流,同步更新推到服务器的音视频流。
  3. 切换流的设备时,对应流的 id 不会发生变化。
  4. 指定流 id 时,对应流的类型必须和 type 匹配,比如 type 是 video,对应的流必须是摄像头采集的流。
  5. 建议直接使用 switchCamera()switchMicrophone()
Example
deviceManager.getDevicesList('video').then((data) => {
  if (data.length > 0) {
    deviceManager.switchDevice('video', data[0].deviceId);
  }
});
Parameters:
Name Type Attributes Description
type string

设备类型,video - 摄像头设备,audio - ⻨克⻛设备。

deviceId string

设备 id,可以通过调用 getDevicesList() 获取设备 id 。

streamId string <optional>

流 id,指定要切换设备的流,启用本地混流后必须传,对应的流必须是摄像头或者麦克风设备采集的流。

Returns:

返回 Promise 对象。

Type
Promise.<void>

switchCamera(deviceId, streamIdopt) → {Promise.<void>}

切换摄像头设备。等同于 switchDevice('video', deviceId, streamId)

Example
deviceManager.getDevicesList('video').then((data) => {
  if (data.length > 0) {
    deviceManager.switchCamera(data[0].deviceId);
  }
});
Parameters:
Name Type Attributes Description
deviceId string

设备 id,可以通过调用 getDevicesList() 获取设备 id 。在移动设备上,可以通过传入 'user' 和 'environment' 来切换前置和后置摄像头。

streamId string <optional>

流 id,指定要切换摄像头设备的流,启用本地混流后必须传,对应的流必须是摄像头设备采集的流。

Returns:

返回 Promise 对象。

Type
Promise.<void>

switchMicrophone(deviceId, streamIdopt) → {Promise.<void>}

切换⻨克⻛设备。等同于 switchDevice('audio', deviceId, streamId)

Example
deviceManager.getDevicesList('audio').then((data) => {
  if (data.length > 0) {
    deviceManager.switchMicrophone(data[0].deviceId);
  }
});
Parameters:
Name Type Attributes Description
deviceId string

设备 id,可以通过调用 getDevicesList() 获取设备 id 。

streamId string <optional>

流 id,指定要切换麦克风设备的流,启用本地混流后必须传,对应的流必须是麦克风设备采集的流。

Returns:

返回 Promise 对象。

Type
Promise.<void>

Type Definitions

TXMediaDeviceInfo

Properties:
Name Type Description
type string

设备类型,video - 摄像头,audio - 麦克风。

deviceId string

设备 id。

deviceName string

设备名称。

设备信息。具体使用请参考 getDevicesList()getCurrentDevice()

Type:
  • object