jessibuca.vue 9.1 KB

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