player.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div class="video-player">
  3. <video class="video-js vjs-custom-skin"></video>
  4. </div>
  5. </template>
  6. <script>
  7. window.videojs = require('video.js')
  8. require('video.js/dist/video-js.css')
  9. var languages = require('./languages.js')
  10. export default {
  11. name: 'video-player',
  12. props: {
  13. options: {
  14. type: Object,
  15. required: true
  16. }
  17. },
  18. mounted() {
  19. if (!this.player) {
  20. this.initialize()
  21. }
  22. },
  23. beforeDestroy() {
  24. if (this.player) {
  25. this.dispose()
  26. }
  27. },
  28. methods: {
  29. initialize() {
  30. // init
  31. var self = this
  32. this.player = null
  33. // options
  34. var videoOptions = Object.assign({
  35. // component options
  36. start: 0,
  37. playsinline: false,
  38. customEventName: 'statechanged',
  39. // videojs options
  40. autoplay: false,
  41. controls: true,
  42. preload: 'auto',
  43. fluid: false,
  44. muted: false,
  45. width: '100%',
  46. height: '360',
  47. language: 'en',
  48. controlBar: {
  49. remainingTimeDisplay: false,
  50. playToggle: {},
  51. progressControl: {},
  52. fullscreenToggle: {},
  53. volumeMenuButton: {
  54. inline: false,
  55. vertical: true
  56. }
  57. },
  58. techOrder: ['html5', 'flash'],
  59. playbackRates: [],
  60. plugins:{}
  61. }, this.options)
  62. // check sources
  63. /*
  64. if (!videoOptions.sources || !videoOptions.sources.length) {
  65. console.warn('Missing required option: "sources".')
  66. return false
  67. }
  68. */
  69. // add language
  70. var language = videoOptions.language
  71. videojs.addLanguage(language, languages[language])
  72. // ios fullscreen
  73. var playsinline = videoOptions.playsinline
  74. if (playsinline) {
  75. this.$el.children[0].setAttribute('playsinline', playsinline)
  76. this.$el.children[0].setAttribute('webkit-playsinline', playsinline)
  77. }
  78. // emit event
  79. var emitPlayerState = function (event, value) {
  80. if (event) {
  81. self.$emit(event, self.player)
  82. }
  83. if (value) {
  84. var values = {}
  85. values[event] = value
  86. self.$emit(videoOptions.customEventName, values)
  87. }
  88. }
  89. // videoOptions
  90. // console.log(videoOptions)
  91. // avoid error "VIDEOJS: ERROR: Unable to find plugin: __ob__"
  92. delete videoOptions.plugins.__ob__;
  93. this.player = videojs(this.$el.children[0], videoOptions, function() {
  94. // player readied
  95. self.$emit('ready', self.player)
  96. this.on('loadeddata', function() {
  97. this.muted(videoOptions.muted)
  98. this.currentTime(videoOptions.start)
  99. emitPlayerState('loadeddata', true)
  100. })
  101. this.on('canplay', function() {
  102. emitPlayerState('canplay', true)
  103. })
  104. this.on('canplaythrough', function() {
  105. emitPlayerState('canplaythrough', true)
  106. })
  107. this.on('play', function() {
  108. emitPlayerState('play', true)
  109. })
  110. this.on('pause', function() {
  111. emitPlayerState('pause', true)
  112. })
  113. this.on('waiting', function() {
  114. emitPlayerState('waiting', true)
  115. })
  116. this.on('playing', function() {
  117. emitPlayerState('playing', true)
  118. })
  119. this.on('ended', function() {
  120. emitPlayerState('ended', true)
  121. })
  122. this.on('timeupdate', function() {
  123. emitPlayerState('timeupdate', this.currentTime())
  124. })
  125. })
  126. },
  127. dispose() {
  128. if (this.player && videojs) {
  129. this.player.pause && this.player.pause()
  130. videojs(this.$el.children[0]).dispose()
  131. if (!this.$el.children.length) {
  132. var video = document.createElement('video')
  133. video.className = 'video-js vjs-custom-skin'
  134. this.$el.appendChild(video)
  135. }
  136. this.player = null
  137. }
  138. }
  139. },
  140. watch: {
  141. options: {
  142. deep: true,
  143. handler(options, oldOptions) {
  144. this.dispose()
  145. if (options && options.sources && options.sources.length) {
  146. this.initialize()
  147. }
  148. }
  149. }
  150. }
  151. }
  152. </script>
  153. <style src="./player.css"></style>