|
| 1 | +import React, { useCallback, useRef } from "react"; |
| 2 | +import EZUIKit from "ezuikit-js"; |
| 3 | +import { isMobile } from "./utils"; |
| 4 | + |
| 5 | +import "./App.css"; |
| 6 | + |
| 7 | +const Player = () => { |
| 8 | + const player = useRef(); |
| 9 | + const initPlayer = useCallback(() => { |
| 10 | + if (document.getElementById("player-container")) { |
| 11 | + let width = 600; |
| 12 | + let height = 400; |
| 13 | + |
| 14 | + if (isMobile()) { |
| 15 | + width = document.documentElement.clientWidth; |
| 16 | + height = (width * 9) / 16; |
| 17 | + } |
| 18 | + |
| 19 | + player.current = new EZUIKit.EZUIKitPlayer({ |
| 20 | + id: "player-container", |
| 21 | + url: "ezopen://open.ys7.com/C17369415/1.rec", |
| 22 | + accessToken: |
| 23 | + "ra.df4odsvw3qmz6rao4181c5oc8cfjprjl-60u980bhwa-081mbqo-d3xptaquw", |
| 24 | + width, |
| 25 | + height, |
| 26 | + template: "pcRec", |
| 27 | + // isCloudRecord: true, // 如果是云录制的播放 需要这个值, 是必须的 |
| 28 | + }); |
| 29 | + } |
| 30 | + }, []); |
| 31 | + |
| 32 | + /** 播放 */ |
| 33 | + const handlePlay = useCallback(() => { |
| 34 | + if (player.current) { |
| 35 | + player.current.play(); |
| 36 | + } |
| 37 | + }, []); |
| 38 | + |
| 39 | + /** 停止 */ |
| 40 | + const handleStop = useCallback(() => { |
| 41 | + if (player.current) { |
| 42 | + player.current.stop(); |
| 43 | + } |
| 44 | + }, []); |
| 45 | + |
| 46 | + /** 开启声音, 默认 0.8, 暂时不可调节, 如要调节请调节系统音量 */ |
| 47 | + const handleOpenSound = useCallback(() => { |
| 48 | + if (player.current) { |
| 49 | + player.current.openSound(); |
| 50 | + } |
| 51 | + }, []); |
| 52 | + |
| 53 | + const handleCloseSound = useCallback(() => { |
| 54 | + if (player.current) { |
| 55 | + player.current.closeSound(); |
| 56 | + } |
| 57 | + }, []); |
| 58 | + /** 开始录制, 录制需要在播放状态下才可以 */ |
| 59 | + const handleStartSave = useCallback(() => { |
| 60 | + if (player.current) { |
| 61 | + player.current.startSave(); |
| 62 | + } |
| 63 | + }, []); |
| 64 | + |
| 65 | + const handleStopSave = useCallback(() => { |
| 66 | + if (player.current) { |
| 67 | + player.current.stopSave(); |
| 68 | + } |
| 69 | + }, []); |
| 70 | + |
| 71 | + /** 抓图/截图 */ |
| 72 | + const handleCapturePicture = useCallback(() => { |
| 73 | + if (player.current) { |
| 74 | + player.current.capturePicture(); |
| 75 | + } |
| 76 | + }, []); |
| 77 | + |
| 78 | + /** 全屏 */ |
| 79 | + const handleFullscreen = useCallback(() => { |
| 80 | + if (player.current) { |
| 81 | + player.current.fullScreen(); |
| 82 | + } |
| 83 | + }, []); |
| 84 | + |
| 85 | + /** 回去OSD时间 */ |
| 86 | + const handleGetOSDTime = useCallback(() => { |
| 87 | + if (player.current) { |
| 88 | + player.current.getOSDTime(); |
| 89 | + } |
| 90 | + }, []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <div> |
| 94 | + <h2>视频模式使用示例:</h2> |
| 95 | + <div id="player-container"></div> |
| 96 | + <div> |
| 97 | + <button onClick={initPlayer}>初始化</button> |
| 98 | + <button onClick={handleStop}>stop</button> |
| 99 | + <button onClick={handlePlay}>play</button> |
| 100 | + <button onClick={handleOpenSound}>openSound</button> |
| 101 | + <button onClick={handleCloseSound}>closeSound</button> |
| 102 | + <button onClick={handleStartSave}>startSave</button> |
| 103 | + <button onClick={handleStopSave}>stopSave</button> |
| 104 | + <button onClick={handleCapturePicture}>capturePicture</button> |
| 105 | + <button onClick={handleFullscreen}>fullScreen</button> |
| 106 | + <button onClick={handleGetOSDTime}>getOSDTime</button> |
| 107 | + {/*<button onClick={() => this.ezopenStartTalk()}>开始对讲</button> |
| 108 | + <button onClick={() => this.ezopenStopTalk()}>结束对讲</button> */} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +class App extends React.Component { |
| 115 | + constructor(props) { |
| 116 | + super(props); |
| 117 | + this.player = null; //定义播放器 |
| 118 | + } |
| 119 | + componentDidMount() { |
| 120 | + // fetch('https://open.ys7.com/jssdk/ezopen/demo/token') |
| 121 | + // .then(response => response.json()) |
| 122 | + // .then(res => { |
| 123 | + // var accessToken = res.data.accessToken; |
| 124 | + |
| 125 | + if (window.player) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + this.player = new EZUIKit.EZUIKitPlayer({ |
| 130 | + id: "video-container", // 视频容器ID |
| 131 | + accessToken: |
| 132 | + "at.0siysnsad14jkcgmbnp2pbop427gcbx6-8l00xx7oa9-193qkwi-ryfn1m0j", |
| 133 | + url: "ezopen://open.ys7.com/BC7900686/1.hd.live", |
| 134 | + // simple:极简版; pcLive: pc直播; pcRec: pc回放; mobileLive: 移动端直播; mobileRec: 移动端回放; security: 安防版; voice: 语音版; |
| 135 | + template: "pcLive", |
| 136 | + plugin: ["talk"], // 加载插件,talk-对讲 |
| 137 | + width: 600, |
| 138 | + height: 400, |
| 139 | + handleFirstFrameDisplay: (res) => { |
| 140 | + if (window.player.jSPlugin) { |
| 141 | + console.log( |
| 142 | + "-------------------res", |
| 143 | + window.player.jSPlugin.player.getFrameInfo() |
| 144 | + ); |
| 145 | + } |
| 146 | + }, |
| 147 | + handleError: (error) => { |
| 148 | + console.log("handleError", error); |
| 149 | + }, |
| 150 | + // language: "en", // zh | en |
| 151 | + // staticPath: "/ezuikit_static", // 如果想使用本地静态资源,请复制根目录下ezuikit_static 到当前目录下, 然后设置该值 |
| 152 | + }); |
| 153 | + // }); |
| 154 | + window.player = this.player; |
| 155 | + |
| 156 | + // console.log(window.player.jSPlugin); |
| 157 | + |
| 158 | + // if (window.player.jSPlugin) { |
| 159 | + // window.player.jSPlugin.player.event.on("videoInfo", (res) => { |
| 160 | + // console.log(res); |
| 161 | + // }); |
| 162 | + // } |
| 163 | + } |
| 164 | + play = () => { |
| 165 | + var playPromise = this.player.play(); |
| 166 | + playPromise.then((data) => { |
| 167 | + console.log("promise 获取 数据", data); |
| 168 | + }); |
| 169 | + }; |
| 170 | + stop = () => { |
| 171 | + var stopPromise = this.player.stop(); |
| 172 | + stopPromise.then((data) => { |
| 173 | + console.log("promise 获取 数据", data); |
| 174 | + }); |
| 175 | + }; |
| 176 | + getOSDTime = () => { |
| 177 | + var getOSDTimePromise = this.player.getOSDTime(); |
| 178 | + getOSDTimePromise.then((data) => { |
| 179 | + console.log("promise 获取 数据", data); |
| 180 | + }); |
| 181 | + }; |
| 182 | + capturePicture = () => { |
| 183 | + var capturePicturePromise = this.player.capturePicture( |
| 184 | + `${new Date().getTime()}` |
| 185 | + ); |
| 186 | + capturePicturePromise.then((data) => { |
| 187 | + console.log("promise 获取 数据", data); |
| 188 | + }); |
| 189 | + }; |
| 190 | + openSound = () => { |
| 191 | + var openSoundPromise = this.player.openSound(); |
| 192 | + openSoundPromise.then((data) => { |
| 193 | + console.log("promise 获取 数据", data); |
| 194 | + }); |
| 195 | + }; |
| 196 | + closeSound = () => { |
| 197 | + var openSoundPromise = this.player.closeSound(); |
| 198 | + openSoundPromise.then((data) => { |
| 199 | + console.log("promise 获取 数据", data); |
| 200 | + }); |
| 201 | + }; |
| 202 | + startSave = () => { |
| 203 | + var startSavePromise = this.player.startSave(`${new Date().getTime()}`); |
| 204 | + startSavePromise.then((data) => { |
| 205 | + console.log("promise 获取 数据", data); |
| 206 | + }); |
| 207 | + }; |
| 208 | + stopSave = () => { |
| 209 | + var stopSavePromise = this.player.stopSave(); |
| 210 | + stopSavePromise.then((data) => { |
| 211 | + console.log("promise 获取 数据", data); |
| 212 | + }); |
| 213 | + }; |
| 214 | + ezopenStartTalk = () => { |
| 215 | + this.player.startTalk(); |
| 216 | + }; |
| 217 | + ezopenStopTalk = () => { |
| 218 | + this.player.stopTalk(); |
| 219 | + }; |
| 220 | + fullScreen = () => { |
| 221 | + this.player.fullScreen(); |
| 222 | + }; |
| 223 | + render() { |
| 224 | + return ( |
| 225 | + <div className="demo"> |
| 226 | + <div id="video-container" style={{ width: 600, height: 600 }}></div> |
| 227 | + </div> |
| 228 | + ); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +export default Player; |
0 commit comments