tcplayer-plugin
Version:
基于腾讯云播放器的组件库,支持Vue2、Vue3和原生JS
7 lines (6 loc) • 8.27 kB
JavaScript
/*!
* tcplayer-plugin v0.1.0
* (c) 2025
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue")):"function"==typeof define&&define.amd?define(["exports","vue"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TcPlayer={},e.Vue)}(this,(function(e,t){"use strict";function i(e,t){let o=[...e];if(!o.length)return t&&t(),console.log("resouce loaded"),!1;let n=o.shift();if(!n)return i(o,t),!1;n.endsWith(".js")?function(e,t){if(document.querySelector(`script[src="${e}"]`))return console.log(e,":已存在"),t&&t(),!1;var i=document.createElement("script"),o=t||function(){};i.type="text/javascript",i.readyState?i.onreadystatechange=function(){"loaded"==i.readyState||i.readyState,i.onreadystatechange=null,o()}:(i.onload=function(){o()},i.onerror=function(e){o(),console.log(e)});i.src=e,document.querySelector("body").appendChild(i)}(n,(()=>{i(o,t)})):n.endsWith(".css")?function(e,t){if(document.querySelector(`link[href="${e}"]`))return console.log(e,":已存在"),t&&t(),!1;var i=document.createElement("link"),o=t||function(){};i.rel="stylesheet",i.readyState?i.onreadystatechange=function(){"loaded"==i.readyState||i.readyState,i.onreadystatechange=null,o()}:(i.onload=function(){o()},i.onerror=function(e){o(),console.log(e)});i.href=e,document.querySelector("head").appendChild(i)}(n,(()=>{i(o,t)})):(console.warn("no suport resource:",n),i(o,t))}"undefined"!=typeof window&&(window._pursuer=window._pursuer||{});var o={name:"TcPlayer",props:{config:{type:Object,default:()=>({})}},data:()=>({id:"",duration:0,currentTime:0,hearttime:2e4,timers:[],initShow:!0,_source_loded:!1}),watch:{config:{handler(e){e&&this._source_loded},deep:!0}},mounted(){if("undefined"==typeof window)return!1;this.id=this.getCfPlayerId();const e=[];window.TCPlayer||e.push("//web.sdk.qcloud.com/player/tcplayer/release/v4.5.2/tcplayer.min.css","//web.sdk.qcloud.com/player/tcplayer/release/v4.5.2/libs/hls.min.0.13.2m.js","//web.sdk.qcloud.com/player/tcplayer/release/v4.5.2/tcplayer.v4.5.2.min.js"),i(e,(()=>{this._source_loded=!0,this.$nextTick((()=>{this.init()}))})),this.$playerBox=document,this.$playerBox.addEventListener("keydown",this.onkeydown,!0),this.$playerBox.addEventListener("keyup",this.onkeyup,!0)},beforeDestroy(){this.destroy()},beforeUnmount(){this.destroy()},methods:{getCfPlayerId(){const e="undefined"!=typeof window?window._pursuer:{};e.playerCount=e.playerCount?e.playerCount+1:1;return this.config&&this.config.id||"player-container-id"},init(){if(!document.querySelector(`#${this.id}`))return console.log(`#${this.id} dom is not found`);this.initPlayer({width:"686px",height:"450px",reportable:!0,...this.config.tconfig||{}})},initPlayer(e){if(this.$player&&(this.destroy(),console.log("run tcplayer destroy")),!window.TCPlayer)return console.log("TCPlayer not found in window"),!1;this.hearttime=this.config.hearttime||this.hearttime,console.log("init player config:",e),this.runs(e)},runs(e){this.$player=new window.TCPlayer(this.id,e),this.$player.on("ready",this.onready),this.$player.on("error",this.onerror),this.$player.on("timeupdate",this.ontimeupdate),this.$player.on("pause",this.onpause),this.$player.on("play",this.onplay),this.$player.on("ended",this.onended),this.$player.on("waiting",this.onwaiting),this.$player.on("snapshoted",this.onsnapshoted),this.$player.on("sourceloaded",this.onsourceloaded),this.$player.getDuration=this.$player.duration,this.$player.getCurrentTime=this.$player.currentTime,this.$player._el=this.$player.el_,this.$player.seek=this.$player.currentTime,this.$player.getStatus=this.$player.getStatus||function(){return this.$player.paused()?"pause":"playing"}.bind(this),this.$player.destroy=this.$player.dispose,this.$player._originalPlaybackRate=this.$player.playbackRate,this.$player.getVolume=this.$player.volume,this.$player.setVolume=this.$player.volume},onsourceloaded(e){const t=e.paramData,i=t.desc,o=t.definition;try{const e=this.$player.getComponent("QualityComponent");e&&"function"==typeof e.setCurrentQuality&&e.setCurrentQuality(i,o)}catch(e){console.warn("设置清晰度失败:",e)}},onready(){console.log("onready:",this.$player),this.onheart(!0),this.$emit("onready",this.$player),this.config.onready&&this.config.onready(this.$player)},onerror(e){console.log("error:",e),this.$emit("onerror",this.$player),this.config.onerror&&this.config.onerror(this.$player)},ontimeupdate(){this.onheart(),this.duration=this.$player.getDuration(),this.currentTime=this.$player.getCurrentTime(),this.$emit("ontimeupdate",this.duration,this.currentTime,this.$player),this.config.ontimeupdate&&this.config.ontimeupdate(this.duration,this.currentTime,this.$player)},onpause(){console.log("onpause:",this.$player),this.$emit("onpause",this.$player),this.config.onpause&&this.config.onpause(this.$player)},onplay(){console.log("onplay:"),this.$emit("onplay",this.$player),this.config.onplay&&this.config.onplay(this.$player)},onended(){console.log("ended"),this.onheart(!0),this.$emit("onended",this.$player),this.config.onended&&this.config.onended(this.$player)},onnext(){this.$emit("onnext",this.$player),this.config.onnext&&this.config.onnext(this.$player)},onpre(){this.$emit("onpre",this.$player),this.config.onpre&&this.config.onpre(this.$player)},onwaiting(){this.$emit("onwaiting",this.$player),this.config.onwaiting&&this.config.onwaiting(this.$player)},onsnapshoted(e){this.$emit("onsnapshoted",e,this.$player),this.config.onsnapshoted&&this.config.onsnapshoted(e,this.$player)},onheart(e){return e?(this.$emit("onheart",this.$player),this.config.onheart&&this.config.onheart(this.$player),!1):!this.onheart_lock&&(clearTimeout(this.timers[0]),this.timers[0]=null,this.onheart_lock=!0,void(this.timers[0]=setTimeout((()=>{this.onheart_lock=!1,this.$emit("onheart",this.$player),this.config.onheart&&this.config.onheart(this.$player)}),this.hearttime)))},destroy(){if(this.$player){clearTimeout(this.timers[0]||null),this.onheart_lock=!1,this.$player.off("ready",this.onready),this.$player.off("error",this.onerror),this.$player.off("timeupdate",this.ontimeupdate),this.$player.off("pause",this.onpause),this.$player.off("play",this.onplay),this.$player.off("ended",this.onended),this.$player.off("waiting",this.onwaiting),this.$player.off("snapshoted",this.onsnapshoted),this.$player.off("sourceloaded",this.onsourceloaded);try{this.$player.destroy()}catch(e){console.log("destroy error:",e)}this.$player=null}this.$playerBox&&(this.$playerBox.removeEventListener("keydown",this.onkeydown,!0),this.$playerBox.removeEventListener("keyup",this.onkeyup,!0),this.$playerBox=null)}}};const n={class:"ps-player"},r=["id"];o.render=function(e,i,o,s,a,l){return t.openBlock(),t.createElementBlock("div",n,[a.initShow?(t.openBlock(),t.createElementBlock("video",{key:0,id:"player-container-id",preload:"auto",playsinline:"","webkit-playsinline":"",tabindex:"1",ref:"playerBox",class:"ps-player-container"},null,8,r)):t.createCommentVNode("v-if",!0)])},o.__file="src/tcplayer.vue";const s="0.1.0",a=e=>e&&!!e.prototype,l=e=>e&&!!e.config&&!!e.config.globalProperties,h=function(e,t={}){a(e)||l(e)?e.component(o.name,o):console.error("[TcPlayer] 不支持的Vue版本,请使用Vue 2.x或3.x")};o.install=h,"undefined"!=typeof window&&window.Vue&&(a(window.Vue)?window.Vue.use(o):l(window.Vue)&&window.Vue.use({install:h}));class d{constructor(e,t){if("string"==typeof e&&(e=document.querySelector(e)),!e)throw new Error("[TcPlayer] 目标DOM元素不存在");this.element=e,this.options=t||{},this._init()}_init(){const e=document.createElement("div");if(e.className="tc-player-container",this.element.appendChild(e),window.Vue){if(l(window.Vue)){const t=window.Vue.createApp({render:()=>window.Vue.h(o,{props:this.options})});this.instance=t.mount(e)}else if(a(window.Vue)){const t=window.Vue.extend({render:e=>e(o,{props:this.options})});this.instance=(new t).$mount(e)}}else console.error("[TcPlayer] 请先引入Vue.js")}destroy(){this.instance&&this.instance.$destroy&&this.instance.$destroy(),this.element&&(this.element.innerHTML="")}}const p={install:h,version:s,TcPlayer:o,TcPlayerJS:d};p.install=h,e.TcPlayer=o,e.TcPlayerJS=d,e.default=p,e.install=h,e.version=s,Object.defineProperty(e,"__esModule",{value:!0})}));