-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MyVideoPlayer.js
141 lines (120 loc) · 4.43 KB
/
MMM-MyVideoPlayer.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
Module.register("MMM-MyVideoPlayer", {
// Module configuration options
defaults: {
webmFolder: "webms",
gifFolder: "gifs",
mp4Folder: "mp4s",
serverUrl: "http://localhost:3001", // Adjust as needed
playsBeforeChange: 2,
},
playsCounter: 0,
currentVideo: null,
start: function () {
this.playRandomVideo();
console.log("Running MyVideoPlayer");
},
getStyles: function () {
return ["MMM-MyVideoPlayer.css"];
},
getDom: function () {
// Create the initial wrapper element if not exists
if (!this.wrapper) {
this.wrapper = document.createElement("div");
this.wrapper.className = "my-video-player"; // Add your custom class if needed
}
// Perform initial setup and create child elements if needed
// Return the wrapper element
return this.wrapper;
},
updateDom: function () {
console.log("Updating DOM");
// Perform any updates to the module's DOM structure
// This method is called whenever the module needs to be updated
// Access and manipulate this.wrapper here
this.playRandomVideo();
},
playRandomVideo: function () {
const videoElement = document.createElement("video");
videoElement.autoplay = true;
videoElement.loop = false;
// Determine whether to play a webm, gif, or mp4
const randomValue = Math.random();
let fileFolder, fileExtension;
if (randomValue < 0.33) {
fileFolder = this.config.webmFolder;
fileExtension = "webm";
} else if (randomValue < 0.66) {
fileFolder = this.config.gifFolder;
fileExtension = "gif";
} else {
fileFolder = this.config.mp4Folder;
fileExtension = "mp4";
}
// Logic to retrieve and play a random video from the specified folder
this.getAndPlayRandomFile(videoElement, fileFolder, fileExtension);
},
getAndPlayRandomFile: function (videoElement, folder, extension) {
// Fetch the list of files from the server
this.getListOfFiles(folder)
.then((files) => {
// If there are no files, log an error and exit
if (files.length === 0) {
console.error(`No ${extension.toUpperCase()} files found in ${folder}`);
return;
}
// Select a random file from the list
const randomFile = files[Math.floor(Math.random() * files.length)];
console.log(`Playing next ${extension.toUpperCase()} file: ${randomFile}`);
// Set the source of the video element to the selected file
videoElement.src = `${this.config.serverUrl}/files/${folder}/${encodeURIComponent(randomFile)}`;
// Check if this.wrapper is defined
if (this.wrapper) {
// Remove existing video elements
while (this.wrapper.firstChild) {
this.wrapper.removeChild(this.wrapper.firstChild);
}
// Append the video element to the module's DOM
this.wrapper.appendChild(videoElement);
videoElement.addEventListener("error", (error) => {
console.error("Error loading video:", error);
// Handle the error (e.g., skip to the next video)
this.playRandomVideo();
});
// Listen for the video ended event to trigger the next video
videoElement.addEventListener("ended", () => {
this.playsCounter++;
// Check if the playsCounter has reached the configured limit
if (this.playsCounter >= this.config.playsBeforeChange) {
// Reset the counter
this.playsCounter = 0;
// Play a new random video
this.playRandomVideo();
} else {
// Continue playing the same video
videoElement.play();
}
});
} else {
console.error(`Error loading ${extension.toUpperCase()} files: 'wrapper' element is undefined.`);
}
})
.catch((error) => {
console.error(`Error loading ${extension.toUpperCase()} files:`, error);
});
},
getListOfFiles: function (folder) {
return new Promise((resolve, reject) => {
const apiUrl = `${this.config.serverUrl}/fileList/${folder}/`;
// Fetch the list of files from the server
fetch(apiUrl)
.then((response) => {
if (!response.ok) {
throw new Error(`Failed to fetch file list for ${folder}`);
}
return response.json();
})
.then((files) => resolve(files))
.catch((error) => reject(error));
});
},
});