videos.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div>
  3. <div class="box">
  4. <ul class="box_ul">
  5. <li
  6. v-for="(item, index) in videos"
  7. :key="index"
  8. @click="changeSource(item.src, index)"
  9. :id="active == index ? 'addclass' : ''"
  10. >
  11. {{ item.name }}
  12. </li>
  13. </ul>
  14. <video id="videoPlayer" class="video-js" crossorigin="anonymous" EXT-X-TARGETDURATION="10">
  15. <!-- <source
  16. id="source"
  17. type="application/x-mpegURL"
  18. > -->
  19. </video>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. var getTime = function () {
  25. var date = new Date();
  26. var year = date.getFullYear();
  27. var month =
  28. date.getMonth() + 1 < 10
  29. ? "0" + (date.getMonth() + 1)
  30. : date.getMonth() + 1;
  31. var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  32. return year + month + day;
  33. };
  34. import Videojs from "video.js";
  35. export default {
  36. data() {
  37. return {
  38. nowPlayVideoUrl: `http://video.nxjiewei.com:8011/jinjiaqu/中部北门值班室俯视北/${getTime()}/out.m3u8`,
  39. options: {
  40. autoplay: true, // 设置自动播放
  41. muted: true, // 设置了它为true,才可实现自动播放,同时视频也被静音 (Chrome66及以上版本,禁止音视频的自动播放)
  42. preload: "auto", // 预加载
  43. controls: true, // 显示播放的控件
  44. playbackRates: [0.5, 1, 1.25, 1.5, 2, 3], // 加上这个属性,可实现倍速播放
  45. },
  46. player: null,
  47. active: 0,
  48. systemTime: "",
  49. videos: [
  50. {
  51. name: "中部北门值班室俯视北",
  52. src: `http://video.nxjiewei.com:8011/jinjiaqu/中部北门值班室俯视北/${getTime()}/out.m3u8`,
  53. },
  54. {
  55. name: "中部北大门值班室俯视南",
  56. src: `http://video.nxjiewei.com:8011/jinjiaqu/中部北大门值班室俯视南/${getTime()}/out.m3u8`,
  57. },
  58. {
  59. name: "炸药路线-副立井东侧",
  60. src: `http://video.nxjiewei.com:8011/jinjiaqu/炸药路线-副立井东侧/${getTime()}/out.m3u8`,
  61. },
  62. {
  63. name: "炸药路线-副立井东侧门",
  64. src: `http://video.nxjiewei.com:8011/jinjiaqu/炸药路线-副立井东侧门/${getTime()}/out.m3u8`,
  65. },
  66. {
  67. name: "炸药路线-副立井东门外",
  68. src: `http://video.nxjiewei.com:8011/jinjiaqu/炸药路线-副立井东门外/${getTime()}/out.m3u8`,
  69. },
  70. {
  71. name: "副井入口-副立井东侧",
  72. src: `http://video.nxjiewei.com:8011/jinjiaqu/"副井入口-副立井东侧/${getTime()}/out.m3u8`,
  73. },
  74. {
  75. name: "马路北头立杆-朝西",
  76. src: `http://video.nxjiewei.com:8011/jinjiaqu/马路北头立杆-朝西/${getTime()}/out.m3u8`,
  77. },
  78. {
  79. name: "马路北头立杆-朝南",
  80. src: `http://video.nxjiewei.com:8011/jinjiaqu/马路北头立杆-朝南/${getTime()}/out.m3u8`,
  81. },
  82. {
  83. name: "马路北头立杆-朝东",
  84. src: `http://video.nxjiewei.com:8011/jinjiaqu/马路北头立杆-朝东/${getTime()}/out.m3u8`,
  85. },
  86. {
  87. name: "马路北头立杆-朝北",
  88. src: `http://video.nxjiewei.com:8011/jinjiaqu/马路北头立杆-朝北/${getTime()}/out.m3u8`,
  89. },
  90. ],
  91. vs: [{ name: "111", src: `http://1231231.com/${getTime()}` }],
  92. };
  93. },
  94. mounted() {
  95. this.getVideo(this.nowPlayVideoUrl);
  96. this.addDate();
  97. console.log(this.vs);
  98. },
  99. methods: {
  100. getVideo(nowPlayVideoUrl) {
  101. this.player = Videojs("videoPlayer", this.options);
  102. //关键代码, 动态设置src,才可实现换台操作
  103. this.player.src([
  104. {
  105. src: nowPlayVideoUrl,
  106. type: "application/x-mpegURL", // 告诉videojs,这是一个hls流
  107. },
  108. ]);
  109. },
  110. changeSource(src, index) {
  111. this.nowPlayVideoUrl = src;
  112. this.active = index;
  113. },
  114. addDate() {
  115. let nowDate = new Date();
  116. let date = {
  117. year: nowDate.getFullYear(),
  118. month:
  119. nowDate.getMonth() + 1 < 10
  120. ? "0" + (nowDate.getMonth() + 1)
  121. : nowDate.getMonth() + 1,
  122. date:
  123. nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate(),
  124. };
  125. this.systemTime = date.year + "-" + date.month + "-" + date.date;
  126. },
  127. },
  128. watch: {
  129. nowPlayVideoUrl(newVal, old) {
  130. this.getVideo(newVal);
  131. },
  132. },
  133. beforeDestroy() {
  134. if (this.player) {
  135. this.player.dispose(); // Removing Players,该方法会重置videojs的内部状态并移除dom
  136. }
  137. },
  138. };
  139. </script>
  140. <style scoped>
  141. #videoPlayer {
  142. width: 800px;
  143. height: 600px;
  144. margin: 50px auto;
  145. }
  146. .box {
  147. display: flex;
  148. justify-content: left;
  149. }
  150. .box_ul {
  151. width: 300px;
  152. height: 100vh;
  153. background: #39f;
  154. overflow: auto;
  155. }
  156. .box_ul > li {
  157. width: 80%;
  158. height: 40px;
  159. text-align: center;
  160. border: 1px solid;
  161. margin: 17px auto;
  162. line-height: 40px;
  163. border-radius: 20px;
  164. background: #fff;
  165. }
  166. #addclass {
  167. background: lightcoral;
  168. }
  169. </style>