VideoWindow2.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import Videojs from 'video.js'; //video.js7.0以后版本默认支持hls(m3u8)格式
  4. import "video.js/dist/video-js.css"; // 引入video.js的css
  5. // video 6.0及以上版本,需要引入videojs-flash库(5.0及以下版本,flash在其核心库中,高版本分离了flash)
  6. // rtmp流的视频格式为flv,只有flash能播放
  7. import "videojs-flash"; // 播放rtmp流需要的插件,flash插件。
  8. import "videojs-flvjs-es6"; //flv格式
  9. import {
  10. getGuid,
  11. } from "../common/common.js";
  12. import '../../Assets/styles/PopupWindow/VideoWindow2.css';
  13. /**
  14. * 视频窗口
  15. */
  16. class VideoWindow2 {
  17. /**
  18. * 初始化
  19. * @author joy/创睿
  20. * @param {Object} viewer 三维场景viewer对象
  21. * @param {Cesium.Cartesian3} position 坐标位置
  22. * @param {Object} videoInfo 属性
  23. * @param {String} videoInfo.type 监控类型 hls 、 rtmp 、flv、mp4
  24. * @param {String} videoInfo.name 监控名称
  25. * @param {String} videoInfo.url 监控路径
  26. */
  27. constructor(viewer, position, videoInfo) {
  28. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  29. if (!position) throw new Cesium.DeveloperError('no position object!');
  30. this.viewer = viewer; //弹窗创建的viewer
  31. this.position = position; //弹窗挂载的位置
  32. //弹窗挂载的位置
  33. if (position instanceof Cesium.Cartesian3) {
  34. this.position = position;
  35. } else {
  36. this.position = Cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0);
  37. }
  38. //如果有div就移除
  39. if (document.getElementsByClassName("popup-video2").length > 0) {
  40. document.getElementsByClassName("popup-video2")[0].remove();
  41. }
  42. this.id = "popup_video_" + getGuid();
  43. this.popupDiv = document.createElement("div");
  44. this.popupDiv.classList.add("popup-video2");
  45. this.popupDiv.id = this.id;
  46. // 将字符串模板生成的内容添加到DOM上
  47. this.viewer.container.append(this.popupDiv);
  48. // this.viewer.cesiumWidget.container.appendChild(this.popupDiv);
  49. //创建Html
  50. this.popupDiv.innerHTML = this._createHtml(videoInfo);
  51. switch (videoInfo.type) {
  52. case "hls":
  53. this.videotype = "application/x-mpegURL"; // 告诉videojs,这是一个hls流
  54. break;
  55. case "rtmp":
  56. this.videotype = "rtmp/flv"; // 告诉videojs这是一个rtmp流视频
  57. // 设置flash路径,用于在videojs发现浏览器不支持HTML5播放器的时候自动唤起flash播放器
  58. // Videojs.options.flash.swf = 'https://cdn.bootcss.com/videojs-swf/5.4.1/video-js.swf';
  59. break;
  60. case "flv":
  61. this.videotype = "video/x-flv"; // 告诉videojs这是一个flv
  62. break;
  63. case "mp4":
  64. this.videotype = "video/mp4"; // 告诉videojs这是一个mp4,普通视频
  65. break;
  66. }
  67. this.videoPlayer = Videojs(document.querySelector('#myvideo'), {
  68. controls: true, //开启交互,即是用户可控。用户可以与之交互的控件
  69. autoplay: 'muted', //自动播放
  70. loop: true, //视频一结束就重新开始
  71. muted: false, //开启视频时是否静音,默认情况下将使所有音频静音
  72. fluid: true, //根据外层css样式大小,自动填充宽高!比较实用,可搭配响应式
  73. aspectRatio: "16:9", //显示比率
  74. reload: "auto", //重载
  75. fullscreen: {
  76. options: {
  77. navigationUI: 'hide'
  78. }
  79. },
  80. sources: [{ //注意,如果是以option方式设置的src,是不能实现 换台的 (即使监听了nowPlayVideoUrl也没实现)
  81. src: videoInfo.url,
  82. type: this.videotype,
  83. }],
  84. }, function onPlayerReady() {
  85. console.log('onPlayerReady', this);
  86. })
  87. //添加场景事件,实时更新窗口的位置
  88. this.viewer.scene.postRender.addEventListener(this.postRender, this);
  89. //绑定关闭事件
  90. document.getElementsByClassName("popup-video2-close-button")[0].onclick = () => {
  91. this.close();
  92. };
  93. }
  94. /**
  95. * 场景渲染事件 实时更新窗口的位置 使其与笛卡尔坐标一致
  96. * @ignore
  97. */
  98. postRender() {
  99. const canvasHeight = this.viewer.scene.canvas.height;
  100. const windowPosition = new Cesium.Cartesian2();
  101. Cesium.SceneTransforms.wgs84ToWindowCoordinates(
  102. this.viewer.scene,
  103. this.position,
  104. windowPosition
  105. );
  106. this.popupDiv.style.left = windowPosition.x - this.popupDiv.offsetWidth - 120 + "px";
  107. this.popupDiv.style.top = windowPosition.y + "px";
  108. const camerPosition = this.viewer.camera.position;
  109. let height = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(camerPosition).height;
  110. height += this.viewer.scene.globe.ellipsoid.maximumRadius;
  111. if ((!(Cesium.Cartesian3.distance(camerPosition, this.position) > height)) && this.viewer.camera.positionCartographic.height < 50000000) {
  112. this.popupDiv.style.display = "block";
  113. } else {
  114. this.popupDiv.style.display = "none";
  115. }
  116. }
  117. /**
  118. * 生成Html
  119. * @ignore
  120. * @param {Object} videoInfo
  121. */
  122. _createHtml(videoInfo) {
  123. let html = `
  124. <div class="rightLine-1"></div>
  125. <div class="rightLine-2"></div>
  126. <div class="rightLine-3"></div>
  127. <div class="popup-header">
  128. <span class="popup-title" >${videoInfo.name }</span>
  129. <span class="popup-video2-close-button" title="关闭" >×</span>
  130. </div>
  131. <div class="popup-content">
  132. <video id="myvideo" class="video-js vjs-big-play-centered vjs-fluid" controls preload="auto" width="100%" height="100%"></video>
  133. </div>
  134. `
  135. return html;
  136. }
  137. }
  138. /**
  139. * 通用对外公开函数
  140. */
  141. Object.assign(VideoWindow2.prototype, /** @lends VideoWindow2.prototype */ {
  142. /**
  143. * 关闭按钮
  144. */
  145. close() {
  146. this.popupDiv.remove();
  147. this.viewer.scene.postRender.removeEventListener(this.postRender, this);
  148. }
  149. });
  150. export default VideoWindow2;