Skip to content
This repository was archived by the owner on Dec 30, 2021. It is now read-only.

Commit 1201b74

Browse files
committed
update player status function created
1 parent 11a7919 commit 1201b74

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

electron/preload.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const videojs = require('video.js')
22
const fs = require('fs-extra');
33
videojs.options.autoplay = true;
44
let $ = null;
5-
const { ipcRenderer} = require('electron');
5+
const { ipcRenderer } = require('electron');
66

77
let g_player = null;
88

@@ -19,13 +19,23 @@ window.addEventListener('DOMContentLoaded', () => {
1919
}
2020
})
2121

22+
ipcRenderer.on('PLAYER_STATUS', (event, arg)=>{
23+
if(g_player){
24+
ipcRenderer.send('RES_PLAYER_STATUS', {isPaused:checkPlayerPaused()});
25+
}
26+
})
27+
2228
ipcRenderer.on('PLAYER_PAUSE', (event, arg)=>{
2329
if(g_player){
2430
pausePlayer();
2531
}
2632
})
2733
})
2834

35+
const checkPlayerPaused = () => {
36+
return g_player.paused();
37+
}
38+
2939
const createPlayer = async(url, subDir, size, hasSubs) =>{
3040
if ($('#player')){
3141
$('#player').remove();
@@ -76,14 +86,14 @@ const pausePlayer = () =>{
7686
}else{
7787
g_player.pause();
7888
}
89+
ipcRenderer.send('RES_PLAYER_STATUS', {isPaused:checkPlayerPaused()});
7990
}
8091

8192
setSubtitles = async (path) =>{
8293
const files = await fs.readdir(path)
8394
const promises = [];
8495
for(f of files){
8596
if (f.includes('.vtt')){
86-
console.log(f)
8797
promises.push(addSubtitle(path, f));
8898
}
8999
}
@@ -95,10 +105,7 @@ addSubtitle = async (path, subName) =>{
95105
let fullPath = path + subName;
96106
let track = g_player.addRemoteTextTrack({src: fullPath, default: true, label:subName})
97107
track.addEventListener('load', function(){
98-
console.log('loaded')
99108
resolve()
100109
})
101110
})
102111
}
103-
104-
//G:/torrents/movies/Avengers Infinity War (2018) [BluRay] [720p] [YTS.AM]/subtitle.srt

main.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {app, BrowserWindow, screen } = require('electron')
1+
const {app, BrowserWindow, screen, ipcMain } = require('electron')
22
const path = require('path')
33
require('dotenv').config()
44
//require('electron-reloader')(module)
@@ -11,7 +11,7 @@ const peerflix = require("./vendor/peerflix");
1111
const subsApi = require('./src/subtitles/subtitles');
1212
const port = process.env.PORT;
1313

14-
let g_activeMovieCode = null;
14+
let g_activeMedia = null;
1515
let mainWindow = null;
1616
let g_engine = null;
1717
let g_dirName = null;
@@ -48,6 +48,10 @@ app.on('window-all-closed', function () {
4848
if (process.platform !== 'darwin') app.quit()
4949
})
5050

51+
ipcMain.on('RES_PLAYER_STATUS', (event, arg)=>{
52+
io.sockets.emit('SET_PLAYER_PAUSED', {isPaused:arg.isPaused});
53+
})
54+
5155
//
5256

5357

@@ -57,39 +61,52 @@ io.on("connection", socket => {
5761
//receives the movie data from the mobile app
5862
socket.on('APP_START_STREAM', (data)=>{
5963
const magnet = data.value.url
60-
g_activeMovieCode = data.imdb_code;
64+
g_activeMedia = data;
6165

6266
//console.log(data);
6367
start(data, magnet);
6468
})
6569

6670
//mobile app requesting status when the component is mounted
6771
socket.on('APP_GET_STATUS',()=>{
68-
if (g_engine != null && g_activeMovieCode != null){
69-
io.sockets.emit('SET_WATCHING',{condition:true, activeCode:g_activeMovieCode});
70-
}else{
71-
io.sockets.emit('SET_WATCHING',{condition:false});
72-
}
72+
updateAppStatus();
7373
})
7474

7575
socket.on('APP_PAUSE_PLAYER', ()=>{
7676
mainWindow.webContents.send('PLAYER_PAUSE');
7777
})
7878

79+
socket.on('APP_GET_PAUSED', ()=>{
80+
mainWindow.webContents.send('PLAYER_STATUS');
81+
})
82+
7983
//received when the mobile app press the button to close the player
8084
socket.on('APP_CLOSE_PROCESS', ()=>{
8185
if (g_engine){
8286
mainWindow.webContents.send('PLAYER_CLOSE');
8387
g_engine.destroy(()=>{
84-
io.sockets.emit('SET_WATCHING', {condition:false});
85-
g_activeMovieCode = null;
88+
g_activeMedia = null;
8689
g_engine = null;
90+
updateAppStatus()
8791
fs.emptyDir(g_dirName);
8892
});
8993
}
9094
})
9195
});
9296

97+
const updateAppStatus = () =>{
98+
if (g_engine != null && g_activeMedia != null){
99+
const media = {
100+
...g_activeMedia.value,
101+
title: g_activeMedia.title,
102+
largeImage: g_activeMedia.largeImage,
103+
}
104+
io.sockets.emit('SET_STATUS_APP', {condition:true, media});
105+
}else{
106+
io.sockets.emit('SET_STATUS_APP',{condition:false});
107+
}
108+
}
109+
93110
//starts the torrent-stream engine and opens the vlc with the engine stream;
94111
async function start(data, uri) {
95112
if (!uri) {
@@ -122,7 +139,7 @@ openPlayer = async(data)=>{
122139
const {status, subs} = await subsApi.getSubtitles(data, g_dirName);
123140
let hasSubs = true;
124141
if(status != 200 || subs < 1){
125-
console.log(chalk.red('Starting without subtitles'));
142+
console.log('Starting without subtitles');
126143
hasSubs = false;
127144
}
128145

@@ -147,7 +164,7 @@ openPlayer = async(data)=>{
147164
clearInterval(interval);
148165

149166
//send watching status to mobile app to show the buttons
150-
io.sockets.emit('SET_WATCHING', {condition: true, activeCode: g_activeMovieCode});
167+
io.sockets.emit('SET_STATUS_APP', {condition: true, activeMedia: g_activeMedia});
151168

152169
//streaming crashes if i remove the verify listener
153170
//g_engine.removeListener('verify', changePiece)

0 commit comments

Comments
 (0)