-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.vue
235 lines (221 loc) · 6.8 KB
/
Player.vue
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
229
230
231
232
233
234
235
<template>
<div class="KaliopPlayer-player" :class="{'is-playing': isPlaying }" :data-current-media-index="currentMediaIndex">
<button class="KaliopPlayer-playerButton" v-if="mediaType === 'video' && playButton.isVisible" @click="playMedia">
<img v-if="mediaPoster" :src="mediaPoster.src" :alt="mediaPosterAlt">
<svg v-if="playButton.hasIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M256 0C114.833 0 0 114.844 0 256s114.833 256 256 256 256-114.844 256-256S397.167 0 256 0zm101.771 264.969l-149.333 96c-1.75 1.135-3.771 1.698-5.771 1.698-1.75 0-3.521-.438-5.104-1.302C194.125 359.49 192 355.906 192 352V160c0-3.906 2.125-7.49 5.563-9.365 3.375-1.854 7.604-1.74 10.875.396l149.333 96c3.042 1.958 4.896 5.344 4.896 8.969s-1.854 7.01-4.896 8.969z"/></svg>
</button>
<video ref="videoTag" v-if="mediaType === 'video'" class="KaliopPlayer-mediaPlayer" data-kaliop-player-mediaPlayer controls :controlsList="checkOptions" >
<source v-for="(source, index) in mediaSources" :key="`source-${index}`" :src="source.src" :type="source.type ? source.type : ''">
<track
v-for="(subtitle, index) in mediaSubtitles" :key="`subtitle-${index}`"
:label="subtitle.label ? subtitle.label : ''"
:srclang="subtitle.srcLang ? subtitle.srcLang : ''"
:src="subtitle.src"
kind="subtitles"
>
</video>
<audio v-else-if="mediaType === 'audio'" class="KaliopPlayer-mediaPlayer" data-kaliop-player-mediaPlayer controls :controlsList="checkOptions" >
<source v-for="(source, index) in mediaSources" :key="`source-${index}`" :src="source.src" :type="source.type ? source.type : ''">
</audio>
</div>
</template>
<script>
export default {
name: 'Player',
props: {
eventBus: {
required: true,
type: Object
},
mediaType: {
required: false,
type: String,
default: 'video'
},
originMedias: {
required: true,
type: Array
},
originMediaSources: {
required: true,
type: Array
},
originMediaPoster: {
required: true,
type: Object
},
originSubtitles: {
required: false,
type: Array
},
originAutoplay: {
required: false,
type: Boolean,
default: false
},
disablePictureInPicture: {
required: false,
type: Boolean,
default: false
},
controlsList: {
required: false,
type: Object,
default: function () {
return {
nodownload: false,
noplaybackrate: false
}
}
},
playButton: {
required: false,
type: Object,
default: function() {
return {
isVisible: false,
hasIcon: false
}
}
}
},
data() {
return {
medias: [],
currentMediaIndex: '',
mediaSources: [],
mediaPoster: {},
mediaSubtitles: [],
isPlaying: false,
autoplay: false,
mediaNode: null
}
},
computed: {
mediaPosterAlt: function() {
if (this.medias[this.currentMediaIndex]) {
return `Lancer la vidéo : ${this.medias[this.currentMediaIndex].title}`
}
return ''
},
checkOptions () {
let result = [];
for (let option in this.controlsList) {
if (this.controlsList[option]) {
result.push(option)
}
}
return result.join(" ");
}
},
methods: {
playMedia() {
this.mediaNode.play()
this.isPlaying = true
},
// Preload current media
loadMedia() {
this.mediaNode.load()
this.mediaNode.addEventListener('loadeddata', () => {
this.playMedia()
})
},
// Play next media if autoplay is enabled
mediaEnded() {
this.mediaNode.addEventListener('ended', () => {
if (this.autoplay) {
this.updateMedia(this.currentMediaIndex + 1)
}
})
},
// Change current media and send new media to other widgets
updateMedia(mediaIndex) {
if (this.medias[mediaIndex] !== undefined) {
this.eventBus.$emit('Player::UpdateMedia', {index: mediaIndex, media: this.medias[mediaIndex]})
this.updatePlayer(this.medias[mediaIndex], mediaIndex)
this.checkCurrentMedia()
}
},
// Update media data with new one
updatePlayer(media, index) {
this.currentMediaIndex = index
this.mediaSources = media.media_sources
this.mediaPoster = media.image
this.mediaSubtitles = media.subtitles
this.loadMedia()
},
setDisablePictureInPicture () {
if (this.disablePictureInPicture && document.body.contains(this.$refs.videoTag)) {
this.$refs.videoTag.setAttribute('disablePictureInPicture', '');
}
},
// Check if the current is first or last and notify Controls widget
checkCurrentMedia() {
if (this.medias[this.currentMediaIndex - 1] === undefined) {
this.eventBus.$emit('Player::IsFirstMedia')
}
if (this.medias[this.currentMediaIndex + 1] === undefined) {
this.eventBus.$emit('Player::IsLastMedia')
}
}
},
mounted() {
this.medias = this.originMedias
this.currentMediaIndex = 0
this.mediaSources = this.originMediaSources
this.mediaPoster = this.originMediaPoster
this.mediaSubtitles = this.originSubtitles
this.autoplay = this.originAutoplay
this.mediaNode = this.$el.querySelector('[data-kaliop-player-mediaPlayer]')
this.setDisablePictureInPicture()
// Add event listener on media ended
this.mediaEnded()
this.eventBus.$on('Playlist::UpdateMedia', (index) => {
this.updateMedia(index)
})
this.eventBus.$on('Controls::PreviousMedia', () => {
this.updateMedia(this.currentMediaIndex - 1)
})
this.eventBus.$on('Controls::NextMedia', () => {
this.updateMedia(this.currentMediaIndex + 1)
})
this.eventBus.$on('Autoplay::AutoplayState', (state) => {
this.autoplay = state
})
},
}
</script>
<style lang="scss" scoped>
.KaliopPlayer-player {
position: relative;
&.is-playing {
.KaliopPlayer-playerButton {
visibility: hidden;
}
}
}
.KaliopPlayer-playerButton {
z-index: 1;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
img {
width: 100%;
height: 100%;
object-fit: cover;
}
svg {
position: absolute;
left: 50%;
top: 50%;
width: 75px;
height: 75px;
transform: translate(-50%, -50%);
}
}
.KaliopPlayer-mediaPlayer {
display: block;
}
</style>