jessibuca.vue 9.0 KB

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