This commit is contained in:
梅航 2024-07-25 14:24:01 +08:00
commit 7b46e3ec79
3 changed files with 64 additions and 9 deletions

View File

@ -40,6 +40,7 @@
.stupWizardRight { .stupWizardRight {
flex-grow: 1; flex-grow: 1;
height: 100%; height: 100%;
position: relative;
>div { >div {
display: flex; display: flex;
@ -58,6 +59,7 @@
>div { >div {
flex-grow: 1; flex-grow: 1;
overflow-y: auto; overflow-y: auto;
overflow-x: hidden;
padding: 20px; padding: 20px;
box-sizing: border-box; box-sizing: border-box;
} }
@ -71,10 +73,16 @@
height: 296px; height: 296px;
border-radius: 10px; border-radius: 10px;
overflow: hidden; overflow: hidden;
border: 1px #292E33 solid;
position: relative;
>video { >video {
width: 100%; width: 100%;
height: 100%; height: 100%;
z-index: 1;
position: absolute;
left: 0;
top: 0;
} }
} }

View File

@ -3,6 +3,7 @@ import ImageUrl from '@/utils/package/ImageUrl';
import { Button, Empty, message, Modal, Select, Slider } from 'antd'; import { Button, Empty, message, Modal, Select, Slider } from 'antd';
import { useState, useImperativeHandle, forwardRef, useEffect } from "react"; import { useState, useImperativeHandle, forwardRef, useEffect } from "react";
import agora from '@/utils/package/agora' import agora from '@/utils/package/agora'
import { CloseOutlined, LoadingOutlined } from '@ant-design/icons';
const StupWizard = forwardRef((props: any, ref: any) => { const StupWizard = forwardRef((props: any, ref: any) => {
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
changeModal: () => { changeModal: () => {
@ -66,6 +67,7 @@ const StupWizard = forwardRef((props: any, ref: any) => {
open={isStupWizard} open={isStupWizard}
footer={null} footer={null}
closable={false} closable={false}
destroyOnClose={true}
centered centered
width={'70vw'} width={'70vw'}
className='modal-padding'> className='modal-padding'>
@ -73,10 +75,13 @@ const StupWizard = forwardRef((props: any, ref: any) => {
<div className={styles.stupWizardLeft}> <div className={styles.stupWizardLeft}>
{list.map((row: any, index: number) => { {list.map((row: any, index: number) => {
return ( return (
<div key={index} className={`${row.active ? styles.active : ''}`} onClick={() => { <div key={index} className={`${row.active ? styles.active : ''}`} onClick={async () => {
const newList = [...list] const newList = [...list]
newList.forEach(item => item.active = false) newList.forEach(item => item.active = false)
newList[index].active = true; newList[index].active = true;
if (newList[index].title !== '视频') {
await agora.stopVideoDevice('videoPreview')
}
setList(newList) setList(newList)
}}> }}>
<img src={row.active ? row.iconActive : row.icon} alt="" /> <img src={row.active ? row.iconActive : row.icon} alt="" />
@ -86,10 +91,22 @@ const StupWizard = forwardRef((props: any, ref: any) => {
})} })}
</div> </div>
<div className={styles.stupWizardRight}> <div className={styles.stupWizardRight}>
<CloseOutlined style={{
position: 'absolute',
color: 'white',
right: '20px',
top: '16px',
cursor: 'pointer'
}}
onClick={() => {
agora.stopVideoDevice('videoPreview')
setIsStupWizard(false)
}}
/>
{list[0].active ? <VideoComponents /> : null} {list[0].active ? <VideoComponents /> : null}
{list[1].active ? <AudioComponents /> : null} {list[1].active ? <AudioComponents /> : null}
{list[2].active ? <RecordingComponents /> : null} {list[2].active ? <RecordingComponents /> : null}
{list[3].active ? <RecordingComponents /> : null} {list[3].active ? <FileComponents /> : null}
</div> </div>
</div> </div>
</Modal> </Modal>
@ -106,7 +123,7 @@ const VideoComponents = () => {
getVideoDeviceList() getVideoDeviceList()
}, []); }, []);
const getVideoDeviceList = (): void => { const getVideoDeviceList = (): void => {
agora.getVideoDeviceManager().then(res => { agora.getVideoDeviceManager().then(async (res) => {
const { item, list } = res const { item, list } = res
setVideoDeviceManager({ setVideoDeviceManager({
list: list.map((row: any) => { list: list.map((row: any) => {
@ -117,7 +134,10 @@ const VideoComponents = () => {
}), }),
item: item ? item : null, item: item ? item : null,
}) })
agora.startPreview('videoPreview') if (item) {
await agora.stopVideoDevice('videoPreview')
agora.startPreview('videoPreview', item)
}
}) })
} }
return ( return (
@ -128,7 +148,14 @@ const VideoComponents = () => {
{ {
videoDeviceManager.item ? videoDeviceManager.item ?
<div> <div>
<video id='videoPreview' controls></video> <video id='videoPreview'></video>
<LoadingOutlined style={{
position: 'absolute',
color: 'white',
right: '50%',
fontSize: '30px',
top: '50%',
}} />
</div> : </div> :
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}><Empty description={'未检测到摄像头'} /></div> <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}><Empty description={'未检测到摄像头'} /></div>
} }
@ -138,7 +165,12 @@ const VideoComponents = () => {
placeholder={videoDeviceManager.list.length ? '请选择设备' : '未检测到摄像头'} placeholder={videoDeviceManager.list.length ? '请选择设备' : '未检测到摄像头'}
options={videoDeviceManager.list} style={{ flexGrow: 1 }} options={videoDeviceManager.list} style={{ flexGrow: 1 }}
value={videoDeviceManager.item} onChange={(e) => { value={videoDeviceManager.item} onChange={(e) => {
setVideoDeviceManager({
...videoDeviceManager,
item: e
})
agora.setVideoDeviceManager(e)
agora.startPreview('videoPreview', e)
}} />; }} />;
</div> </div>
</div> </div>

View File

@ -270,11 +270,17 @@ const agora = {
item: rtcEngine.getVideoDeviceManager().getDevice() item: rtcEngine.getVideoDeviceManager().getDevice()
} }
}, },
// 通过设备 ID 指定视频采集设备。
setVideoDeviceManager: (deviceIdUTF8: string) => {
rtcEngine.getVideoDeviceManager().setDevice(deviceIdUTF8)
},
// 开启本地视频预览 // 开启本地视频预览
startPreview: async (id: string): Promise<boolean> => { startPreview: async (id: string, deviceId: string): Promise<boolean> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
navigator.mediaDevices.getUserMedia({ navigator.mediaDevices.getUserMedia({
video: true, video: {
deviceId: { exact: deviceId }, // 指定设备ID
},
audio: true, audio: true,
}).then((stream) => { }).then((stream) => {
let dom = document.getElementById(id) as any; 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;
}
},