From 6eeba373e475f6f608b81b3a7299b6e445a17aee Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Wed, 18 Jun 2025 17:03:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=AE=BE=E5=A4=87=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A7=86=E9=A2=91=E6=B5=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/pages/device-ops/monitor/index.tsx | 372 ++++++++++++++---- 1 file changed, 301 insertions(+), 71 deletions(-) diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx index 61a32194..f4f39aeb 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx @@ -18,6 +18,7 @@ import { import { device, getStreamUrls } from '@/services/device/api'; import { request } from 'umi'; import { Amap } from '@amap/amap-react'; +import ReactPlayer from 'react-player'; const { RangePicker } = DatePicker; const { TabPane } = Tabs; @@ -424,10 +425,14 @@ const DeviceMonitor: React.FC = () => { const [videoFullscreen, setVideoFullscreen] = useState(false); const [videoQuality, setVideoQuality] = useState('auto'); const [allStreamUrls, setAllStreamUrls] = useState<{ + pushUrl: string; playFlvUrl: string; playM3u8Url: string; playRtmpUrl: string; } | null>(null); + const [currentStreamType, setCurrentStreamType] = useState<'hls' | 'flv' | 'rtmp'>('hls'); + const [useReactPlayer, setUseReactPlayer] = useState(true); + const [videoStreamStatus, setVideoStreamStatus] = useState<'loading' | 'playing' | 'error' | 'ended' | 'stalled'>('loading'); const tokenCache = { value: '', @@ -1937,24 +1942,28 @@ const DeviceMonitor: React.FC = () => { console.log('视频流API响应:', response); - if (response && response.data) { - const { playFlvUrl, playM3u8Url, playRtmpUrl } = response.data; - console.log('获取到视频流地址:', { playFlvUrl, playM3u8Url, playRtmpUrl }); + if (response && response.resp_code === 0 && response.data) { + const { pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl } = response.data; + console.log('获取到视频流地址:', { pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl }); // 保存所有流URL - setAllStreamUrls({ playFlvUrl, playM3u8Url, playRtmpUrl }); + setAllStreamUrls({ pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl }); // 优先使用HLS流,然后是FLV,最后是RTMP const streamUrl = playM3u8Url || playFlvUrl || playRtmpUrl; if (streamUrl) { + console.log('选择的流URL:', streamUrl); + console.log('流类型:', streamUrl.includes('.m3u8') ? 'HLS' : + streamUrl.includes('.flv') ? 'FLV' : + streamUrl.startsWith('rtmp://') ? 'RTMP' : '未知'); setVideoStreamUrl(streamUrl); return streamUrl; } else { throw new Error('未找到可用的视频流地址'); } } else { - throw new Error('获取视频流地址失败'); + throw new Error(String(response?.resp_msg || '获取视频流地址失败')); } } catch (error) { console.error('获取视频流地址失败:', error); @@ -1976,6 +1985,8 @@ const DeviceMonitor: React.FC = () => { setVideoStreamVisible(true); setVideoError(''); setVideoStreamUrl(''); + setAllStreamUrls(null); + setCurrentStreamType('hls'); // 获取视频流地址 const streamUrl = await getVideoStreamUrl(device.deviceSn); @@ -1994,6 +2005,10 @@ const DeviceMonitor: React.FC = () => { setVideoStreamUrl(''); setVideoError(''); setVideoLoading(false); + setAllStreamUrls(null); + setCurrentStreamType('hls'); + setUseReactPlayer(true); // 重置为ReactPlayer + setVideoStreamStatus('loading'); // 重置状态 }; // 重新加载视频流 @@ -2001,6 +2016,8 @@ const DeviceMonitor: React.FC = () => { if (currentVideoDevice) { setVideoError(''); setVideoStreamUrl(''); + setAllStreamUrls(null); + setCurrentStreamType('hls'); const streamUrl = await getVideoStreamUrl(currentVideoDevice.deviceSn); if (streamUrl) { setVideoStreamUrl(streamUrl); @@ -2009,6 +2026,31 @@ const DeviceMonitor: React.FC = () => { } }; + // 切换流格式 + const handleSwitchStreamType = (type: 'hls' | 'flv' | 'rtmp') => { + if (!allStreamUrls) return; + + let newUrl = ''; + switch (type) { + case 'hls': + newUrl = allStreamUrls.playM3u8Url; + break; + case 'flv': + newUrl = allStreamUrls.playFlvUrl; + break; + case 'rtmp': + newUrl = allStreamUrls.playRtmpUrl; + break; + } + + if (newUrl) { + setCurrentStreamType(type); + setVideoStreamUrl(newUrl); + setVideoPlayerKey(prev => prev + 1); + console.log(`切换到${type}格式:`, newUrl); + } + }; + // 截图功能 const handleScreenshot = () => { const video = document.querySelector('video'); @@ -2063,35 +2105,13 @@ const DeviceMonitor: React.FC = () => { const renderVideoPlayer = () => { if (!videoStreamUrl) return null; - // 判断视频流类型 - const isHLS = videoStreamUrl.includes('.m3u8') || videoStreamUrl.includes('application/x-mpegURL'); - const isRTMP = videoStreamUrl.startsWith('rtmp://'); - const isWebRTC = videoStreamUrl.startsWith('webrtc://'); + console.log('渲染视频播放器,URL:', videoStreamUrl); + console.log('当前流类型:', currentStreamType); + console.log('ReactPlayer组件:', ReactPlayer); - if (isHLS) { - // HLS流播放器 - return ( - - ); - } else if (isRTMP) { - // RTMP流播放器(需要Flash或特殊播放器) + // 检查ReactPlayer是否可用 + if (!ReactPlayer) { + console.error('ReactPlayer组件未加载'); return (