ybplayer.js 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. export default function YBPlayer ({container, src, title, poster, formats, barrages, barrageShow, barrageGap, barrageConfig, controls, playShow=true, progressShow=true, timeShow = true, volumeShow=true, settingShow=true, fullscreenShow=true, fullscreen = false, autoplay, mirror, pictureInPicture, nextBtnShow, prevBtnShow, muted, loop, preload, settings, initialTime, duration, volume, longpressPlaybackRate, playbackRate, objectFit, crossOrigin, segments, isLive, flvConfig, enableLongpressPlaybackRate, enableDoubleToggle, enableBlob }){
  2. if(!(this instanceof YBPlayer)){ //如果this不是指向MyClass
  3. throw new TypeError("TypeError: Class constructor YBPlayer cannot be invoked without 'new'")
  4. }
  5. this.container = typeof container == 'string' ? document.getElementById(container) : container
  6. this.src = src || ''
  7. this.title = title || ''
  8. this.poster = poster || ''
  9. this.formats = formats || 'auto'
  10. this.barrages = barrages
  11. this.barrageShow = barrageShow
  12. this.barrageGap = barrageGap || 5
  13. this.barrageConfig = barrageConfig
  14. this.controls = controls
  15. this.playShow = playShow
  16. this.progressShow = progressShow
  17. this.timeShow = timeShow
  18. this.volumeShow = volumeShow
  19. this.settingShow = settingShow
  20. this.fullscreenShow = fullscreenShow
  21. this.fullscreen = fullscreen
  22. this.autoplay = autoplay
  23. this.mirror = mirror
  24. this.pictureInPicture = pictureInPicture
  25. this.nextBtnShow = nextBtnShow
  26. this.prevBtnShow = prevBtnShow
  27. this.muted = muted
  28. this.loop = loop
  29. this.preload = preload || 'auto'
  30. this.settings = settings || 'all'
  31. this.initialTime = initialTime || 0
  32. this.duration = duration || 0
  33. this.volume = volume || 1
  34. this.longpressPlaybackRate = longpressPlaybackRate || 3
  35. this.playbackRate = playbackRate || 1
  36. this.objectFit = objectFit || 'contain'
  37. this.crossOrigin = crossOrigin || ''
  38. this.segments = segments
  39. this.isLive = isLive
  40. this.flvConfig = flvConfig || {}
  41. this.enableLongpressPlaybackRate = enableLongpressPlaybackRate
  42. this.enableDoubleToggle = enableDoubleToggle
  43. this.enableBlob = enableBlob
  44. this.video = null
  45. this.hls = null
  46. this.flv = null
  47. this.barrage = null
  48. this._eventCallback = {}
  49. this._wrapperEl = null
  50. this._videoEl = null
  51. this._posterEl = null
  52. this._videoBackground = 'inherit'
  53. this._headersEl = null
  54. this._controlsEl = null
  55. this._barrageEl = null
  56. this._settingEl = null
  57. this._playbackRateEl = null
  58. this._slotsEl = null
  59. this._controlSlotsEl = null
  60. this._longPlaybackRateEl = null
  61. this._isDrag = false
  62. this._controlsTimer = null
  63. this._seizingTimer = null
  64. this._clickTime = 0,//点击次数
  65. this._touchstartX = 0,//触摸开始x轴位置
  66. this._touchstartY = 0,//触摸开始y轴位置
  67. this._touchmoveX = 0,//触摸滑动x轴距离
  68. this._touchmoveY = 0,//触摸滑动y轴距离
  69. this._touchTime = 0//触摸时间
  70. this._mousedown = false
  71. this._init()
  72. }
  73. Object.defineProperty(YBPlayer.prototype,'_init',{
  74. value:function(){
  75. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  76. throw new TypeError("TypeError: YBPlayer._init is not a constructor")
  77. }
  78. if ( this.container && typeof this.container != 'undefined' ) {
  79. try{
  80. this.container.style.position = 'relative';
  81. this.container.style.overflow = 'hidden';
  82. //监听全屏事件
  83. this.container.addEventListener('fullscreenerror', this._fullscreenerror.bind(this));
  84. this.container.addEventListener('mozfullscreenerror', this._fullscreenerror.bind(this));
  85. this.container.addEventListener('msfullscreenerror', this._fullscreenerror.bind(this));
  86. this.container.addEventListener('webkitfullscreenerror', this._fullscreenerror.bind(this));
  87. this.container.addEventListener('fullscreenchange', this._fullscreenchanged.bind(this));
  88. this.container.addEventListener('mozfullscreenchange', this._fullscreenchanged.bind(this));
  89. this.container.addEventListener('msfullscreenchange', this._fullscreenchanged.bind(this));
  90. this.container.addEventListener('webkitfullscreenchange', this._fullscreenchanged.bind(this));
  91. this._wrapperEl = document.createElement('DIV')
  92. this._wrapperEl.setAttribute('class', 'ybplayer-video-wrapper')
  93. this.container.appendChild(this._wrapperEl)
  94. // this._wrapperEl.onclick = () => {
  95. // if ( this._playbackRateEl ) {
  96. // this._hidePlaybackRate()
  97. // } else if ( this._settingEl ) {
  98. // this._hideSetting()
  99. // } else if ( this._controlsEl ) {
  100. // if ( this._controlsEl.classList.value.indexOf('ybplayer-controls-show') > -1 ) {
  101. // this._hideControls()
  102. // } else {
  103. // this._showControls()
  104. // }
  105. // }
  106. // }
  107. const resetTouch = () => {
  108. this._touchstartX = 0
  109. this._touchstartY = 0
  110. this._touchmoveX = 0
  111. this._touchmoveY = 0
  112. this._touchTime = 0
  113. this._clickTime = 0
  114. }
  115. const threshold = 50
  116. const singleTouchTime = 150//双击间隔时间
  117. const touchstart = (e) => {
  118. if ( this._touchTimer ) {//清楚定时任务
  119. window.clearTimeout(this._touchTimer)
  120. this._touchTimer = null
  121. }
  122. if ( this._longTimer ) {//清楚定时任务
  123. window.clearTimeout(this._longTimer)
  124. this._longTimer = null
  125. }
  126. const touch = e.touches[0]
  127. this._touchstartX = touch.pageX
  128. this._touchstartY = touch.pageY
  129. this._clickTime++
  130. if ( this._clickTime == 1 ) {//第一次点击
  131. this._touchTimer = window.setTimeout(() => {
  132. this._touchTime = singleTouchTime
  133. }, singleTouchTime)
  134. this._longTimer = window.setTimeout(() => {
  135. this._touchTime = 500
  136. if ( this._touchmoveX <= threshold && this._touchmoveY <= threshold && this.enableLongpressPlaybackRate ) {
  137. //长按事件
  138. this._isLongTouch = 1
  139. this._showLongPlaybackRate()
  140. }
  141. resetTouch()
  142. }, 500)
  143. }
  144. }
  145. const touchmove = (e) => {
  146. if ( this._clickTime > 0 ) {
  147. const touch = e.touches[0]
  148. this._touchmoveX = Math.abs(touch.pageX - this._touchstartX)
  149. this._touchmoveY = Math.abs(touch.pageY - this._touchstartY)
  150. }
  151. }
  152. const touchend = (e) => {
  153. if ( this._touchTimer ) {//清楚按下定时任务
  154. window.clearTimeout(this._touchTimer);
  155. this._touchTimer = null
  156. }
  157. if ( this._longTimer ) {//清楚长按定时任务
  158. window.clearTimeout(this._longTimer)
  159. this._longTimer = null
  160. }
  161. this._hideLongPlaybackRate()
  162. //点击次数为零或者触摸时间超过单击时间,或者滑动距离超过阙值
  163. if ( this._clickTime == 0 || this._touchTime > singleTouchTime || this._touchmoveX > threshold || this._touchmoveY > threshold ) {
  164. resetTouch()
  165. return
  166. }
  167. if ( this._clickTime == 1 ) {//第一次点击
  168. this._touchTimer = window.setTimeout(() => {
  169. //单击事件
  170. if ( this._playbackRateEl ) {
  171. this._hidePlaybackRate()
  172. } else if ( this._settingEl ) {
  173. this._hideSetting()
  174. } else if ( this._controlsEl ) {
  175. if ( this._controlsEl.classList.value.indexOf('ybplayer-controls-show') > -1 ) {
  176. this._hideControls()
  177. } else {
  178. this._showControls()
  179. }
  180. }
  181. resetTouch()
  182. }, singleTouchTime)
  183. }
  184. if ( this._clickTime == 2 ) {//第二次点击
  185. //双击事件
  186. if ( this.enableDoubleToggle ) this.toggle()
  187. resetTouch()
  188. }
  189. }
  190. this._wrapperEl.ontouchstart = touchstart
  191. this._wrapperEl.ontouchmove = touchmove
  192. this._wrapperEl.ontouchend = touchend
  193. this._wrapperEl.onmousedown = (e) => {
  194. if ( 'ontouchstart' in window || (window.DocumentTouch && document instanceof DocumentTouch) ) return//设备支持触屏则不触发mousedown
  195. this._mousedown = true
  196. touchstart({touches: [{pageX: e.pageX, pageY: e.pageY}]})
  197. }
  198. this._wrapperEl.onmousemove = (e) => {
  199. if ( !this._mousedown ) return
  200. touchmove({touches: [{pageX: e.pageX, pageY: e.pageY}]})
  201. }
  202. this._wrapperEl.onmouseup = (e) => {
  203. this._mousedown = false
  204. touchend({touches: [{pageX: e.pageX, pageY: e.pageY}]})
  205. }
  206. this._initVideo();
  207. this._initSlots();
  208. this._setVideoUrl();
  209. if ( this.fullscreen ) this._initHeaders();
  210. if ( this.controls ) this._initControls();
  211. }catch(e){
  212. throw new Error(e.toString());
  213. }
  214. }
  215. },
  216. enumerable:false
  217. })
  218. Object.defineProperty(YBPlayer.prototype,'_destroy',{
  219. value:function(destroyContainer = true){
  220. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  221. throw new TypeError("TypeError: YBPlayer._destroy is not a constructor")
  222. }
  223. this._destroyHls()
  224. this._destroyFlv();
  225. this._destroyControls();
  226. this._destroyHeaders();
  227. this._destroyBarrage();
  228. this._destroyVideo();
  229. this._destroySlots();
  230. if ( this._wrapperEl ) {
  231. this._wrapperEl.onclick = null;
  232. this._wrapperEl.remove();
  233. this._wrapperEl = null;
  234. }
  235. if ( this.container && destroyContainer ) {
  236. this.container.removeEventListener('fullscreenerror', this._fullscreenerror.bind(this));
  237. this.container.removeEventListener('mozfullscreenerror', this._fullscreenerror.bind(this));
  238. this.container.removeEventListener('msfullscreenerror', this._fullscreenerror.bind(this));
  239. this.container.removeEventListener('webkitfullscreenerror', this._fullscreenerror.bind(this));
  240. this.container.removeEventListener('fullscreenchange', this._fullscreenchanged.bind(this));
  241. this.container.removeEventListener('mozfullscreenchange', this._fullscreenchanged.bind(this));
  242. this.container.removeEventListener('msfullscreenchange', this._fullscreenchanged.bind(this));
  243. this.container.removeEventListener('webkitfullscreenchange', this._fullscreenchanged.bind(this));
  244. this.container = null;
  245. }
  246. },
  247. enumerable:false
  248. })
  249. Object.defineProperty(YBPlayer.prototype,'_initVideo',{
  250. value:function(){
  251. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  252. throw new TypeError("TypeError: YBPlayer._initVideo is not a constructor")
  253. }
  254. this._videoEl = document.createElement('DIV');
  255. this._videoEl.setAttribute('class', 'ybplayer-video-content');
  256. this._wrapperEl.appendChild(this._videoEl);
  257. this.video = document.createElement('VIDEO');
  258. this.video.setAttribute('style', 'width: 100%;height:100%;flex:1;opacity:0;object-fit:' + this.objectFit + ';');
  259. this.video.setAttribute('preload', this.preload);
  260. this.video.setAttribute('volume', this.volume);
  261. this.video.setAttribute('x-webkit-airplay', 'allow');
  262. this.video.setAttribute('webkit-playsinline', true);
  263. this.video.setAttribute('playsinline', true);
  264. this.video.setAttribute('x5-video-player-type', 'h5');
  265. this.video.setAttribute('x5-video-play', true);
  266. this.crossOrigin && this.video.setAttribute('crossOrigin', this.crossOrigin);
  267. this.video.innerHTML = '您的浏览器不支持 video 标签。';
  268. this._videoEl.appendChild(this.video);
  269. this._showPoster()
  270. this.video.muted = this.muted;
  271. this.video.defaultMuted = this.muted;
  272. this.video.playbackRate = this.playbackRate;
  273. this.video.defaultPlaybackRate = this.playbackRate;
  274. this.video.autoplay = this.autoplay;
  275. this.video.loop = this.loop;
  276. this.video.oncanplay = () => {
  277. this._eventCallback.canplay && this._eventCallback.canplay({
  278. duration: this.video.duration,
  279. width: this.video.videoWidth,//视频宽度
  280. height: this.video.videoHeight//视频高度
  281. });
  282. }
  283. this.video.oncanplaythrough = () => {
  284. this._eventCallback.canplaythrough && this._eventCallback.canplaythrough({
  285. duration: this.video.duration,
  286. width: this.video.videoWidth,//视频宽度
  287. height: this.video.videoHeight//视频高度
  288. });
  289. }
  290. this.video.onloadeddata = () => {
  291. this._eventCallback.loadeddata && this._eventCallback.loadeddata({
  292. duration: this.video.duration,
  293. width: this.video.videoWidth,//视频宽度
  294. height: this.video.videoHeight//视频高度
  295. });
  296. this.video.currentTime = this.initialTime;
  297. this.video.style.opacity = 1
  298. }
  299. this.video.onloadedmetadata = () => {
  300. this._eventCallback.loadedmetadata && this._eventCallback.loadedmetadata({
  301. duration: this.video.duration,
  302. width: this.video.videoWidth,//视频宽度
  303. height: this.video.videoHeight//视频高度
  304. });
  305. if ( this.barrageShow ) this._initBarrage();
  306. }
  307. this.video.onloadstart = () => {
  308. this._eventCallback.loadstart && this._eventCallback.loadstart();
  309. }
  310. this.video.onplay = () => {
  311. this._hidePoster()
  312. this._eventCallback.play && this._eventCallback.play()
  313. this.barrage && this.barrage.play();
  314. this._setControlsView('play')
  315. if ( this.duration && this.video.currentTime >= this.duration && !this.isLive ) this.seek(0)
  316. if ( this.flv ) this.flv.play()
  317. }
  318. this.video.onpause = () => {
  319. this._eventCallback.pause && this._eventCallback.pause();
  320. this.barrage && this.barrage.pause();
  321. this._setControlsView('play')
  322. if ( this.flv ) this.flv.pause()
  323. }
  324. this.video.onended = () => {
  325. this._eventCallback.ended && this._eventCallback.ended();
  326. }
  327. this.video.onseeking = () => {
  328. this._eventCallback.seeking && this._eventCallback.seeking({
  329. currentTime: this.video.currentTime
  330. });
  331. }
  332. this.video.onseeked = () => {
  333. this._eventCallback.seeked && this._eventCallback.seeked({
  334. currentTime: this.video.currentTime
  335. });
  336. this.barrage && this.barrage.seek(this.video.currentTime);
  337. }
  338. this.video.ontimeupdate = () => {
  339. this._eventCallback.timeupdate && this._eventCallback.timeupdate({
  340. currentTime: this.video.currentTime
  341. })
  342. if ( this.duration && this.video.currentTime >= this.duration && !this.isLive ) {
  343. if ( this.loop ) {
  344. this.seek(0)
  345. } else {
  346. this.pause();
  347. this._eventCallback.ended && this._eventCallback.ended();
  348. }
  349. }
  350. if ( this._seizingTimer ) {
  351. window.clearTimeout(this._seizingTimer)
  352. this._seizingTimer = null
  353. }
  354. this._seizingTimer = window.setTimeout(() => {
  355. if ( this.video && this.video.src && !this.video.paused ) this._eventCallback.seizing && this._eventCallback.seizing()
  356. }, 5000)
  357. this._setControlsView('timeUpdate')
  358. }
  359. this.video.ondurationchange = () => {
  360. this._eventCallback.durationchange && this._eventCallback.durationchange({
  361. duration: this.video.duration
  362. })
  363. }
  364. this.video.onwaiting = () => {
  365. this._eventCallback.waiting && this._eventCallback.waiting({
  366. currentTime: this.video.currentTime
  367. });
  368. this.barrage && this.barrage.pause();
  369. }
  370. this.video.onplaying = () => {
  371. this._eventCallback.playing && this._eventCallback.playing({
  372. currentTime: this.video.currentTime
  373. })
  374. this.barrage && this.barrage.play();
  375. }
  376. this.video.onprogress = () => {
  377. this._eventCallback.progress && this._eventCallback.progress({
  378. buffered: this.video.buffered
  379. })
  380. }
  381. this.video.onabort = () => {
  382. this._eventCallback.abort && this._eventCallback.abort();
  383. }
  384. this.video.onerror = (e) => {
  385. this._eventCallback.error && this._eventCallback.error(this._deepClone(e));
  386. }
  387. this.video.onvolumechange = () => {
  388. this._eventCallback.volumechange && this._eventCallback.volumechange({
  389. volume: this.video.volume
  390. });
  391. this._setControlsView('volume')
  392. }
  393. this.video.onratechange = () => {
  394. this._eventCallback.ratechange && this._eventCallback.ratechange({
  395. playbackRate: this.video.playbackRate
  396. });
  397. if ( this._playbackRateEl && this._querySelector('ybplayer-setting') ) {
  398. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  399. this._querySelectorAll('ybplayer-setting')[i].getElementsByClassName('ybplayer-setting-text')[0].style.color = this.playbackRate == this._querySelectorAll('ybplayer-setting')[i].getAttribute('data-rate') ? '#2ca2f9' : '#333';
  400. }
  401. }
  402. this.barrage && this.barrage.setConfig({
  403. playbackRate: this.video.playbackRate
  404. });
  405. }
  406. this.video.onenterpictureinpicture = () => {
  407. this._eventCallback.enterpictureinpicture && this._eventCallback.enterpictureinpicture()
  408. this.pictureInPicture = true
  409. this._setSettingView('pictureInPicture');
  410. }
  411. this.video.onleavepictureinpicture = () => {
  412. this._eventCallback.leavepictureinpicture && this._eventCallback.leavepictureinpicture()
  413. this.pictureInPicture = false
  414. this._setSettingView('pictureInPicture');
  415. }
  416. this._barrageEl = document.createElement('DIV');
  417. this._barrageEl.setAttribute('class', 'ybplayer-video-barrage');
  418. this._barrageEl.setAttribute('style', 'margin:' + this.barrageGap + 'px 0;');
  419. this._wrapperEl.appendChild(this._barrageEl);
  420. },
  421. enumerable:false
  422. })
  423. Object.defineProperty(YBPlayer.prototype,'_destroyVideo',{
  424. value:function(){
  425. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  426. throw new TypeError("TypeError: YBPlayer._destroyVideo is not a constructor")
  427. }
  428. if ( this.video ) {
  429. this.video.pause();
  430. this.video.removeAttribute('src')
  431. this.video.load()
  432. this.video.oncanplay = null;
  433. this.video.onloadedmetadata = null;
  434. this.video.onplay = null;
  435. this.video.onpause = null;
  436. this.video.onended = null;
  437. this.video.onseeking = null;
  438. this.video.onseeked = null;
  439. this.video.ontimeupdate = null;
  440. this.video.ondurationchange = null;
  441. this.video.onwaiting = null;
  442. this.video.onplaying = null;
  443. this.video.onprogress = null;
  444. this.video.onabort = null;
  445. this.video.onerror = null;
  446. this.video.onvolumechange = null;
  447. this.video.onratechange = null;
  448. this.video.onenterpictureinpicture = null;
  449. this.video.onleavepictureinpicture = null;
  450. this.video.remove();
  451. this.video = null;
  452. }
  453. this._hidePoster()
  454. this._hideLongPlaybackRate()
  455. if ( this._videoEl ) {
  456. this._videoEl.remove();
  457. this._videoEl = null;
  458. }
  459. if ( this._barrageEl ) {
  460. this._barrageEl.remove();
  461. this._barrageEl = null;
  462. }
  463. if ( this._seizingTimer ) {
  464. window.clearTimeout(this._seizingTimer)
  465. this._seizingTimer = null
  466. }
  467. },
  468. enumerable:false
  469. })
  470. Object.defineProperty(YBPlayer.prototype,'_showPoster',{
  471. value:function(){
  472. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  473. throw new TypeError("TypeError: YBPlayer._showPoster is not a constructor")
  474. }
  475. if ( this._videoEl && this.poster && this.video.paused ) {
  476. let img = new Image();
  477. img.onload = () => {
  478. if ( this.video.paused ) {
  479. this._posterEl = document.createElement('DIV');
  480. this._posterEl.setAttribute('class', 'ybplayer-video-poster');
  481. this._posterEl.setAttribute('style', `background-size: ${this.objectFit};background-image: url(${this.poster})`);
  482. this._videoEl.appendChild(this._posterEl);
  483. }
  484. img = null
  485. }
  486. img.onerror = () => {
  487. img = null
  488. }
  489. img.src = this.poster;
  490. }
  491. },
  492. enumerable:false
  493. })
  494. Object.defineProperty(YBPlayer.prototype,'_hidePoster',{
  495. value:function(){
  496. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  497. throw new TypeError("TypeError: YBPlayer._hidePoster is not a constructor")
  498. }
  499. if ( this._posterEl ) {
  500. this._posterEl.onerror = null
  501. this._posterEl.onload = null
  502. this._posterEl.remove()
  503. this._posterEl = null
  504. }
  505. },
  506. enumerable:false
  507. })
  508. Object.defineProperty(YBPlayer.prototype,'_initHeaders',{
  509. value:function(){
  510. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  511. throw new TypeError("TypeError: YBPlayer._initHeaders is not a constructor")
  512. }
  513. if ( this.container && this.title ) {
  514. this._destroyHeaders()
  515. this._headersEl = document.createElement('DIV');
  516. this._headersEl.setAttribute('class', 'ybplayer-headers');
  517. this._headersEl.innerHTML = `
  518. <div class="ybplayer-headers-shadow"></div>
  519. <i class="ybplayerIconfont icon-angle-arrow-left ybplayer-icon ${this._setClassName('ybplayer-icon-back')}"></i>
  520. <span class="${this._setClassName('ybplayer-headers-title')}">${this.title}</span>
  521. `;
  522. this.container.appendChild(this._headersEl);
  523. this._querySelector('ybplayer-icon-back').onclick = () => {
  524. this.exitFullscreen()
  525. }
  526. }
  527. },
  528. enumerable:false
  529. })
  530. Object.defineProperty(YBPlayer.prototype,'_destroyHeaders',{
  531. value:function(){
  532. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  533. throw new TypeError("TypeError: YBPlayer._destroyHeaders is not a constructor")
  534. }
  535. if ( this._headersEl ) {
  536. this._querySelector('ybplayer-icon-back').onclick = null
  537. this._headersEl.remove();
  538. this._headersEl = null;
  539. }
  540. },
  541. enumerable:false
  542. })
  543. Object.defineProperty(YBPlayer.prototype,'_initControls',{
  544. value:function(){
  545. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  546. throw new TypeError("TypeError: YBPlayer._initControls is not a constructor")
  547. }
  548. if ( this.container ) {
  549. this._controlsEl = document.createElement('DIV');
  550. this._controlsEl.setAttribute('class', 'ybplayer-controls');
  551. this._controlsEl.innerHTML = `
  552. <div class="ybplayer-controls-top">
  553. <div class="${this._setClassName('ybplayer-icon-play-prev')} ybplayer-controls-item" style="margin-right: ${this.prevBtnShow ? '10px' : 0};">
  554. ${this.prevBtnShow ? `
  555. <i class="ybplayerIconfont icon-play-prev-fill ybplayer-icon"></i>
  556. ` : ''}
  557. </div>
  558. <div class="${this._setClassName('ybplayer-icon-play')} ybplayer-controls-item">
  559. ${this.playShow ? `
  560. <i class="ybplayerIconfont ${this.video && !this.video.paused ? 'icon-pause' : 'icon-play'} ybplayer-icon"></i>
  561. ` : ''}
  562. </div>
  563. <div class="${this._setClassName('ybplayer-icon-play-next')} ybplayer-controls-item" style="margin-left: ${this.nextBtnShow ? '10px' : 0};">
  564. ${this.nextBtnShow ? `
  565. <i class="ybplayerIconfont icon-play-next-fill ybplayer-icon"></i>
  566. ` : ''}
  567. </div>
  568. <div class="${this._setClassName('ybplayer-controls-time')} ybplayer-controls-item">
  569. ${this.timeShow ? ` 00:00 / 00:00 ` : ''}
  570. </div>
  571. <div class="${this._setClassName('ybplayer-icon-fullscreen')} ybplayer-controls-item ybplayer-controls-item-right" style="margin-left: ${this.fullscreenShow ? '25px' : 0};">
  572. ${this.fullscreenShow ? `
  573. <i class="ybplayerIconfont ${ this.fullscreen ? 'icon-out-fullscreen' : 'icon-in-fullscreen'} ybplayer-icon"></i>
  574. ` : ''}
  575. </div>
  576. <div class="${this._setClassName('ybplayer-icon-setting')} ybplayer-controls-item ybplayer-controls-item-right" style="margin-left: ${this.settingShow ? '25px' : 0};">
  577. ${this.settingShow ? `
  578. <i class="ybplayerIconfont icon-setting ybplayer-icon"></i>
  579. ` : ''}
  580. </div>
  581. <div class="${this._setClassName('ybplayer-icon-volume')} ybplayer-controls-item ybplayer-controls-item-right" style="margin-left: ${this.volumeShow ? '25px' : 0};">
  582. ${this.volumeShow ? `
  583. <i class="ybplayerIconfont icon-volume-${this.muted ? 'muted' : 'medium'} ybplayer-icon"></i>
  584. ` : ''}
  585. </div>
  586. </div>
  587. <div class="${this._setClassName('ybplayer-controls-progress')}">
  588. </div>
  589. <div class="ybplayer-controls-shadow"></div>
  590. `;
  591. this.container.appendChild(this._controlsEl);
  592. this._controlsEl.onmousedown = () => {
  593. this._clear_controlsTimer()
  594. }
  595. this._controlsEl.onmouseleave = () => {
  596. this._start_controlsTimer()
  597. }
  598. if ( this.playShow ) {
  599. this._querySelector('ybplayer-icon-play').onclick = () => {
  600. this.toggle()
  601. }
  602. }
  603. if ( this.volumeShow ) {
  604. this._querySelector('ybplayer-icon-volume').onclick = () => {
  605. this.setConfig('muted', !this.muted)
  606. }
  607. }
  608. if ( this.settingShow ) {
  609. this._querySelector('ybplayer-icon-setting').onclick = () => {
  610. this._showSetting()
  611. }
  612. }
  613. if ( this.fullscreenShow ) {
  614. this._querySelector('ybplayer-icon-fullscreen').onclick = () => {
  615. this.switchFullscreen()
  616. }
  617. }
  618. if ( this.prevBtnShow ) {
  619. this._querySelector('ybplayer-icon-play-prev').onclick = () => {
  620. this._eventCallback.prevBtnClick && this._eventCallback.prevBtnClick();
  621. }
  622. }
  623. if ( this.nextBtnShow ) {
  624. this._querySelector('ybplayer-icon-play-next').onclick = () => {
  625. this._eventCallback.nextBtnClick && this._eventCallback.nextBtnClick();
  626. }
  627. }
  628. this._initProgress()
  629. this._showControls();
  630. }
  631. },
  632. enumerable:false
  633. })
  634. Object.defineProperty(YBPlayer.prototype,'_destroyControls',{
  635. value:function(){
  636. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  637. throw new TypeError("TypeError: YBPlayer._destroyControls is not a constructor")
  638. }
  639. if ( this._controlsEl ) {
  640. this._destroyProgress()
  641. this._controlsEl.onmousedown = null
  642. this._controlsEl.onmouseleave = null
  643. if ( this.playShow && this._querySelector('ybplayer-icon-volume') ) this._querySelector('ybplayer-icon-play').onclick = null
  644. if ( this.volumeShow && this._querySelector('ybplayer-icon-volume') ) this._querySelector('ybplayer-icon-volume').onclick = null
  645. if ( this.settingShow && this._querySelector('ybplayer-icon-setting') ) this._querySelector('ybplayer-icon-setting').onclick = null
  646. if ( this.fullscreenShow && this._querySelector('ybplayer-icon-fullscreen') ) this._querySelector('ybplayer-icon-fullscreen').onclick = null
  647. if ( this.prevBtnShow && this._querySelector('ybplayer-icon-play-prev') ) this._querySelector('ybplayer-icon-play-prev').onclick = null
  648. if ( this.nextBtnShow && this._querySelector('ybplayer-icon-play-next') ) this._querySelector('ybplayer-icon-play-next').onclick = null
  649. this._controlsEl.remove();
  650. this._controlsEl = null
  651. }
  652. if ( this._settingEl ) {
  653. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  654. this._querySelectorAll('ybplayer-setting').onclick = null
  655. }
  656. this._settingEl.remove();
  657. this._settingEl = null;
  658. }
  659. if ( this._playbackRateEl ) {
  660. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  661. this._querySelectorAll('ybplayer-setting')[i].onclick = null
  662. }
  663. this._playbackRateEl.remove();
  664. this._playbackRateEl = null;
  665. }
  666. },
  667. enumerable:false
  668. })
  669. Object.defineProperty(YBPlayer.prototype,'_showControls',{
  670. value:function(){
  671. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  672. throw new TypeError("TypeError: YBPlayer._showControls is not a constructor")
  673. }
  674. this._clear_controlsTimer()
  675. if ( this._controlsEl ) {
  676. this._controlsEl.setAttribute('class', 'ybplayer-controls ybplayer-controls-show');
  677. if ( this._controlSlotsEl ) this._controlSlotsEl.style.visibility = 'visible';
  678. if ( this._headersEl ) this._headersEl.setAttribute('class', 'ybplayer-headers ybplayer-headers-show');
  679. this._eventCallback.controlsChange && this._eventCallback.controlsChange({
  680. controls: true
  681. });
  682. this._start_controlsTimer()
  683. }
  684. },
  685. enumerable:false
  686. })
  687. Object.defineProperty(YBPlayer.prototype,'_hideControls',{
  688. value:function(){
  689. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  690. throw new TypeError("TypeError: YBPlayer._hideControls is not a constructor")
  691. }
  692. this._clear_controlsTimer()
  693. if ( this._controlsEl ) {
  694. this._controlsEl.setAttribute('class', 'ybplayer-controls ybplayer-controls-hide')
  695. if ( this._headersEl ) this._headersEl.setAttribute('class', 'ybplayer-headers ybplayer-headers-hide');
  696. if ( this._controlSlotsEl ) this._controlSlotsEl.style.visibility = 'hidden';
  697. this._eventCallback.controlsChange && this._eventCallback.controlsChange({
  698. controls: false
  699. });
  700. }
  701. },
  702. enumerable:false
  703. })
  704. Object.defineProperty(YBPlayer.prototype,'_setControlsView',{
  705. value:function(value){
  706. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  707. throw new TypeError("TypeError: YBPlayer._setControlsView is not a constructor")
  708. }
  709. if ( !this.controls ) return
  710. if ( value == 'play' && this._querySelector('ybplayer-icon-play') ) {
  711. if ( this.playShow ) {
  712. this._querySelector('ybplayer-icon-play').innerHTML = `<i class="ybplayerIconfont icon-${this.video.paused ? 'play' : 'pause'} ybplayer-icon"></i>`;
  713. } else {
  714. this._querySelector('ybplayer-icon-play').innerHTML = '';
  715. }
  716. } else if ( value == 'timeUpdate' ) {
  717. if ( !this._isDrag ) {
  718. if ( this.timeShow || this.progressShow ) {
  719. var currentTime = this.video ? this.video.currentTime && this.video.currentTime != 'Infinity' ? this.video.currentTime : 0 : 0;
  720. var duration = this.isLive ? this.duration : this.video ? (this.duration || (this.video.duration && this.video.duration != 'Infinity' ? this.video.duration : 0) ) : 0;
  721. var rate = duration > 0 ? currentTime / duration : 0
  722. if ( this.progressShow ) {
  723. if ( this._querySelector('ybplayer-slider-focus') ) {
  724. this._querySelector('ybplayer-slider-focus').style.width = (rate * 100) + '%'
  725. }
  726. if ( this._querySelector('ybplayer-slider') ) {
  727. this._querySelector('ybplayer-slider').value = rate * 100
  728. }
  729. }
  730. if ( this._querySelector('ybplayer-controls-time') && this.timeShow ) {
  731. this._querySelector('ybplayer-controls-time').innerHTML = this.isLive ? this._timesFormat(parseInt(duration) + parseInt(currentTime)) : `${this._timesFormat(currentTime)} / ${this._timesFormat(duration)}`
  732. }
  733. } else {
  734. this._querySelector('ybplayer-controls-time').innerHTML = ''
  735. }
  736. }
  737. } else if ( value == 'volume' && this._querySelector('ybplayer-icon-volume') ) {
  738. if ( this.volumeShow ) {
  739. this._querySelector('ybplayer-icon-volume').style.marginLeft = '25px';
  740. this._querySelector('ybplayer-icon-volume').innerHTML = '<i class="ybplayerIconfont icon-volume-' + (this.muted ? 'muted' : 'medium') + ' ybplayer-icon"></i>'
  741. this._querySelector('ybplayer-icon-volume').onclick = () => {
  742. this.setConfig('muted', !this.muted)
  743. }
  744. } else {
  745. this._querySelector('ybplayer-icon-volume').style.marginLeft = '';
  746. this._querySelector('ybplayer-icon-volume').innerHTML = '';
  747. this._querySelector('ybplayer-icon-volume').onclick = null
  748. }
  749. } else if ( value == 'setting' && this._querySelector('ybplayer-icon-setting') ) {
  750. if ( this.settingShow ) {
  751. this._querySelector('ybplayer-icon-setting').style.marginLeft = '25px';
  752. this._querySelector('ybplayer-icon-setting').innerHTML = '<i class="ybplayerIconfont icon-setting ybplayer-icon"></i>';
  753. this._querySelector('ybplayer-icon-setting').onclick = () => {
  754. this._showSetting()
  755. }
  756. } else {
  757. this._querySelector('ybplayer-icon-setting').style.marginLeft = '';
  758. this._querySelector('ybplayer-icon-setting').innerHTML = '';
  759. this._querySelector('ybplayer-icon-setting').onclick = null;
  760. }
  761. } else if ( value == 'fullscreen' && this._querySelector('ybplayer-icon-fullscreen') ) {
  762. if ( this.fullscreenShow ) {
  763. this._querySelector('ybplayer-icon-fullscreen').style.marginLeft = '25px';
  764. this._querySelector('ybplayer-icon-fullscreen').innerHTML = '<i class="ybplayerIconfont icon-' + (this.fullscreen ? 'out-fullscreen' : 'in-fullscreen') + ' ybplayer-icon"></i>';
  765. this._querySelector('ybplayer-icon-fullscreen').onclick = () => {
  766. this.switchFullscreen()
  767. }
  768. } else {
  769. this._querySelector('ybplayer-icon-fullscreen').style.marginLeft = '';
  770. this._querySelector('ybplayer-icon-fullscreen').innerHTML = '';
  771. this._querySelector('ybplayer-icon-fullscreen').onclick = null;
  772. }
  773. } else if ( value == 'prevBtn' && this._querySelector('ybplayer-icon-play-prev') ) {
  774. if ( this.prevBtnShow ) {
  775. this._querySelector('ybplayer-icon-play-prev').style.marginRight = '10px';
  776. this._querySelector('ybplayer-icon-play-prev').innerHTML = '<i class="ybplayerIconfont icon-play-prev-fill ybplayer-icon"></i>';
  777. this._querySelector('ybplayer-icon-play-prev').onclick = () => {
  778. this._eventCallback.prevBtnClick && this._eventCallback.prevBtnClick()
  779. }
  780. } else {
  781. this._querySelector('ybplayer-icon-play-prev').style.marginRight = '';
  782. this._querySelector('ybplayer-icon-play-prev').innerHTML = '';
  783. this._querySelector('ybplayer-icon-play-prev').onclick = null;
  784. }
  785. } else if ( value == 'nextBtn' && this._querySelector('ybplayer-icon-play-next') ) {
  786. if ( this.nextBtnShow ) {
  787. this._querySelector('ybplayer-icon-play-next').style.marginLeft = '10px';
  788. this._querySelector('ybplayer-icon-play-next').innerHTML = '<i class="ybplayerIconfont icon-play-next-fill ybplayer-icon"></i>';
  789. this._querySelector('ybplayer-icon-play-next').onclick = () => {
  790. this._eventCallback.nextBtnClick && this._eventCallback.nextBtnClick()
  791. }
  792. } else {
  793. this._querySelector('ybplayer-icon-play-next').style.marginLeft = '';
  794. this._querySelector('ybplayer-icon-play-next').innerHTML = '';
  795. this._querySelector('ybplayer-icon-play-next').onclick = null;
  796. }
  797. }
  798. },
  799. enumerable:false
  800. })
  801. Object.defineProperty(YBPlayer.prototype,'_initProgress',{
  802. value:function(){
  803. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  804. throw new TypeError("TypeError: YBPlayer._initProgress is not a constructor")
  805. }
  806. if ( this._querySelector('ybplayer-controls-progress') && this.progressShow && !this.isLive ) {
  807. this._querySelector('ybplayer-controls-progress').innerHTML = `
  808. <div class="ybplayer-slider-box">
  809. <div class="${this._setClassName('ybplayer-slider-track')}"></div>
  810. <div class="${this._setClassName('ybplayer-slider-focus')}"></div>
  811. <input class="${this._setClassName('ybplayer-slider')} ybplayer-controls-slider" value="0" type="range">
  812. </div>
  813. `
  814. this._querySelector('ybplayer-slider').onchange = (e) => {
  815. this._isDrag = false
  816. this.video && this.seek((this._querySelector('ybplayer-slider').value / 100) * (this.duration || this.video.duration));
  817. this._start_controlsTimer();
  818. }
  819. this._querySelector('ybplayer-slider').oninput = (e) => {
  820. this._isDrag = true
  821. if ( this._querySelector('ybplayer-slider-focus') ) this._querySelector('ybplayer-slider-focus').style.width = this._querySelector('ybplayer-slider').value + '%'
  822. this._clear_controlsTimer();
  823. }
  824. }
  825. },
  826. enumerable:false
  827. })
  828. Object.defineProperty(YBPlayer.prototype,'_destroyProgress',{
  829. value:function(){
  830. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  831. throw new TypeError("TypeError: YBPlayer._destroyProgress is not a constructor")
  832. }
  833. if ( this._querySelector('ybplayer-controls-progress') ) {
  834. if ( this._querySelector('ybplayer-slider') ) this._querySelector('ybplayer-slider').onchange = null
  835. if ( this._querySelector('ybplayer-slider') ) this._querySelector('ybplayer-slider').oninput = null
  836. this._querySelector('ybplayer-controls-progress').innerHTML = ''
  837. }
  838. },
  839. enumerable:false
  840. })
  841. Object.defineProperty(YBPlayer.prototype,'_showLongPlaybackRate',{
  842. value:function(){
  843. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  844. throw new TypeError("TypeError: YBPlayer._showLongPlaybackRate is not a constructor")
  845. }
  846. if ( this._wrapperEl && this.video && !this.video.paused ) {
  847. this.video.playbackRate = this.longpressPlaybackRate
  848. this._longPlaybackRateEl = document.createElement('DIV')
  849. this._longPlaybackRateEl.setAttribute('style', 'position:absolute;top:20px;left:50%;transform:translateX(-50%);padding:8px 13px;border-radius:5px;background-color:rgba(0,0,0,.5);color:#fff;font-size:13px;')
  850. this._longPlaybackRateEl.innerHTML = `
  851. <i class="ybplayerIconfont icon-play ybplayer-icon ybplayer-long-playbackrate-1"></i>
  852. <i class="ybplayerIconfont icon-play ybplayer-icon ybplayer-long-playbackrate-2"></i>
  853. <i class="ybplayerIconfont icon-play ybplayer-icon ybplayer-long-playbackrate-3"></i>
  854. <span style="margin-left:5px">快速播放中</span>`
  855. this._wrapperEl.appendChild(this._longPlaybackRateEl)
  856. }
  857. },
  858. enumerable:false
  859. })
  860. Object.defineProperty(YBPlayer.prototype,'_hideLongPlaybackRate',{
  861. value:function(){
  862. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  863. throw new TypeError("TypeError: YBPlayer._showLongPlaybackRate is not a constructor")
  864. }
  865. if ( this._longPlaybackRateEl ) {
  866. this._longPlaybackRateEl.remove()
  867. this._longPlaybackRateEl = null
  868. }
  869. if ( this.video ) this.video.playbackRate = this.playbackRate
  870. },
  871. enumerable:false
  872. })
  873. Object.defineProperty(YBPlayer.prototype,'_clear_controlsTimer',{
  874. value:function(){
  875. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  876. throw new TypeError("TypeError: YBPlayer._clear_controlsTimer is not a constructor")
  877. }
  878. if ( this.controls && this._controlsTimer ) {
  879. window.clearTimeout(this._controlsTimer)
  880. this._controlsTimer = null
  881. }
  882. },
  883. enumerable:false
  884. })
  885. Object.defineProperty(YBPlayer.prototype,'_start_controlsTimer',{
  886. value:function(){
  887. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  888. throw new TypeError("TypeError: YBPlayer._start_controlsTimer is not a constructor")
  889. }
  890. if ( this.controls ) {
  891. this._controlsTimer = window.setTimeout(() => {
  892. this._hideControls()
  893. }, 5000)
  894. }
  895. },
  896. enumerable:false
  897. })
  898. Object.defineProperty(YBPlayer.prototype,'_initSlots',{
  899. value:function(){
  900. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  901. throw new TypeError("TypeError: YBPlayer._initSlots is not a constructor")
  902. }
  903. let nodes = this.container.childNodes;
  904. for ( let i = 0; i < nodes.length; i++ ) {
  905. if ( [].concat(nodes[i].classList).toString().indexOf('ybplayer-slots') > -1 ) {
  906. this._slotsEl = nodes[i]
  907. let childNodes = this._slotsEl.childNodes;
  908. for ( let j = 0; j < childNodes.length; j++ ) {
  909. if ( [].concat(childNodes[j].classList).toString().indexOf('ybplayer-controls-slots') > -1 ) {
  910. this._controlSlotsEl = childNodes[j];
  911. }
  912. }
  913. this._wrapperEl.appendChild(this._slotsEl);
  914. }
  915. }
  916. },
  917. enumerable:false
  918. })
  919. Object.defineProperty(YBPlayer.prototype,'_destroySlots',{
  920. value:function(){
  921. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  922. throw new TypeError("TypeError: YBPlayer._destroySlots is not a constructor")
  923. }
  924. if ( this._controlSlotsEl ) this._controlSlotsEl = null
  925. if ( this._slotsEl && this.container ) {
  926. this.container.appendChild(this._slotsEl)
  927. this._slotsEl = null
  928. }
  929. },
  930. enumerable:false
  931. })
  932. Object.defineProperty(YBPlayer.prototype,'_showSetting',{
  933. value:function(){
  934. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  935. throw new TypeError("TypeError: YBPlayer._showSetting is not a constructor")
  936. }
  937. if ( this.container ) {
  938. this._settingEl = document.createElement('DIV');
  939. this._settingEl.setAttribute('class', 'ybplayer-settings');
  940. let barrageHtml = '';
  941. let settings = this.settings.split(',').map(setting => setting.trim());
  942. try{
  943. barrageHtml = YBBarrage ? `
  944. <div class="${this._setClassName('ybplayer-setting')}" data-value="barrage">
  945. <i class="ybplayerIconfont icon-barrage-${this.barrageShow ? 'show' : 'hide'} ybplayer-setting-icon"></i>
  946. <p class="ybplayer-setting-text">${this.barrageShow ? '关闭弹幕' : '开启弹幕'}</p>
  947. </div>
  948. ` : '';
  949. }catch(e){
  950. }
  951. this._settingEl.innerHTML = `
  952. ${ this.settings == 'all' || settings.indexOf('barrage') > -1 ? barrageHtml : ''}
  953. ${ this.settings == 'all' || settings.indexOf('playbackRate') > -1 ? `
  954. <div class="${this._setClassName('ybplayer-setting')}" data-value="playbackRate">
  955. <i class="ybplayerIconfont icon-play-rate-circle ybplayer-setting-icon"></i>
  956. <p class="ybplayer-setting-text">播放速度</p>
  957. </div>
  958. ` : '' }
  959. ${ this.settings == 'all' || settings.indexOf('mirror') > -1 ? `
  960. <div class="${this._setClassName('ybplayer-setting')}" data-value="mirror">
  961. <i class="ybplayerIconfont icon-mirror ybplayer-setting-icon"></i>
  962. <p class="ybplayer-setting-text">${this.mirror ? '关闭镜像' : '镜像画面'}</p>
  963. </div>
  964. ` : '' }
  965. ${ this.settings == 'all' || settings.indexOf('capture') > -1 ? `
  966. <div class="${this._setClassName('ybplayer-setting')}" data-value="capture">
  967. <i class="ybplayerIconfont icon-screenshot ybplayer-setting-icon"></i>
  968. <p class="ybplayer-setting-text">截取画面</p>
  969. </div>
  970. ` : '' }
  971. ${ (this.settings == 'all' || settings.indexOf('pictureInPicture') > -1) && document.pictureInPictureEnabled ? `
  972. <div class="${this._setClassName('ybplayer-setting')}" data-value="pictureInPicture">
  973. <i class="ybplayerIconfont icon-picture-in-picture-${this.pictureInPicture ? 'open' : 'exit'} ybplayer-setting-icon"></i>
  974. <p class="ybplayer-setting-text">${this.pictureInPicture ? '退出画中画' : '画中画'}</p>
  975. </div>
  976. ` : '' }
  977. ${ (this.settings == 'all' || settings.indexOf('dlna') > -1) && false ? `
  978. <div class="${this._setClassName('ybplayer-setting')}" data-value="dlna">
  979. <i class="ybplayerIconfont icon-picture-in-picture-open ybplayer-setting-icon"></i>
  980. <p class="ybplayer-setting-text">开启投屏</p>
  981. </div>
  982. ` : '' }
  983. `;
  984. this.container.appendChild(this._settingEl);
  985. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  986. this._querySelectorAll('ybplayer-setting')[i].onclick = () => {
  987. let type = this._querySelectorAll('ybplayer-setting')[i].getAttribute('data-value');
  988. if ( type == 'barrage' ) {
  989. this.setConfig('barrageShow', !this.barrageShow);
  990. } else if ( type == 'playbackRate' ) {
  991. if ( this._playbackRateEl ) {
  992. this._hidePlaybackRate();
  993. } else {
  994. this._showPlaybackRate();
  995. }
  996. } else if ( type == 'mirror' ) {
  997. this.setConfig('mirror', !this.mirror);
  998. } else if ( type == 'capture' ) {
  999. this.capture();
  1000. this._hideSetting();
  1001. } else if ( type == 'pictureInPicture' ) {
  1002. this.setConfig('pictureInPicture', !this.pictureInPicture);
  1003. } else {
  1004. this._startSearchDlna()
  1005. }
  1006. }
  1007. }
  1008. }
  1009. },
  1010. enumerable:false
  1011. })
  1012. Object.defineProperty(YBPlayer.prototype,'_hideSetting',{
  1013. value:function(){
  1014. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1015. throw new TypeError("TypeError: YBPlayer._hideSetting is not a constructor")
  1016. }
  1017. if ( this.container && this._settingEl ) {
  1018. this._settingEl.setAttribute('class', 'ybplayer-settings ybplayer-settings-hide');
  1019. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  1020. this._querySelectorAll('ybplayer-setting')[i].onclick = null
  1021. }
  1022. window.setTimeout(() => {
  1023. this.container.removeChild(this._settingEl);
  1024. this._settingEl = null;
  1025. }, 300);
  1026. }
  1027. },
  1028. enumerable:false
  1029. })
  1030. Object.defineProperty(YBPlayer.prototype,'_setSettingView',{
  1031. value:function(value){
  1032. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1033. throw new TypeError("TypeError: YBPlayer._setSettingView is not a constructor")
  1034. }
  1035. if ( this._querySelector('ybplayer-setting') ) {
  1036. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  1037. if ( this._querySelectorAll('ybplayer-setting')[i].getAttribute('data-value') == value ) {
  1038. if ( value == 'barrage' ) {
  1039. this._querySelectorAll('ybplayer-setting')[i].innerHTML = `
  1040. <i class="ybplayerIconfont icon-barrage-${this.barrageShow ? 'show' : 'hide'} ybplayer-setting-icon"></i>
  1041. <p class="ybplayer-setting-text">${this.barrageShow ? '关闭弹幕' : '开启弹幕'}</p>
  1042. `;
  1043. } else if ( value == 'mirror' ) {
  1044. this._querySelectorAll('ybplayer-setting')[i].innerHTML = `
  1045. <i class="ybplayerIconfont icon-mirror ybplayer-setting-icon"></i>
  1046. <p class="ybplayer-setting-text">${this.mirror ? '关闭镜像' : '镜像画面'}</p>
  1047. `;
  1048. } else if ( value == 'pictureInPicture' ) {
  1049. this._querySelectorAll('ybplayer-setting')[i].innerHTML = `
  1050. <i class="ybplayerIconfont icon-picture-in-picture-${this.pictureInPicture ? 'open' : 'exit'} ybplayer-setting-icon"></i>
  1051. <p class="ybplayer-setting-text">${this.pictureInPicture ? '退出画中画' : '画中画'}</p>
  1052. `;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. },
  1058. enumerable:false
  1059. })
  1060. Object.defineProperty(YBPlayer.prototype,'_startSearchDlna',{
  1061. value:function(){
  1062. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1063. throw new TypeError("TypeError: YBPlayer._startSearchDlna is not a constructor")
  1064. }
  1065. try{
  1066. jsBridge.dlna.search(function(succ, data) {
  1067. console.log('succ', succ);
  1068. console.log('data', data);
  1069. });
  1070. }catch(e){
  1071. //TODO handle the exception
  1072. }
  1073. },
  1074. enumerable:false
  1075. })
  1076. Object.defineProperty(YBPlayer.prototype,'_showPlaybackRate',{
  1077. value:function(){
  1078. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1079. throw new TypeError("TypeError: YBPlayer._showPlaybackRate is not a constructor")
  1080. }
  1081. if ( this.container ) {
  1082. this._hideSetting();
  1083. this._playbackRateEl = document.createElement('DIV');
  1084. this._playbackRateEl.setAttribute('class', 'ybplayer-settings');
  1085. let playbackRates = [{
  1086. label: '0.25',
  1087. value: 0.25
  1088. },{
  1089. label: '0.5',
  1090. value: 0.5
  1091. },{
  1092. label: '0.75',
  1093. value: 0.75
  1094. },{
  1095. label: '正常',
  1096. value: 1.0
  1097. },{
  1098. label: '1.25',
  1099. value: 1.25
  1100. },{
  1101. label: '1.5',
  1102. value: 1.5
  1103. },{
  1104. label: '1.75',
  1105. value: 1.75
  1106. },{
  1107. label: '2.0',
  1108. value: 2.0
  1109. }];
  1110. for ( let rate of playbackRates ) {
  1111. this._playbackRateEl.innerHTML += `
  1112. <div class="${this._setClassName('ybplayer-setting')}" data-rate="${rate.value}">
  1113. <p class="ybplayer-setting-text" style="color: ${rate.value == this.playbackRate ? '#2ca2f9' : '#333'};">${rate.label}</p>
  1114. </div>
  1115. `;
  1116. }
  1117. this.container.appendChild(this._playbackRateEl);
  1118. let that = this
  1119. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  1120. this._querySelectorAll('ybplayer-setting')[i].onclick = function () {
  1121. that.setConfig('playbackRate', this.getAttribute('data-rate'))
  1122. }
  1123. }
  1124. }
  1125. },
  1126. enumerable:false
  1127. })
  1128. Object.defineProperty(YBPlayer.prototype,'_hidePlaybackRate',{
  1129. value:function(){
  1130. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1131. throw new TypeError("TypeError: YBPlayer._hidePlaybackRate is not a constructor")
  1132. }
  1133. if ( this.container && this._playbackRateEl ) {
  1134. this._playbackRateEl.setAttribute('class', 'ybplayer-settings ybplayer-settings-hide');
  1135. for ( let i = 0; i < this._querySelectorAll('ybplayer-setting').length; i++ ) {
  1136. this._querySelectorAll('ybplayer-setting')[i].onclick = null
  1137. }
  1138. window.setTimeout(() => {
  1139. this.container.removeChild(this._playbackRateEl);
  1140. this._playbackRateEl = null;
  1141. this._showSetting();
  1142. }, 300);
  1143. }
  1144. },
  1145. enumerable:false
  1146. })
  1147. Object.defineProperty(YBPlayer.prototype,'_initBarrage',{
  1148. value:function(){
  1149. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1150. throw new TypeError("TypeError: YBPlayer._initBarrage is not a constructor")
  1151. }
  1152. try{
  1153. this.barrage = new YBBarrage({
  1154. container: this._barrageEl,
  1155. barrages: this.barrages,
  1156. config: Object.assign({}, this.barrageConfig, {
  1157. initialTime: this.video ? this.video.currentTime : 0
  1158. })
  1159. });
  1160. if ( !this.video.paused ) this.barrage.play();
  1161. this._setSettingView('barrage');
  1162. this._eventCallback.barrageChange && this._eventCallback.barrageChange({
  1163. show: true
  1164. })
  1165. }catch(e){
  1166. //TODO handle the exception
  1167. }
  1168. },
  1169. enumerable:false
  1170. })
  1171. Object.defineProperty(YBPlayer.prototype,'_destroyBarrage',{
  1172. value:function(){
  1173. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1174. throw new TypeError("TypeError: YBPlayer._destroyBarrage is not a constructor")
  1175. }
  1176. if ( this.barrage ) {
  1177. this.barrage.stop();
  1178. this.barrage = null;
  1179. this._setSettingView('barrage');
  1180. this._eventCallback.barrageChange && this._eventCallback.barrageChange({
  1181. show: false
  1182. })
  1183. }
  1184. },
  1185. enumerable:false
  1186. })
  1187. Object.defineProperty(YBPlayer.prototype,'_setConfig',{
  1188. value:function(attribute, value){
  1189. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1190. throw new TypeError("TypeError: YBPlayer._setConfig is not a constructor")
  1191. }
  1192. const videoAttributes = ['autoplay', 'loop', 'muted', 'preload', 'volume', 'playbackRate', 'crossOrigin'];
  1193. if ( this.video && videoAttributes.includes(attribute) ) this.video[attribute] = value;
  1194. if ( this.video && attribute == 'objectFit' ) this.video.style.objectFit = value;
  1195. if ( this.video && attribute == 'mirror' ) {
  1196. this.video.style.transform = 'rotateY(' + (value ? 180 : 0) + 'deg)';
  1197. this._setSettingView('mirror');
  1198. }
  1199. if ( this.video && attribute == 'pictureInPicture' ) {
  1200. if ( document.pictureInPictureEnabled ) {
  1201. if ( value )
  1202. this.video.requestPictureInPicture();
  1203. else
  1204. document.pictureInPictureElement && document.exitPictureInPicture();
  1205. }
  1206. }
  1207. if ( attribute == 'controls' ) {
  1208. if ( value )
  1209. this._initControls()
  1210. else
  1211. this._destroyControls()
  1212. }
  1213. if ( attribute == 'prevBtnShow' ) {
  1214. this._setControlsView('prevBtn')
  1215. }
  1216. if ( attribute == 'nextBtnShow' ) {
  1217. this._setControlsView('nextBtn')
  1218. }
  1219. if ( attribute == 'timeShow' ) {
  1220. this._setControlsView('timeUpdate')
  1221. }
  1222. if ( attribute == 'settingShow' ) {
  1223. this._setControlsView('setting')
  1224. }
  1225. if ( attribute == 'progressShow' ) {
  1226. if ( value )
  1227. this._initProgress()
  1228. else
  1229. this._destroyProgress()
  1230. }
  1231. if ( attribute == 'barrageShow' ) {
  1232. if ( value )
  1233. this._initBarrage()
  1234. else
  1235. this._destroyBarrage()
  1236. }
  1237. if ( attribute == 'barrageConfig' ) {
  1238. this.barrage && this.barrage.setConfig(value);
  1239. }
  1240. if ( attribute == 'barrageGap' ) {
  1241. this.barrage && this.barrage.refresh();
  1242. }
  1243. if ( attribute == 'title' ) {
  1244. if ( value ) {
  1245. if ( this._querySelector('ybplayer-headers-title') ) {
  1246. this._querySelector('ybplayer-headers-title').innerHTML = value;
  1247. } else {
  1248. this._initHeaders();
  1249. }
  1250. } else {
  1251. this._destroyHeaders();
  1252. }
  1253. }
  1254. if ( attribute == 'src' || attribute == 'segments' ) {
  1255. this._destroy(false)
  1256. this._init()
  1257. // this._destroyHls()
  1258. // this._destroyFlv();
  1259. // this._destroyControls();
  1260. // this._destroyBarrage();
  1261. // this._destroyHeaders();
  1262. // this._destroyVideo();
  1263. // this._destroySlots();
  1264. // this._initVideo();
  1265. // this._initSlots();
  1266. // this._setVideoUrl();
  1267. // if ( this.fullscreen ) this._initHeaders();
  1268. // if ( this.controls ) this._initControls();
  1269. }
  1270. },
  1271. enumerable:false
  1272. })
  1273. //获取后缀
  1274. Object.defineProperty(YBPlayer.prototype,'_suffix',{
  1275. value:function(name){
  1276. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1277. throw new TypeError("TypeError: YBPlayer._suffix is not a constructor")
  1278. }
  1279. let fileName = name.lastIndexOf(".");
  1280. let fileNameLength = name.length;
  1281. let fileFormat = name.substring(fileName + 1, fileNameLength);
  1282. return fileFormat;
  1283. },
  1284. enumerable:false
  1285. })
  1286. //格式化时间
  1287. Object.defineProperty(YBPlayer.prototype,'_timesFormat',{
  1288. value:function(value){
  1289. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1290. throw new TypeError("TypeError: YBPlayer._timesFormat is not a constructor")
  1291. }
  1292. let hours = Math.floor(value / 60 / 60 % 60) >= 10 ? Math.floor(value / 60 / 60 % 60) : '0' + Math.floor(value / 60 / 60 % 60);
  1293. let minutes = Math.floor(value / 60 % 60) >= 10 ? Math.floor(value / 60 % 60) : '0' + Math.floor(value / 60 % 60);
  1294. let seconds = Math.floor(value % 60) >= 10 ? Math.floor(value % 60) : '0' + Math.floor(value % 60);
  1295. return hours == '00' ? (minutes + ':' + seconds) : (hours + ':' + minutes + ':' + seconds);
  1296. },
  1297. enumerable:false
  1298. })
  1299. //深度克隆
  1300. Object.defineProperty(YBPlayer.prototype,'_deepClone',{
  1301. value:function(obj){
  1302. if(typeof obj !== "object" && typeof obj !== 'function') {
  1303. //原始类型直接返回
  1304. return obj;
  1305. }
  1306. var o = Object.prototype.toString.call(obj) === '[object Array]' ? [] : {};
  1307. for(let i in obj) {
  1308. if(obj.hasOwnProperty(i)){
  1309. o[i] = i === 'loader' ? '' : Object.prototype.toString.call( obj[i] ) === '[object XMLHttpRequest]' ? "" : typeof obj[i] === "object" ? this._deepClone(obj[i]) : obj[i];
  1310. }
  1311. }
  1312. return o;
  1313. },
  1314. enumerable:false
  1315. })
  1316. Object.defineProperty(YBPlayer.prototype,'_setClassName',{
  1317. value:function(className){
  1318. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1319. throw new TypeError("TypeError: YBPlayer._setClassName is not a constructor")
  1320. }
  1321. return this.container.id + '_' + className + ' ' + className
  1322. },
  1323. enumerable:false
  1324. })
  1325. Object.defineProperty(YBPlayer.prototype,'_querySelector',{
  1326. value:function(className){
  1327. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1328. throw new TypeError("TypeError: YBPlayer._querySelector is not a constructor")
  1329. }
  1330. return this.container && document.getElementsByClassName(this.container.id + '_' + className) && document.getElementsByClassName(this.container.id + '_' + className).length > 0 ? document.getElementsByClassName(this.container.id + '_' + className)[0] : null
  1331. },
  1332. enumerable:false
  1333. })
  1334. Object.defineProperty(YBPlayer.prototype,'_querySelectorAll',{
  1335. value:function(className){
  1336. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1337. throw new TypeError("TypeError: YBPlayer._querySelectorAll is not a constructor")
  1338. }
  1339. return this.container ? document.getElementsByClassName(this.container.id + '_' + className) || [] : []
  1340. },
  1341. enumerable:false
  1342. })
  1343. Object.defineProperty(YBPlayer.prototype,'_setVideoUrl',{
  1344. value:function(){
  1345. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1346. throw new TypeError("TypeError: YBPlayer._setVideoUrl is not a constructor")
  1347. }
  1348. if ( this.src ) {
  1349. let formats = '';
  1350. if ( this.formats == 'auto' )
  1351. formats = this._suffix(this.src.split('?')[0]);
  1352. else
  1353. formats = this.formats;
  1354. if ( formats == 'm3u8' )
  1355. this._setM3u8();
  1356. else if ( formats == 'flv' )
  1357. this._setFlv();
  1358. else
  1359. this._setBlob();
  1360. } else if ( this.segments ) {
  1361. this._setFlv();
  1362. }
  1363. },
  1364. enumerable:false
  1365. })
  1366. Object.defineProperty(YBPlayer.prototype,'_setBlob',{
  1367. value:function(){
  1368. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1369. throw new TypeError("TypeError: YBPlayer._setBlob is not a constructor")
  1370. }
  1371. if ( this.enableBlob ) {
  1372. let xhr = new XMLHttpRequest()
  1373. xhr.open('GET', this.src, true)
  1374. xhr.responseType = 'blob'
  1375. xhr.onload = () => {
  1376. if ( xhr.status == 200 ) {
  1377. try {
  1378. this.video.srcObject = xhr.response
  1379. } catch (error) {
  1380. const URL = window.URL || window.webkitURL
  1381. this.video.src = URL.createObjectURL(xhr.response);
  1382. }
  1383. } else {
  1384. this.video.src = this.src
  1385. }
  1386. // this.video.load()
  1387. xhr.abort()
  1388. xhr = null
  1389. }
  1390. xhr.onerror = () => {
  1391. this.video.src = this.src
  1392. // this.video.load()
  1393. xhr = null
  1394. }
  1395. xhr.send()
  1396. } else {
  1397. this.video.src = this.src
  1398. // this.video.load()
  1399. }
  1400. },
  1401. enumerable:false
  1402. })
  1403. Object.defineProperty(YBPlayer.prototype,'_setM3u8',{
  1404. value:function(){
  1405. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1406. throw new TypeError("TypeError: YBPlayer._setM3u8 is not a constructor")
  1407. }
  1408. try{
  1409. if ( Hls.isSupported() ) {
  1410. this.hls = new Hls();
  1411. this.hls.loadSource(this.src);
  1412. this.hls.attachMedia(this.video);
  1413. // this.hls.on(Hls.Events.MANIFEST_PARSED, (e) => { });
  1414. this.hls.on(Hls.Events.ERROR, (e, data) => {
  1415. this._eventCallback.error && this._eventCallback.error(this._deepClone(data));
  1416. });
  1417. } else {
  1418. this.video.src = this.src;
  1419. // this.video.load();
  1420. }
  1421. }catch(e){
  1422. this._eventCallback.error && this._eventCallback.error(this._deepClone(e))
  1423. }
  1424. },
  1425. enumerable:false
  1426. })
  1427. Object.defineProperty(YBPlayer.prototype,'_destroyHls',{
  1428. value:function(){
  1429. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1430. throw new TypeError("TypeError: YBPlayer._destroyHls is not a constructor")
  1431. }
  1432. if (this.hls) {
  1433. this.hls.destroy();
  1434. this.hls = null;
  1435. }
  1436. },
  1437. enumerable:false
  1438. })
  1439. Object.defineProperty(YBPlayer.prototype,'_setFlv',{
  1440. value:function(){
  1441. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1442. throw new TypeError("TypeError: YBPlayer._setFlv is not a constructor")
  1443. }
  1444. try{
  1445. if (flvjs.isSupported()) {
  1446. this.flv = flvjs.createPlayer(
  1447. {
  1448. type: "flv",
  1449. isLive: this.isLive,
  1450. url: this.src,
  1451. segments: this.segment && this.segments.length > 0 ? this.segment : null,
  1452. cors: this.flvConfig.cors,
  1453. withCredentials: this.flvConfig.withCredentials,
  1454. hasAudio: this.flvConfig.hasAudio,
  1455. hasVideo: this.flvConfig.hasVideo,
  1456. duration: this.flvConfig.duration,
  1457. filesize: this.flvConfig.filesize
  1458. },
  1459. Object.assign({}, {
  1460. enableWorker: false, //不启用分离线程
  1461. enableStashBuffer: false, //关闭IO隐藏缓冲区
  1462. isLive: true,
  1463. lazyLoad: false
  1464. }, this.flvConfig)
  1465. )
  1466. } else {
  1467. this.video.src = this.src;
  1468. // this.video.load();
  1469. }
  1470. this.flv.on('error', this._flvError.bind(this));
  1471. this.flv.on("statistics_info", this._statisticsInfo.bind(this));
  1472. this.flv.on("loading_complete", this._loadingComplete.bind(this));
  1473. this.flv.on("recovered_early_eof", this._recoveredEarlyEof.bind(this));
  1474. this.flv.on("media_info", this._mediaInfo.bind(this));
  1475. this.flv.on("metadata_arrived", this._metadataArrived.bind(this));
  1476. this.flv.on("scriptdata_arrived", this._scriptdataArrived.bind(this));
  1477. this.flv.attachMediaElement(this.video);
  1478. this.flv.load();
  1479. }catch(e){
  1480. this._flvError(e)
  1481. }
  1482. },
  1483. enumerable:false
  1484. })
  1485. Object.defineProperty(YBPlayer.prototype,'_destroyFlv',{
  1486. value:function(){
  1487. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1488. throw new TypeError("TypeError: YBPlayer._destroyFlv is not a constructor")
  1489. }
  1490. if (this.flv) {
  1491. this.flv.off('error', this._flvError)
  1492. this.flv.off('statistics_info', this._statisticsInfo.bind(this))
  1493. this.flv.off('loading_complete', this._loadingComplete.bind(this))
  1494. this.flv.off("recovered_early_eof", this._recoveredEarlyEof.bind(this));
  1495. this.flv.off("media_info", this._mediaInfo.bind(this));
  1496. this.flv.off("metadata_arrived", this._metadataArrived.bind(this));
  1497. this.flv.off("scriptdata_arrived", this._scriptdataArrived.bind(this));
  1498. this.flv.pause();
  1499. this.flv.unload();
  1500. this.flv.detachMediaElement();
  1501. this.flv.destroy();
  1502. this.flv= null;
  1503. }
  1504. },
  1505. enumerable:false
  1506. })
  1507. Object.defineProperty(YBPlayer.prototype,'_flvError',{
  1508. value:function(e){
  1509. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1510. throw new TypeError("TypeError: YBPlayer._flvError is not a constructor")
  1511. }
  1512. this._eventCallback.error && this._eventCallback.error(this._deepClone(e));
  1513. },
  1514. enumerable:false
  1515. })
  1516. Object.defineProperty(YBPlayer.prototype,'_statisticsInfo',{
  1517. value:function(e){
  1518. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1519. throw new TypeError("TypeError: YBPlayer._statisticsInfo is not a constructor")
  1520. }
  1521. this._eventCallback.statisticsInfo && this._eventCallback.statisticsInfo(this._deepClone(e));
  1522. },
  1523. enumerable:false
  1524. })
  1525. Object.defineProperty(YBPlayer.prototype,'_loadingComplete',{
  1526. value:function(e){
  1527. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1528. throw new TypeError("TypeError: YBPlayer._loadingComplete is not a constructor")
  1529. }
  1530. this._eventCallback.loadingComplete && this._eventCallback.loadingComplete(this._deepClone(e));
  1531. },
  1532. enumerable:false
  1533. })
  1534. Object.defineProperty(YBPlayer.prototype,'_recoveredEarlyEof',{
  1535. value:function(e){
  1536. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1537. throw new TypeError("TypeError: YBPlayer._recoveredEarlyEof is not a constructor")
  1538. }
  1539. this._eventCallback.recoveredEarlyEof && this._eventCallback.recoveredEarlyEof(this._deepClone(e));
  1540. },
  1541. enumerable:false
  1542. })
  1543. Object.defineProperty(YBPlayer.prototype,'_mediaInfo',{
  1544. value:function(e){
  1545. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1546. throw new TypeError("TypeError: YBPlayer._mediaInfo is not a constructor")
  1547. }
  1548. this._eventCallback.mediaInfo && this._eventCallback.mediaInfo(this._deepClone(e));
  1549. },
  1550. enumerable:false
  1551. })
  1552. Object.defineProperty(YBPlayer.prototype,'_metadataArrived',{
  1553. value:function(e){
  1554. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1555. throw new TypeError("TypeError: YBPlayer._metadataArrived is not a constructor")
  1556. }
  1557. this._eventCallback.metadataArrived && this._eventCallback.metadataArrived(this._deepClone(e));
  1558. },
  1559. enumerable:false
  1560. })
  1561. Object.defineProperty(YBPlayer.prototype,'_scriptdataArrived',{
  1562. value:function(e){
  1563. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1564. throw new TypeError("TypeError: YBPlayer._scriptdataArrived is not a constructor")
  1565. }
  1566. this._eventCallback.scriptdataArrived && this._eventCallback.scriptdataArrived(this._deepClone(e));
  1567. },
  1568. enumerable:false
  1569. })
  1570. Object.defineProperty(YBPlayer.prototype,'_fullscreenerror',{
  1571. value:function(){
  1572. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1573. throw new TypeError("TypeError: YBPlayer._fullscreenerror is not a constructor")
  1574. }
  1575. if ( this.container ) {
  1576. this.container.style.position = 'fixed';
  1577. this.container.style.left = 0;
  1578. this.container.style.right = 0;
  1579. this.container.style.bottom = 0;
  1580. this.container.style.top = 0;
  1581. this.container.style.width = '100vw';
  1582. this.container.style.height = '100vh';
  1583. this.container.style.zIndex = 999;
  1584. this._videoBackground = this.container.style.background
  1585. this.container.style.background = '#000'
  1586. this._cssfullscreenchange();
  1587. }
  1588. },
  1589. enumerable:false
  1590. })
  1591. Object.defineProperty(YBPlayer.prototype,'_fullscreenchanged',{
  1592. value:function(){
  1593. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1594. throw new TypeError("TypeError: YBPlayer._fullscreenchanged is not a constructor")
  1595. }
  1596. if ( document.fullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || document.webkitFullscreenElement ) {
  1597. this._eventCallback.fullscreenChange && this._eventCallback.fullscreenChange({
  1598. fullscreen: true,
  1599. type: 'requestfullscreen'
  1600. });
  1601. this.fullscreen = true;
  1602. this._initHeaders();
  1603. } else {
  1604. this._eventCallback.fullscreenChange && this._eventCallback.fullscreenChange({
  1605. fullscreen: false,
  1606. type: 'requestfullscreen'
  1607. });
  1608. this.fullscreen = false;
  1609. this._destroyHeaders();
  1610. }
  1611. this._setControlsView('fullscreen');
  1612. this.refreshBarrage();
  1613. },
  1614. enumerable:false
  1615. })
  1616. Object.defineProperty(YBPlayer.prototype,'_cssfullscreenchange',{
  1617. value:function(){
  1618. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1619. throw new TypeError("TypeError: YBPlayer._cssfullscreenchange is not a constructor")
  1620. }
  1621. if ( this.container && this.container.style.position == 'fixed' ) {
  1622. this._eventCallback.fullscreenChange && this._eventCallback.fullscreenChange({
  1623. fullscreen: true,
  1624. type: 'cssfullscreen'
  1625. });
  1626. this.fullscreen = true;
  1627. this._initHeaders();
  1628. } else {
  1629. this._eventCallback.fullscreenChange && this._eventCallback.fullscreenChange({
  1630. fullscreen: false,
  1631. type: 'cssfullscreen'
  1632. });
  1633. this.fullscreen = false;
  1634. this._destroyHeaders();
  1635. }
  1636. this._setControlsView('fullscreen');
  1637. this.refreshBarrage();
  1638. },
  1639. enumerable:false
  1640. })
  1641. Object.defineProperty(YBPlayer.prototype,'on',{
  1642. value:function(event, callback){
  1643. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1644. throw new TypeError("TypeError: YBPlayer.on is not a constructor")
  1645. }
  1646. this._eventCallback[event] = callback;
  1647. },
  1648. enumerable:false
  1649. })
  1650. Object.defineProperty(YBPlayer.prototype,'play',{
  1651. value:function(){
  1652. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1653. throw new TypeError("TypeError: YBPlayer.play is not a constructor")
  1654. }
  1655. this.video && this.video.play()
  1656. },
  1657. enumerable:false
  1658. })
  1659. Object.defineProperty(YBPlayer.prototype,'pause',{
  1660. value:function(){
  1661. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1662. throw new TypeError("TypeError: YBPlayer.pause is not a constructor")
  1663. }
  1664. this.video && this.video.pause()
  1665. },
  1666. enumerable:false
  1667. })
  1668. Object.defineProperty(YBPlayer.prototype,'toggle',{
  1669. value:function(){
  1670. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1671. throw new TypeError("TypeError: YBPlayer.toggle is not a constructor")
  1672. }
  1673. this.video && this.video.paused ? this.video.play() : this.video.pause()
  1674. },
  1675. enumerable:false
  1676. })
  1677. Object.defineProperty(YBPlayer.prototype,'seek',{
  1678. value:function(time){
  1679. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1680. throw new TypeError("TypeError: YBPlayer.seek is not a constructor")
  1681. }
  1682. if ( this.video ) this.video.currentTime = time
  1683. },
  1684. enumerable:false
  1685. })
  1686. Object.defineProperty(YBPlayer.prototype,'reload',{
  1687. value:function(){
  1688. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1689. throw new TypeError("TypeError: YBPlayer.reload is not a constructor")
  1690. }
  1691. this._destroy(false)
  1692. this._init()
  1693. },
  1694. enumerable:false
  1695. })
  1696. Object.defineProperty(YBPlayer.prototype,'stop',{
  1697. value:function(){
  1698. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1699. throw new TypeError("TypeError: YBPlayer.stop is not a constructor")
  1700. }
  1701. this._destroy()
  1702. },
  1703. enumerable:false
  1704. })
  1705. Object.defineProperty(YBPlayer.prototype,'capture',{
  1706. value:function(){
  1707. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1708. throw new TypeError("TypeError: YBPlayer.capture is not a constructor")
  1709. }
  1710. const canvas = document.createElement('canvas');
  1711. canvas.width = this.video.videoWidth;
  1712. canvas.height = this.video.videoHeight;
  1713. canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height);
  1714. canvas.toBlob(blob => {
  1715. this._eventCallback.captureFinish && this._eventCallback.captureFinish({
  1716. blob: blob,
  1717. base64: canvas.toDataURL('image/jpg')
  1718. })
  1719. }, 'image/jpg')
  1720. },
  1721. enumerable:false
  1722. })
  1723. Object.defineProperty(YBPlayer.prototype,'drawBarrage',{
  1724. value:function(barrage){
  1725. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1726. throw new TypeError("TypeError: YBPlayer.drawBarrage is not a constructor")
  1727. }
  1728. this.barrage && this.barrage.add(barrage);
  1729. },
  1730. enumerable:false
  1731. })
  1732. Object.defineProperty(YBPlayer.prototype,'refreshBarrage',{
  1733. value:function(){
  1734. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1735. throw new TypeError("TypeError: YBPlayer.refreshBarrage is not a constructor")
  1736. }
  1737. this.barrage && this.barrage.refresh();
  1738. },
  1739. enumerable:false
  1740. })
  1741. Object.defineProperty(YBPlayer.prototype,'setBarrages',{
  1742. value:function(barrages){
  1743. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1744. throw new TypeError("TypeError: YBPlayer.setBarrages is not a constructor")
  1745. }
  1746. this.barrages = barrages;
  1747. this.barrage && this.barrage.setBarrages(barrages);
  1748. },
  1749. enumerable:false
  1750. })
  1751. Object.defineProperty(YBPlayer.prototype,'setConfig',{
  1752. value:function(attribute, value){
  1753. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1754. throw new TypeError("TypeError: YBPlayer.setConfig is not a constructor")
  1755. }
  1756. this[attribute] = value;
  1757. this._setConfig(attribute, value);
  1758. },
  1759. enumerable:false
  1760. })
  1761. Object.defineProperty(YBPlayer.prototype,'switchFullscreen',{
  1762. value:function(){
  1763. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1764. throw new TypeError("TypeError: YBPlayer.switchFullscreen is not a constructor")
  1765. }
  1766. if ( document.fullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || document.webkitFullscreenElement || (this.container && this.container.style.position == 'fixed') ) {
  1767. this.exitFullscreen()
  1768. } else {
  1769. this.lanuchFullscreen()
  1770. }
  1771. },
  1772. enumerable:false
  1773. })
  1774. Object.defineProperty(YBPlayer.prototype,'lanuchFullscreen',{
  1775. value:function(){
  1776. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1777. throw new TypeError("TypeError: YBPlayer.lanuchFullscreen is not a constructor")
  1778. }
  1779. if ( document.fullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || document.webkitFullscreenElement ) return
  1780. const rfs = document.documentElement.requestFullscreen || document.documentElement.webkitRequestFullscreen || document.documentElement.mozRequestFullscreen || document.documentElement.requestFullScreen || document.documentElement.webkitRequestFullScreen || document.documentElement.mozRequestFullScreen
  1781. if ( typeof rfs != 'undefined' && rfs && this.container ) {
  1782. rfs.call(this.container)
  1783. } else if (typeof window.ActiveXObject !== "undefined") {
  1784. //IE浏览器,模拟按下F11全屏
  1785. var wscript = new ActiveXObject("WScript.Shell");
  1786. if (wscript != null) {
  1787. wscript.SendKeys("{F11}");
  1788. }
  1789. } else {
  1790. this._fullscreenerror()
  1791. }
  1792. },
  1793. enumerable:false
  1794. })
  1795. Object.defineProperty(YBPlayer.prototype,'exitFullscreen',{
  1796. value:function(){
  1797. if(!(this instanceof YBPlayer)){//那么相反 不是正常调用的就是错误的调用
  1798. throw new TypeError("TypeError: YBPlayer.exitFullscreen is not a constructor")
  1799. }
  1800. if ( document.fullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || document.webkitFullscreenElement ) {
  1801. const cfs = document.exitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen || document.webkitExitFullscreen;
  1802. if ( typeof cfs != 'undefined' && cfs ) {
  1803. cfs.call(document)
  1804. }
  1805. } else if (typeof window.ActiveXObject !== "undefined") {
  1806. //IE浏览器,模拟按下F11键退出全屏
  1807. var wscript = new ActiveXObject("WScript.Shell");
  1808. if (wscript != null) {
  1809. wscript.SendKeys("{F11}");
  1810. }
  1811. } else {
  1812. if ( this.container && this.container.style.position == 'fixed' ) {
  1813. this.container.style.position = 'relative';
  1814. this.container.style.width = '100%';
  1815. this.container.style.height = '100%';
  1816. this.container.style.top = 'inherit';
  1817. this.container.style.left = 'inherit';
  1818. this.container.style.right = 'inherit';
  1819. this.container.style.bottom = 'inherit';
  1820. this.container.style.zIndex = 'inherit';
  1821. this.container.style.background = this._videoBackground
  1822. this._videoBackground = 'inherit'
  1823. this._cssfullscreenchange();
  1824. }
  1825. }
  1826. },
  1827. enumerable:false
  1828. })