jessibuca.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <div ref="container" @dblclick="fullscreenSwich"
  3. style="width:100%;height:100%;background-color: #000000;margin:0 auto;">
  4. <div class="buttons-box" id="buttonsBox">
  5. <div class="buttons-box-left">
  6. <i v-if="!playing" class="iconfont icon-play jessibuca-btn" @click="playBtnClick"></i>
  7. <i v-if="playing" class="iconfont icon-pause jessibuca-btn" @click="pause"></i>
  8. <i class="iconfont icon-stop jessibuca-btn" @click="destroy"></i>
  9. <i v-if="isNotMute" class="iconfont icon-audio-high jessibuca-btn" @click="mute()"></i>
  10. <i v-if="!isNotMute" class="iconfont icon-audio-mute jessibuca-btn" @click="cancelMute()"></i>
  11. </div>
  12. <div class="buttons-box-right">
  13. <span class="jessibuca-btn">{{ kBps }} kb/s</span>
  14. <!-- <i class="iconfont icon-file-record1 jessibuca-btn"></i>-->
  15. <!-- <i class="iconfont icon-xiangqing2 jessibuca-btn" ></i>-->
  16. <i class="iconfont icon-camera1196054easyiconnet jessibuca-btn" @click="screenshot"
  17. style="font-size: 1rem !important"></i>
  18. <i class="iconfont icon-shuaxin11 jessibuca-btn" @click="playBtnClick"></i>
  19. <i v-if="!fullscreen" class="iconfont icon-weibiaoti10 jessibuca-btn" @click="fullscreenSwich"></i>
  20. <i v-if="fullscreen" class="iconfont icon-weibiaoti11 jessibuca-btn" @click="fullscreenSwich"></i>
  21. </div>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. let jessibucaPlayer = {};
  27. export default {
  28. name: 'jessibuca',
  29. data() {
  30. return {
  31. playing: false,
  32. isNotMute: false,
  33. quieting: false,
  34. fullscreen: false,
  35. loaded: false, // mute
  36. speed: 0,
  37. performance: "", // 工作情况
  38. kBps: 0,
  39. btnDom: null,
  40. videoInfo: null,
  41. volume: 1,
  42. rotate: 0,
  43. vod: true, // 点播
  44. forceNoOffscreen: false,
  45. };
  46. },
  47. props: ['videoUrl', 'error', 'hasAudio', 'height'],
  48. mounted() {
  49. window.onerror = (msg) => {
  50. // console.error(msg)
  51. };
  52. console.log(this._uid)
  53. let paramUrl = decodeURIComponent(this.$route.params.url)
  54. this.$nextTick(() => {
  55. this.updatePlayerDomSize()
  56. window.onresize = () => {
  57. this.updatePlayerDomSize()
  58. }
  59. if (typeof (this.videoUrl) == "undefined") {
  60. this.videoUrl = paramUrl;
  61. }
  62. this.btnDom = document.getElementById("buttonsBox");
  63. console.log("初始化时的地址为: " + this.videoUrl)
  64. this.play(this.videoUrl)
  65. })
  66. },
  67. watch: {
  68. videoUrl(newData, oldData) {
  69. this.play(newData)
  70. },
  71. immediate: true
  72. },
  73. methods: {
  74. updatePlayerDomSize() {
  75. let dom = this.$refs.container;
  76. let width = dom.parentNode.clientWidth
  77. let height = (9 / 16) * width
  78. const clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
  79. if (height > clientHeight) {
  80. height = clientHeight
  81. width = (16 / 9) * height
  82. }
  83. dom.style.width = width + 'px';
  84. dom.style.height = height + "px";
  85. },
  86. create() {
  87. let options = {};
  88. console.log("hasAudio " + this.hasAudio)
  89. jessibucaPlayer[this._uid] = new window.Jessibuca(Object.assign(
  90. {
  91. container: this.$refs.container,
  92. autoWasm: true,
  93. background: "",
  94. controlAutoHide: false,
  95. debug: false,
  96. decoder: "static/js/jessibuca/decoder.js",
  97. forceNoOffscreen: true,
  98. hasAudio: typeof (this.hasAudio) == "undefined" ? true : this.hasAudio,
  99. hasVideo: true,
  100. heartTimeout: 5,
  101. heartTimeoutReplay: true,
  102. heartTimeoutReplayTimes: 3,
  103. hiddenAutoPause: false,
  104. hotKey: false,
  105. isFlv: false,
  106. isFullResize: false,
  107. isNotMute: this.isNotMute,
  108. isResize: false,
  109. keepScreenOn: false,
  110. loadingText: "请稍等, 视频加载中......",
  111. loadingTimeout: 10,
  112. loadingTimeoutReplay: true,
  113. loadingTimeoutReplayTimes: 3,
  114. openWebglAlignment: false,
  115. operateBtns: {
  116. fullscreen: false,
  117. screenshot: false,
  118. play: false,
  119. audio: false,
  120. record: false
  121. },
  122. recordType: "webm",
  123. rotate: 0,
  124. showBandwidth: false,
  125. supportDblclickFullscreen: false,
  126. timeout: 10,
  127. useMSE: location.hostname !== "localhost" && location.protocol !== "https:",
  128. useOffscreen: false,
  129. useWCS: location.hostname === "localhost" || location.protocol === "https",
  130. useWebFullScreen: false,
  131. videoBuffer: 0,
  132. wasmDecodeAudioSyncVideo: true,
  133. wasmDecodeErrorReplay: true,
  134. wcsUseVideoRender: true
  135. },
  136. options
  137. ));
  138. let jessibuca = jessibucaPlayer[this._uid];
  139. let _this = this;
  140. jessibuca.on("load", function () {
  141. console.log("on load init");
  142. });
  143. jessibuca.on("log", function (msg) {
  144. console.log("on log", msg);
  145. });
  146. jessibuca.on("record", function (msg) {
  147. console.log("on record:", msg);
  148. });
  149. jessibuca.on("pause", function () {
  150. _this.playing = false;
  151. });
  152. jessibuca.on("play", function () {
  153. _this.playing = true;
  154. });
  155. jessibuca.on("fullscreen", function (msg) {
  156. console.log("on fullscreen", msg);
  157. _this.fullscreen = msg
  158. });
  159. jessibuca.on("mute", function (msg) {
  160. console.log("on mute", msg);
  161. _this.isNotMute = !msg;
  162. });
  163. jessibuca.on("audioInfo", function (msg) {
  164. console.log("audioInfo", msg);
  165. });
  166. jessibuca.on("bps", function (bps) {
  167. // console.log('bps', bps);
  168. });
  169. let _ts = 0;
  170. jessibuca.on("timeUpdate", function (ts) {
  171. // console.log('timeUpdate,old,new,timestamp', _ts, ts, ts - _ts);
  172. _ts = ts;
  173. });
  174. jessibuca.on("videoInfo", function (info) {
  175. console.log("videoInfo", info);
  176. });
  177. jessibuca.on("error", function (error) {
  178. console.log("error", error);
  179. });
  180. jessibuca.on("timeout", function () {
  181. console.log("timeout");
  182. });
  183. jessibuca.on('start', function () {
  184. console.log('start');
  185. })
  186. jessibuca.on("performance", function (performance) {
  187. let show = "卡顿";
  188. if (performance === 2) {
  189. show = "非常流畅";
  190. } else if (performance === 1) {
  191. show = "流畅";
  192. }
  193. _this.performance = show;
  194. });
  195. jessibuca.on('buffer', function (buffer) {
  196. // console.log('buffer', buffer);
  197. })
  198. jessibuca.on('stats', function (stats) {
  199. // console.log('stats', stats);
  200. })
  201. jessibuca.on('kBps', function (kBps) {
  202. _this.kBps = Math.round(kBps);
  203. });
  204. // 显示时间戳 PTS
  205. jessibuca.on('videoFrame', function () {
  206. })
  207. //
  208. jessibuca.on('metadata', function () {
  209. });
  210. },
  211. playBtnClick: function (event) {
  212. this.play(this.videoUrl)
  213. },
  214. play: function (url) {
  215. console.log(url)
  216. if (jessibucaPlayer[this._uid]) {
  217. this.destroy();
  218. }
  219. this.create();
  220. jessibucaPlayer[this._uid].on("play", () => {
  221. this.playing = true;
  222. this.loaded = true;
  223. this.quieting = jessibuca.quieting;
  224. });
  225. if (jessibucaPlayer[this._uid].hasLoaded()) {
  226. jessibucaPlayer[this._uid].play(url);
  227. } else {
  228. jessibucaPlayer[this._uid].on("load", () => {
  229. console.log("load 播放")
  230. jessibucaPlayer[this._uid].play(url);
  231. });
  232. }
  233. },
  234. pause: function () {
  235. if (jessibucaPlayer[this._uid]) {
  236. jessibucaPlayer[this._uid].pause();
  237. }
  238. this.playing = false;
  239. this.err = "";
  240. this.performance = "";
  241. },
  242. screenshot: function () {
  243. if (jessibucaPlayer[this._uid]) {
  244. jessibucaPlayer[this._uid].screenshot();
  245. }
  246. },
  247. mute: function () {
  248. if (jessibucaPlayer[this._uid]) {
  249. jessibucaPlayer[this._uid].mute();
  250. }
  251. },
  252. cancelMute: function () {
  253. if (jessibucaPlayer[this._uid]) {
  254. jessibucaPlayer[this._uid].cancelMute();
  255. }
  256. },
  257. destroy: function () {
  258. if (jessibucaPlayer[this._uid]) {
  259. jessibucaPlayer[this._uid].destroy();
  260. }
  261. if (document.getElementById("buttonsBox") == null) {
  262. this.$refs.container.appendChild(this.btnDom)
  263. }
  264. jessibucaPlayer[this._uid] = null;
  265. this.playing = false;
  266. this.err = "";
  267. this.performance = "";
  268. },
  269. eventcallbacK: function (type, message) {
  270. // console.log("player 事件回调")
  271. // console.log(type)
  272. // console.log(message)
  273. },
  274. fullscreenSwich: function () {
  275. let isFull = this.isFullscreen()
  276. jessibucaPlayer[this._uid].setFullscreen(!isFull)
  277. this.fullscreen = !isFull;
  278. },
  279. isFullscreen: function () {
  280. return document.fullscreenElement ||
  281. document.msFullscreenElement ||
  282. document.mozFullScreenElement ||
  283. document.webkitFullscreenElement || false;
  284. }
  285. },
  286. destroyed() {
  287. if (jessibucaPlayer[this._uid]) {
  288. jessibucaPlayer[this._uid].destroy();
  289. }
  290. this.playing = false;
  291. this.loaded = false;
  292. this.performance = "";
  293. },
  294. }
  295. </script>
  296. <style>
  297. .buttons-box {
  298. width: 100%;
  299. height: 28px;
  300. background-color: rgba(43, 51, 63, 0.7);
  301. position: absolute;
  302. display: -webkit-box;
  303. display: -ms-flexbox;
  304. display: flex;
  305. left: 0;
  306. bottom: 0;
  307. user-select: none;
  308. z-index: 10;
  309. }
  310. .jessibuca-btn {
  311. width: 20px;
  312. color: rgb(255, 255, 255);
  313. line-height: 27px;
  314. margin: 0px 10px;
  315. padding: 0px 2px;
  316. cursor: pointer;
  317. text-align: center;
  318. font-size: 0.8rem !important;
  319. }
  320. .buttons-box-right {
  321. position: absolute;
  322. right: 0;
  323. }
  324. </style>