player.vue
5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<template>
<div class="video-player" v-if="reseted">
<video class="video-js" ref="video"></video>
</div>
</template>
<script>
// lib
import _videojs from 'video.js'
const videojs = window.videojs || _videojs
// pollfill
if (typeof Object.assign != 'function') {
Object.defineProperty(Object, 'assign', {
value(target, varArgs) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object')
}
const to = Object(target)
for (let index = 1; index < arguments.length; index++) {
const nextSource = arguments[index]
if (nextSource != null) {
for (const nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey]
}
}
}
}
return to
},
writable: true,
configurable: true
})
}
// as of videojs 6.6.0
const DEFAULT_EVENTS = [
'loadeddata',
'canplay',
'canplaythrough',
'play',
'pause',
'waiting',
'playing',
'ended',
'error'
]
// export
export default {
name: 'video-player',
props: {
start: {
type: Number,
default: 0
},
crossOrigin: {
type: String,
default: ''
},
playsinline: {
type: Boolean,
default: false
},
customEventName: {
type: String,
default: 'statechanged'
},
options: {
type: Object,
required: true
},
events: {
type: Array,
default: () => []
},
globalOptions: {
type: Object,
default: () => ({
// autoplay: false,
controls: true,
// preload: 'auto',
// fluid: false,
// muted: false,
controlBar: {
remainingTimeDisplay: false,
playToggle: {},
progressControl: {},
fullscreenToggle: {},
volumeMenuButton: {
inline: false,
vertical: true
}
},
techOrder: ['html5'],
plugins: {}
})
},
globalEvents: {
type: Array,
default: () => []
}
},
data() {
return {
player: null,
reseted: true
}
},
mounted() {
if (!this.player) {
this.initialize()
}
},
beforeDestroy() {
if (this.player) {
this.dispose()
}
},
methods: {
initialize() {
// videojs options
const videoOptions = Object.assign({}, this.globalOptions, this.options)
// ios fullscreen
if (this.playsinline) {
this.$refs.video.setAttribute('playsinline', this.playsinline)
this.$refs.video.setAttribute('webkit-playsinline', this.playsinline)
this.$refs.video.setAttribute('x5-playsinline', this.playsinline)
this.$refs.video.setAttribute('x5-video-player-type', 'h5')
this.$refs.video.setAttribute('x5-video-player-fullscreen', false)
}
// cross origin
if (this.crossOrigin !== '') {
this.$refs.video.crossOrigin = this.crossOrigin
this.$refs.video.setAttribute('crossOrigin', this.crossOrigin)
}
// emit event
const emitPlayerState = (event, value) => {
if (event) {
this.$emit(event, this.player)
}
if (value) {
this.$emit(this.customEventName, { [event]: value })
}
}
// avoid error "VIDEOJS: ERROR: Unable to find plugin: __ob__"
if (videoOptions.plugins) {
delete videoOptions.plugins.__ob__
}
// videoOptions
// console.log('videoOptions', videoOptions)
// player
const self = this
this.player = videojs(this.$refs.video, videoOptions, function() {
// events
const events = DEFAULT_EVENTS.concat(self.events).concat(self.globalEvents)
// watch events
const onEdEvents = {}
for (let i = 0; i < events.length; i++) {
if (typeof events[i] === 'string' && onEdEvents[events[i]] === undefined) {
(event => {
onEdEvents[event] = null
this.on(event, () => {
emitPlayerState(event, true)
})
})(events[i])
}
}
// watch timeupdate
this.on('timeupdate', function() {
emitPlayerState('timeupdate', this.currentTime())
})
// player readied
self.$emit('ready', this)
})
},
dispose(callback) {
if (this.player && this.player.dispose) {
if (this.player.techName_ !== 'Flash') {
this.player.pause && this.player.pause()
}
this.player.dispose()
this.player = null
this.$nextTick(() => {
this.reseted = false
this.$nextTick(() => {
this.reseted = true
this.$nextTick(() => {
callback && callback()
})
})
})
/*
if (!this.$el.children.length) {
const video = document.createElement('video')
video.className = 'video-js'
this.$el.appendChild(video)
}
*/
}
}
},
watch: {
options: {
deep: true,
handler(options, oldOptions) {
this.dispose(() => {
if (options && options.sources && options.sources.length) {
this.initialize()
}
})
}
}
}
}
</script>