Twilio Video - 切换媒体设备选项不起作用

Twilio Video - switch media devices option not working

我正在使用 twiliotwilio-video v beta-2 指望这个 repohttps://github.com/twilio/video-quickstart-js 的主分支
我必须显示 select 媒体并将设备推入其中,但是当我尝试更新视频设备时出现错误

updateVideoDevice error TypeError: track must be a LocalAudioTrack, LocalVideoTrack, LocalDataTrack, or MediaStreamTrack
at Object.INVALID_TYPE (index.js:30952)
at Object.validateLocalTrack (index.js:31469)
at LocalParticipant.unpublishTrack (index.js:17047)
at index.js:17096
at Array.reduce (<anonymous>)
at LocalParticipant.unpublishTracks (index.js:17095)
at index.js:36056

我的updateVideoDevice函数如下

function updateVideoDevice(event) {
const select = event.target;
const localParticipant = room.localParticipant;
if (select.value !== '') {
    Video.createLocalVideoTrack({
        deviceId: { exact: select.value }
    }).then(function(localVideoTrack) {
        const tracks = Array.from(localParticipant.videoTracks.values());
        localParticipant.unpublishTracks(tracks);
        log(localParticipant.identity + " removed track: " + tracks[0].kind);
        detachTracks(tracks);

        localParticipant.publishTrack(localVideoTrack);
        log(localParticipant.identity + " added track: " + localVideoTrack.kind);
        const previewContainer = document.getElementById('local-media');
        attachTracks([localVideoTrack], previewContainer);
    })
    .catch(error => {
        console.error('updateVideoDevice error' ,error);
    });
}
}

任何人都可以解释我做错了什么吗?

这里是 Twilio 开发人员布道者。

这看起来是 Twilio Video JS v1 和 v2 之间的重大变化。在 v2 文档中,调用 localParticipant.videoTracks returns a Map of <Track.SID, LocalVideoTrackPublication>. Calling .values() on that map returns an iterator of LocalVideoTrackPublications 然后使用 Array.from.

将其转换为数组

问题是您随后将 LocalVideoTrackPublication 的数组传递给 localParticipant.unpublishTracks(tracks);,这会导致错误,因为 unpublishTracks 需要 LocalTrack 的数组而不是 LocalVideoTrackPublications.

您可以通过映射出版物并返回 track 属性:

来解决这个问题
const tracks = Array.from(localParticipant.videoTracks.values())
                 .map(publication => publication.track);

如果有帮助请告诉我。