-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMMM-Videoplayer.js
executable file
·157 lines (136 loc) · 4.94 KB
/
MMM-Videoplayer.js
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
/* global Module */
/* Magic Mirror 2
* Module: MMM-Videoplayer
*
* By Erik Pettersson
* Assisted by Timo Pettersson
* Based on the MMM-videoPlay Module by Sungje KIM
*
* MIT Licensed.
*/
Module.register("MMM-Videoplayer", {
defaults: {
defaultvideo: "/modules/MMM-Videoplayer/video/mov_bbb.mp4",
//video: "/modules/MMM-Videoplayer/video/mov_bbb.mp4", // This can also be a link to a mp4 file on the internet.
//videolist: ["/modules/MMM-Videoplayer/video/test01.mp4", "/modules/MMM-Videoplayer/video/test02.mp4", "/modules/MMM-Videoplayer/video/test03.mp4"], // Can also be links to a mp4 files on the internet.
random: false, // Play the videos randomly.
loop: true, // Repeat the video list.
hideonstart: false, // If set to true, the player will hide it self when a clip is loaded (and just started playing). Then when the player is shown again it will continue play the clip and hide itself again when the next clip is loaded (and just starts playing) and so on.
fadeSpeed: 1000, // The speed to hide the module (milliseconds).
showcontrols: false, // Set to true if you want the video controls to show.
preload: "auto", // Can be set to: "auto", "metadata", "none".
autoplay: true, // If set to true, sound (muted below) has to be true, otherwise the video will not auto play.
muted: true, // Mute the sound. If auto play is true, this needs to be true as well, otherwise the video will not auto play.
pauseonhide: true, // If true the module will pause the video when hidden.
resumeonshow: true, // If true the module will resume the video when shown.
notification: "VIDEOPLAYER1", // Unique notification string for this player.
},
// Loading the CSS
getStyles: function () {
return ["MMM-Videoplayer.css"];
},
// Pause, play, replay and next video control via notifications using "TOGGLE", "REPLAY" or "NEXT".
notificationReceived: function (notification, payload, sender) {
if (notification === this.config.notification) {
if (payload === 'TOGGLE') {
if (this.video.paused) {
this.video.play();
} else {
this.video.pause();
}
} else if (payload === 'NEXT') {
this.nextVideo();
} else if (payload === 'REPLAY') {
this.replayVideo();
}
}
},
// What happens when the module is hidden.
suspend: function () {
if (this.config.pauseonhide) {
this.video.pause();
}
},
// What happens when the module is shown.
resume: function () {
if (this.config.resumeonshow) {
this.video.play();
}
},
// Restart current playing video from start.
replayVideo: function () {
var lastIndex = this.playedVideoArray.length - 1;
if (lastIndex > -1) {
this.video.setAttribute("src", this.playedVideoArray[lastIndex]);
this.video.load();
this.video.play();
}
},
// Plays the next video in queue.
nextVideo: function () {
// If set to true, the player will hide it self when a clip is loaded (and just started playing).
if (this.config.hideonstart) {
this.hide(this.config.fadeSpeed)
}
// Resets the video queue if set to loop.
if (this.videoArray.length == 0) {
if (!this.config.loop) {
return;
}
this.videoArray = this.playedVideoArray;
this.playedVideoArray = [];
}
// Random video.
if (this.config.random) {
this.currentVideoIndex = Math.floor(Math.random() * this.videoArray.length);
}
// Sets the video to play.
this.video.setAttribute("src", this.videoArray[this.currentVideoIndex]);
// Add the played video to the played queue.
this.playedVideoArray.push(this.videoArray.splice(this.currentVideoIndex, 1))
this.video.load();
this.video.play();
},
// Send the module. :)
getDom: function () {
// Setup the video array.
this.videoArray = [];
this.playedVideoArray = [];
// Checks if anything is defined in the config (video or videolist).
if (!this.config.video && !this.config.videolist) {
// If not, adds the default clip.
this.videoArray = [this.config.defaultvideo];
} else {
// If videolist is defined, adds them to the array.
if (this.config.videolist) {
this.videoArray = this.config.videolist
}
// If video is defined add that first in the array.
if (this.config.video) {
this.videoArray.unshift(this.config.video)
}
}
// Build the player.
var wrapper = document.createElement("div");
// Adds the video
this.video = document.createElement("video");
// Make sure we set the video list to 0
this.currentVideoIndex = 0;
// Adds the ended event so we know.
this.video.addEventListener('ended', this.nextVideo.bind(this), false);
// Adds the rest of the payer video tag settings.
this.video.muted = this.config.muted;
this.video.autoplay = this.config.autoplay;
this.video.loop = false;
this.video.controls = this.config.showcontrols;
this.video.preload = this.config.preload;
this.video.id = this.identifier + "_video";
// Loads the first video.
this.nextVideo();
// Wrap it up.
wrapper.appendChild(this.video);
//Sends it back to the dom.
return wrapper;
},
}
);