PlayerItem.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * PlayerItem
  3. */
  4. class PlayerItem {
  5. /**
  6. * @param {*} opt.wrapperDomId 父级id
  7. * @param {*} opt.index 索引
  8. */
  9. constructor(opt) {
  10. // dom
  11. this.$el = null
  12. // 播放用元素
  13. this.canvasElem = null
  14. this.videoElem = null
  15. // 每个组件的dom唯一 id
  16. this.domId = opt.wrapperDomId + '-' + opt.index
  17. // 所属的wsplayer
  18. this.wsPlayer = opt.wsPlayer
  19. // index序号
  20. this.index = opt.index
  21. // 第一帧事件
  22. this.firstTime = 0
  23. // audioOn
  24. this.isAudioPlay = false
  25. }
  26. /**
  27. * @param {*} domId 此播放器的id
  28. */
  29. initDom() {
  30. let template = this.getTemplate()
  31. let player = $(template)
  32. this.wsPlayer.$wrapper.append(player[0])
  33. this.$el = $('#' + this.domId)
  34. this.canvasElem = document.getElementById(this.canvasId)
  35. this.videoElem = document.getElementById(this.videoId)
  36. }
  37. // 添加监听
  38. initMouseEvent() {
  39. this.$el.click((evt) => {
  40. this.wsPlayer.setSelectIndex(this.index)
  41. this.$el.siblings().removeClass('selected').addClass('unselected')
  42. this.$el.removeClass('unselected').addClass('selected')
  43. })
  44. this.$el.dblclick((evt) => {
  45. if (this.wsPlayer.$wrapper.hasClass('fullplayer')) {
  46. this.wsPlayer.setPlayerNum(4)
  47. } else {
  48. this.wsPlayer.setPlayerNum(1)
  49. }
  50. // this.wsPlayer.$wrapper.toggleClass('fullplayer')
  51. this.wsPlayer.setSelectIndex(this.index)
  52. this.$el.siblings().removeClass('selected').addClass('unselected')
  53. this.$el.removeClass('unselected').addClass('selected')
  54. })
  55. $('.end-icon', this.$el).on('click', () => {
  56. this.close()
  57. })
  58. $('.audio-icon', this.$el).click((evt) => {
  59. if (this.isAudioPlay) {
  60. // 正在播放,关闭声音
  61. this.player.setAudioVolume(0);
  62. $(evt.target).removeClass('on').addClass('off')
  63. } else {
  64. // 未播放,打开声音
  65. this.player.setAudioVolume(1);
  66. $(evt.target).removeClass('off').addClass('on')
  67. }
  68. this.isAudioPlay = !this.isAudioPlay
  69. })
  70. $('.capture-icon', this.$el).click((evt) => {
  71. this.player.capture(`抓图-${Date.now()}`);
  72. })
  73. $('.close-icon', this.$el).click((evt) => {
  74. this.player.stop()
  75. this.player.close()
  76. this.setStatus('closed')
  77. })
  78. }
  79. // 设置状态
  80. setStatus() {}
  81. /**
  82. * 关闭视频
  83. */
  84. play() {
  85. this.player.play()
  86. this.setStatus('playing')
  87. }
  88. /**
  89. * 关闭视频
  90. */
  91. pause() {
  92. this.player.pause()
  93. this.setStatus('pause')
  94. }
  95. /**
  96. * 关闭视频
  97. */
  98. close() {
  99. this.player.stop()
  100. this.player.close()
  101. this.setStatus('closed')
  102. }
  103. // 设置元素是否可见
  104. setDomVisible(dom, visible) {
  105. dom.css({
  106. visibility: visible ? 'visible' : 'hidden'
  107. })
  108. }
  109. }
  110. export default PlayerItem