-
Notifications
You must be signed in to change notification settings - Fork 19
/
youtube.es7.js
56 lines (47 loc) · 1.15 KB
/
youtube.es7.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
/* Import the module */
import kodi from '../';
/* Define a youtube video ID */
const videoId = '-ZOiX6cIT8o';
/* Construct url to play with youtube plugin */
const url = 'plugin://plugin.video.youtube/?action=play_video&videoid=' + videoId;
/* Utility function to delay async execution */
function sleep(ms) {
return new Promise(res => {
setTimeout(function() {
res();
}, ms);
});
}
/* Utility function to stop all active players of a kodi instance */
async function stopAllActivePlayers(con) {
const players = await con.Player.GetActivePlayers();
await Promise.all(players.map(player => {
con.Player.Stop(player.playerid);
}));
}
/* Main logic */
async function main() {
const con = await kodi('127.0.0.1', 9090);
/* Stop all players, then start the video */
await stopAllActivePlayers(con);
await con.Player.Open({
item: {
file: url
}
});
/* Stop the video after 20 seconds */
await sleep(20000);
await stopAllActivePlayers(con);
}
/* Run the thing */
main().catch(e => {
/* Handle errors */
if(e.stack) {
console.error(e.stack);
} else {
console.error(e);
}
}).then(() => {
/* Finally exit this process */
process.exit();
});