This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TwitchPlayer.vue
executable file
·149 lines (135 loc) · 4.29 KB
/
TwitchPlayer.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
<template>
<div ref="player"></div>
</template>
<script>
import Vue from 'vue';
import LoadScript from 'vue-plugin-load-script';
Vue.use(LoadScript);
let player;
export default {
name: 'twitch-player',
props: {
width: {
type: String,
default: '400',
},
height: {
type: String,
default: '300',
},
volume: {
type: Number,
default: 0.5,
},
quality: {
type: String,
default: 'medium',
},
playsInline: { // If true, the embedded player plays inline for mobile iOS apps.
type: Boolean,
default: false,
},
channel: String,
collection: String,
video: String,
},
beforeCreate() {
Vue.loadScript('https://player.twitch.tv/js/embed/v1.js')
.then(() => {
const options = {
width: this.width,
height: this.height,
};
if (this.playsInline) {
options.playsinline = true;
}
if (this.channel) {
options.channel = this.channel;
} else if (this.collection) {
options.collection = this.collection;
} else if (this.video) {
options.video = this.video;
} else {
this.$emit('error', 'no source specified');
}
player = new window.Twitch.Player(this.$refs.player, options);
player.addEventListener('ended', () => (this.$emit('ended')));
player.addEventListener('pause', () => (this.$emit('pause')));
player.addEventListener('play', () => (this.$emit('play')));
player.addEventListener('offline', () => (this.$emit('offline')));
player.addEventListener('online', () => (this.$emit('online')));
player.addEventListener('ready', () => {
player.setQuality(this.quality);
player.setVolume(this.volume);
this.$emit('ready');
});
}).catch((e) => (this.$emit('error', e)));
},
methods: {
play() { // Begins playing the specified video.
player.play();
},
pause() { // Pauses the player.
player.pause();
},
seek(timestamp) { // Seeks to the specified timestamp (in seconds) in the video and resumes playing if paused. Does not work for live streams.
player.seek(timestamp);
},
getCurrentTime() { // Returns the current video’s timestamp, in seconds. Works only for VODs, not live streams.
return player.getCurrentTime();
},
getDuration() { // Returns the duration of the video, in seconds. Works only for VODs,not live streams.
return player.getDuration();
},
getPlaybackStats() { // Returns an object with statistics the embedded video player and the current live stream or VOD.
return player.getPlaybackStats();
},
getQuality() { // Returns the current quality of video playback.
return player.getQuality();
},
isPaused() { // Returns true if the video is paused; otherwise, false. Buffering or seeking is considered playing.
return player.isPaused();
},
hasEnded() { // Returns true if the live stream or VOD has ended; otherwise, false.
return player.getEnded();
},
getVolume() { // Returns the volume level, a value between 0.0 and 1.0.
return player.getVolume();
},
isMuted() { // Returns true if the player is muted; otherwise, false.
return player.getMuted();
},
mute() { // Mutes the player.
player.setMuted(true);
},
unmute() { // Unmutes the player.
player.setMuted(false);
},
checkChannel() {
return this.channel === player.getChannel();
},
checkVideo() {
return this.video = player.getVideo();
},
},
watch: {
channel(newChannel) {
player.setChannel(newChannel);
},
collection(newCollection) {
player.setCollection(newCollection);
},
video(newVideo) {
player.setVideo(newVideo);
},
volume(newVolume) {
player.setVolume(newVolume);
},
quality(newQuality) {
if (player.getQualities().indexOf(newQuality) !== -1) {
player.setQuality(newQuality);
}
},
},
};
</script>