From 21592b196766ae289485a8bcaec3cbe03ce34847 Mon Sep 17 00:00:00 2001 From: yj <1336058017@qq.com> Date: Thu, 25 Jul 2024 14:17:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/StupWizard/index.module.scss | 8 ++++ src/components/StupWizard/index.tsx | 44 ++++++++++++++++++--- src/utils/package/agora.ts | 21 ++++++++-- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/components/StupWizard/index.module.scss b/src/components/StupWizard/index.module.scss index b40247d..1588780 100644 --- a/src/components/StupWizard/index.module.scss +++ b/src/components/StupWizard/index.module.scss @@ -40,6 +40,7 @@ .stupWizardRight { flex-grow: 1; height: 100%; + position: relative; >div { display: flex; @@ -58,6 +59,7 @@ >div { flex-grow: 1; overflow-y: auto; + overflow-x: hidden; padding: 20px; box-sizing: border-box; } @@ -71,10 +73,16 @@ height: 296px; border-radius: 10px; overflow: hidden; + border: 1px #292E33 solid; + position: relative; >video { width: 100%; height: 100%; + z-index: 1; + position: absolute; + left: 0; + top: 0; } } diff --git a/src/components/StupWizard/index.tsx b/src/components/StupWizard/index.tsx index 1a177d3..c449e0d 100644 --- a/src/components/StupWizard/index.tsx +++ b/src/components/StupWizard/index.tsx @@ -3,6 +3,7 @@ import ImageUrl from '@/utils/package/ImageUrl'; import { Button, Empty, message, Modal, Select, Slider } from 'antd'; import { useState, useImperativeHandle, forwardRef, useEffect } from "react"; import agora from '@/utils/package/agora' +import { CloseOutlined, LoadingOutlined } from '@ant-design/icons'; const StupWizard = forwardRef((props: any, ref: any) => { useImperativeHandle(ref, () => ({ changeModal: () => { @@ -66,6 +67,7 @@ const StupWizard = forwardRef((props: any, ref: any) => { open={isStupWizard} footer={null} closable={false} + destroyOnClose={true} centered width={'70vw'} className='modal-padding'> @@ -73,10 +75,13 @@ const StupWizard = forwardRef((props: any, ref: any) => {
{list.map((row: any, index: number) => { return ( -
{ +
{ const newList = [...list] newList.forEach(item => item.active = false) newList[index].active = true; + if (newList[index].title !== '视频') { + await agora.stopVideoDevice('videoPreview') + } setList(newList) }}> @@ -86,10 +91,22 @@ const StupWizard = forwardRef((props: any, ref: any) => { })}
+ { + agora.stopVideoDevice('videoPreview') + setIsStupWizard(false) + }} + /> {list[0].active ? : null} {list[1].active ? : null} {list[2].active ? : null} - {list[3].active ? : null} + {list[3].active ? : null}
@@ -106,7 +123,7 @@ const VideoComponents = () => { getVideoDeviceList() }, []); const getVideoDeviceList = (): void => { - agora.getVideoDeviceManager().then(res => { + agora.getVideoDeviceManager().then(async (res) => { const { item, list } = res setVideoDeviceManager({ list: list.map((row: any) => { @@ -117,7 +134,10 @@ const VideoComponents = () => { }), item: item ? item : null, }) - agora.startPreview('videoPreview') + if (item) { + await agora.stopVideoDevice('videoPreview') + agora.startPreview('videoPreview', item) + } }) } return ( @@ -128,7 +148,14 @@ const VideoComponents = () => { { videoDeviceManager.item ?
- + +
:
} @@ -138,7 +165,12 @@ const VideoComponents = () => { placeholder={videoDeviceManager.list.length ? '请选择设备' : '未检测到摄像头'} options={videoDeviceManager.list} style={{ flexGrow: 1 }} value={videoDeviceManager.item} onChange={(e) => { - + setVideoDeviceManager({ + ...videoDeviceManager, + item: e + }) + agora.setVideoDeviceManager(e) + agora.startPreview('videoPreview', e) }} />;
diff --git a/src/utils/package/agora.ts b/src/utils/package/agora.ts index cc6fa26..65faefc 100644 --- a/src/utils/package/agora.ts +++ b/src/utils/package/agora.ts @@ -270,11 +270,17 @@ const agora = { item: rtcEngine.getVideoDeviceManager().getDevice() } }, + // 通过设备 ID 指定视频采集设备。 + setVideoDeviceManager: (deviceIdUTF8: string) => { + rtcEngine.getVideoDeviceManager().setDevice(deviceIdUTF8) + }, // 开启本地视频预览 - startPreview: async (id: string): Promise => { + startPreview: async (id: string, deviceId: string): Promise => { return new Promise((resolve, reject) => { navigator.mediaDevices.getUserMedia({ - video: true, + video: { + deviceId: { exact: deviceId }, // 指定设备ID + }, audio: true, }).then((stream) => { let dom = document.getElementById(id) as any; @@ -288,7 +294,16 @@ const agora = { }); }) }, - + stopVideoDevice: async (id: string) => { + let dom = document.getElementById(id) as any; + if (dom && dom.srcObject) { + const tracks = dom.srcObject.getTracks(); + tracks.forEach((track: any) => { + track.stop(); + }); + dom.srcObject = null; + } + },