mpris fix
This commit is contained in:
parent
6da8c3c2b6
commit
44ed769ed6
6 changed files with 127 additions and 133 deletions
|
@ -12,9 +12,9 @@ import fetch from 'electron-fetch'
|
|||
import {wsapi} from "./wsapi";
|
||||
import {utils} from './utils';
|
||||
import {Plugins} from "./plugins";
|
||||
|
||||
const fileWatcher = require('chokidar');
|
||||
const AdmZip = require("adm-zip");
|
||||
import {watch} from "chokidar";
|
||||
// @ts-ignore
|
||||
import * as AdmZip from "adm-zip";
|
||||
|
||||
/**
|
||||
* @file Creates the BrowserWindow
|
||||
|
@ -244,9 +244,7 @@ export class BrowserWindow {
|
|||
};
|
||||
|
||||
StartWatcher(path: string) {
|
||||
var chokidar = require("chokidar");
|
||||
|
||||
var watcher = chokidar.watch(path, {
|
||||
const watcher = watch(path, {
|
||||
ignored: /[\/\\]\./,
|
||||
persistent: true
|
||||
});
|
||||
|
@ -1049,11 +1047,7 @@ export class BrowserWindow {
|
|||
|
||||
ipcMain.on('disable-update', (event) => {
|
||||
// Check if using app store builds so people don't get pissy wen button go bonk
|
||||
if (app.isPackaged && !process.mas || !process.windowsStore) {
|
||||
event.returnValue = false
|
||||
} else {
|
||||
event.returnValue = true
|
||||
}
|
||||
event.returnValue = !(app.isPackaged && !process.mas || !process.windowsStore);
|
||||
})
|
||||
|
||||
|
||||
|
@ -1190,7 +1184,7 @@ export class BrowserWindow {
|
|||
private async broadcastRemote() {
|
||||
const myString = `http://${BrowserWindow.getIP()}:${this.remotePort}`;
|
||||
const mdns = require('mdns-js');
|
||||
const encoded = new Buffer(myString).toString('base64');
|
||||
const encoded = Buffer.from(myString).toString('base64');
|
||||
const x = mdns.tcp('cider-remote');
|
||||
const txt_record = {
|
||||
"Ver": "131077",
|
||||
|
|
|
@ -54,6 +54,7 @@ app.on('ready', () => {
|
|||
|
||||
win.on("ready-to-show", () => {
|
||||
Cider.bwCreated();
|
||||
console.debug('[Cider] Window is Ready.')
|
||||
CiderPlug.callPlugins('onReady', win);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
// @ts-ignore
|
||||
import * as Player from 'mpris-service';
|
||||
|
||||
export default class MPRIS {
|
||||
export default class mpris {
|
||||
/**
|
||||
* Private variables for interaction in plugins
|
||||
*/
|
||||
private _win: any;
|
||||
private _app: any;
|
||||
private static utils: any;
|
||||
|
||||
/**
|
||||
|
@ -20,8 +18,8 @@ export default class MPRIS {
|
|||
/**
|
||||
* MPRIS Service
|
||||
*/
|
||||
private mpris: any;
|
||||
private mprisEvents: Object = {
|
||||
private static player: Player.Player;
|
||||
private static mprisEvents: Object = {
|
||||
"playpause": "playPause",
|
||||
"play": "play",
|
||||
"pause": "pause",
|
||||
|
@ -35,11 +33,12 @@ export default class MPRIS {
|
|||
|
||||
/**
|
||||
* Runs a media event
|
||||
* @param type - pausePlay, nextTrack, PreviousTrack
|
||||
* @param type - pausePlay, next, previous
|
||||
* @private
|
||||
*/
|
||||
private static runMediaEvent(type: string) {
|
||||
MPRIS.utils.getWindow().webContents.executeJavaScript(`MusicKitInterop.${type}()`).catch(console.error)
|
||||
console.debug(`[Plugin][${this.name}] ${type}.`);
|
||||
mpris.utils.getWindow().webContents.executeJavaScript(`MusicKitInterop.${type}()`).catch(console.error)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,90 +58,89 @@ export default class MPRIS {
|
|||
/**
|
||||
* Connects to MPRIS Service
|
||||
*/
|
||||
@MPRIS.linuxOnly
|
||||
private connect() {
|
||||
this.mpris = Player({
|
||||
name: 'Cider',
|
||||
@mpris.linuxOnly
|
||||
private static connect() {
|
||||
|
||||
const player = Player({
|
||||
name: 'cider',
|
||||
identity: 'Cider',
|
||||
supportedUriSchemes: [],
|
||||
supportedMimeTypes: [],
|
||||
supportedInterfaces: ['player']
|
||||
});
|
||||
this.mpris = Object.assign(this.mpris, {
|
||||
canQuit: true,
|
||||
canControl: true,
|
||||
canPause: true,
|
||||
canPlay: true,
|
||||
canGoNext: true,
|
||||
active: true
|
||||
})
|
||||
|
||||
console.debug(`[Plugin][${mpris.name}] Successfully connected.`);
|
||||
|
||||
const pos_atr = {durationInMillis: 0};
|
||||
this.mpris.getPosition = function () {
|
||||
player.getPosition = function () {
|
||||
const durationInMicro = pos_atr.durationInMillis * 1000;
|
||||
const percentage = parseFloat("0") || 0;
|
||||
return durationInMicro * percentage;
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(this.mprisEvents)) {
|
||||
this.mpris.on(key, () => {
|
||||
MPRIS.runMediaEvent(value)
|
||||
for (const [key, value] of Object.entries(mpris.mprisEvents)) {
|
||||
player.on(key, function () {
|
||||
mpris.runMediaEvent(value)
|
||||
});
|
||||
}
|
||||
|
||||
player.on('quit', function () {
|
||||
process.exit();
|
||||
});
|
||||
|
||||
mpris.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update MPRIS Player Attributes
|
||||
* Update M.P.R.I.S Player Attributes
|
||||
*/
|
||||
@MPRIS.linuxOnly
|
||||
private updatePlayer(attributes: any) {
|
||||
@mpris.linuxOnly
|
||||
private static updatePlayer(attributes: any) {
|
||||
|
||||
const MetaData = {
|
||||
'mpris:trackid': this.mpris.objectPath(`track/${attributes.playParams.id.replace(/[.]+/g, "")}`),
|
||||
'mpris:trackid': mpris.player.objectPath(`track/${attributes.playParams.id.replace(/[.]+/g, "")}`),
|
||||
'mpris:length': attributes.durationInMillis * 1000, // In microseconds
|
||||
'mpris:artUrl': (attributes.artwork.url.replace('/{w}x{h}bb', '/512x512bb')).replace('/2000x2000bb', '/35x35bb'),
|
||||
'xesam:title': `${attributes.name}`,
|
||||
'xesam:album': `${attributes.albumName}`,
|
||||
'xesam:artist': [`${attributes.artistName}`,],
|
||||
'xesam:artist': [`${attributes.artistName}`],
|
||||
'xesam:genre': attributes.genreNames
|
||||
}
|
||||
|
||||
if (this.mpris.metadata["mpris:trackid"] === MetaData["mpris:trackid"]) {
|
||||
if (mpris.player.metadata["mpris:trackid"] === MetaData["mpris:trackid"]) {
|
||||
return
|
||||
}
|
||||
|
||||
this.mpris.metadata = MetaData;
|
||||
mpris.player.metadata = MetaData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update MPRIS Player State
|
||||
* Update M.P.R.I.S Player State
|
||||
* @private
|
||||
* @param attributes
|
||||
*/
|
||||
@MPRIS.linuxOnly
|
||||
private updatePlayerState(attributes: any) {
|
||||
|
||||
let status = 'Stopped';
|
||||
if (attributes.status) {
|
||||
status = 'Playing';
|
||||
} else if (attributes.status === false) {
|
||||
status = 'Paused';
|
||||
@mpris.linuxOnly
|
||||
private static updatePlayerState(attributes: any) {
|
||||
switch (attributes.status) {
|
||||
case true: // Playing
|
||||
mpris.player.playbackStatus = Player.PLAYBACK_STATUS_PLAYING;
|
||||
break;
|
||||
case false: // Paused
|
||||
mpris.player.playbackStatus = Player.PLAYBACK_STATUS_PAUSED;
|
||||
break;
|
||||
default:
|
||||
mpris.player.playbackStatus = Player.PLAYBACK_STATUS_STOPPED;
|
||||
break
|
||||
}
|
||||
|
||||
if (this.mpris.playbackStatus === status) {
|
||||
return
|
||||
}
|
||||
this.mpris.playbackStatus = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear state
|
||||
* @private
|
||||
*/
|
||||
private clearState() {
|
||||
this.mpris.metadata = {'mpris:trackid': '/org/mpris/MediaPlayer2/TrackList/NoTrack'}
|
||||
this.mpris.playbackStatus = 'Stopped';
|
||||
private static clearState() {
|
||||
mpris.player.metadata = {'mpris:trackid': '/org/mpris/MediaPlayer2/TrackList/NoTrack'}
|
||||
mpris.player.playbackStatus = Player.PLAYBACK_STATUS_STOPPED;
|
||||
}
|
||||
|
||||
|
||||
|
@ -153,31 +151,32 @@ export default class MPRIS {
|
|||
/**
|
||||
* Runs on plugin load (Currently run on application start)
|
||||
*/
|
||||
constructor(utils: { getApp: () => any; }) {
|
||||
MPRIS.utils = utils
|
||||
this._app = utils.getApp();
|
||||
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
||||
constructor(utils: any) {
|
||||
mpris.utils = utils
|
||||
|
||||
console.debug(`[Plugin][${mpris.name}] Loading Complete.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on app ready
|
||||
*/
|
||||
onReady(win: any): void {
|
||||
this._win = win;
|
||||
console.debug(`[Plugin][${this.name}] Ready.`);
|
||||
this.connect()
|
||||
onReady(_: any): void {
|
||||
console.debug(`[Plugin][${mpris.name}] Ready.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renderer ready
|
||||
*/
|
||||
onRendererReady(): void {
|
||||
mpris.connect()
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on app stop
|
||||
*/
|
||||
onBeforeQuit(): void {
|
||||
console.debug(`[Plugin][${this.name}] Stopped.`);
|
||||
try {
|
||||
this.clearState()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
console.debug(`[Plugin][${mpris.name}] Stopped.`);
|
||||
mpris.clearState()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,7 +184,8 @@ export default class MPRIS {
|
|||
* @param attributes Music Attributes (attributes.status = current state)
|
||||
*/
|
||||
onPlaybackStateDidChange(attributes: object): void {
|
||||
this.updatePlayerState(attributes)
|
||||
console.debug(`[Plugin][${mpris.name}] onPlaybackStateDidChange.`);
|
||||
mpris.updatePlayerState(attributes)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,7 +193,8 @@ export default class MPRIS {
|
|||
* @param attributes Music Attributes
|
||||
*/
|
||||
onNowPlayingItemDidChange(attributes: object): void {
|
||||
this.updatePlayer(attributes);
|
||||
console.debug(`[Plugin][${mpris.name}] onMetadataDidChange.`);
|
||||
mpris.updatePlayer(attributes);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ const MusicKitInterop = {
|
|||
const attributes = MusicKitInterop.getAttributes()
|
||||
if (MusicKitInterop.filterTrack(attributes, true, false)) {
|
||||
global.ipcRenderer.send('playbackStateDidChange', attributes)
|
||||
ipcRenderer.send('wsapi-updatePlaybackState', attributes);
|
||||
global.ipcRenderer.send('wsapi-updatePlaybackState', attributes);
|
||||
// if (typeof _plugins != "undefined") {
|
||||
// _plugins.execute("OnPlaybackStateChanged", {Attributes: MusicKitInterop.getAttributes()})
|
||||
// }
|
||||
|
@ -24,6 +24,7 @@ const MusicKitInterop = {
|
|||
/** wsapi */
|
||||
|
||||
MusicKit.getInstance().addEventListener(MusicKit.Events.nowPlayingItemDidChange, async () => {
|
||||
console.log('nowPlayingItemDidChange')
|
||||
const attributes = MusicKitInterop.getAttributes()
|
||||
const trackFilter = MusicKitInterop.filterTrack(attributes, false, true)
|
||||
|
||||
|
@ -47,7 +48,7 @@ const MusicKitInterop = {
|
|||
})
|
||||
},
|
||||
async modifyNamesOnLocale() {
|
||||
if (app.mklang == '' || app.mklang == null) {
|
||||
if (app.mklang === '' || app.mklang == null) {
|
||||
return;
|
||||
}
|
||||
const mk = MusicKit.getInstance()
|
||||
|
@ -56,17 +57,17 @@ const MusicKitInterop = {
|
|||
return;
|
||||
}
|
||||
const id = nowPlayingItem?._songId ?? (nowPlayingItem?.songId ?? nowPlayingItem?.id)
|
||||
if (id != null && id != -1) {
|
||||
if (id != null && id !== -1) {
|
||||
try{
|
||||
const query = await mk.api.v3.music(`/v1${(((nowPlayingItem?._songId ?? nowPlayingItem?.songId) != null) && ((nowPlayingItem?._songId ?? nowPlayingItem?.songId) != -1)) ? `/catalog/${mk.storefrontId}/` : `/me/library/`}songs/${id}?l=${app.mklang}`);
|
||||
const query = await mk.api.v3.music(`/v1${(((nowPlayingItem?._songId ?? nowPlayingItem?.songId) != null) && ((nowPlayingItem?._songId ?? nowPlayingItem?.songId) !== -1)) ? `/catalog/${mk.storefrontId}/` : `/me/library/`}songs/${id}?l=${app.mklang}`);
|
||||
if (query?.data?.data[0]){
|
||||
let attrs = query?.data?.data[0]?.attributes;
|
||||
if (attrs?.name) { nowPlayingItem.attributes.name = attrs?.name ?? ''}
|
||||
if (attrs?.albumName) { nowPlayingItem.attributes.albumName = attrs?.albumName ?? ''}
|
||||
if (attrs?.artistName) { nowPlayingItem.attributes.artistName = attrs?.artistName ?? ''}
|
||||
|
||||
}} catch (e) { return;}
|
||||
} else {return;}
|
||||
}} catch (e) { }
|
||||
} else {}
|
||||
},
|
||||
getAttributes: function () {
|
||||
const mk = MusicKit.getInstance()
|
||||
|
@ -75,7 +76,7 @@ const MusicKitInterop = {
|
|||
const remainingTimeExport = mk.currentPlaybackTimeRemaining;
|
||||
const attributes = (nowPlayingItem != null ? nowPlayingItem.attributes : {});
|
||||
|
||||
attributes.status = isPlayingExport ?? false;
|
||||
attributes.status = isPlayingExport ?? null;
|
||||
attributes.name = attributes?.name ?? 'no-title-found';
|
||||
attributes.artwork = attributes?.artwork ?? {url: ''};
|
||||
attributes.artwork.url = (attributes?.artwork?.url ?? '').replace(`{f}`, "png");
|
||||
|
@ -120,7 +121,7 @@ const MusicKitInterop = {
|
|||
},
|
||||
|
||||
play: () => {
|
||||
MusicKit.getInstance().play().then(r => console.log(`[MusicKitInterop.play] ${r}`));
|
||||
MusicKit.getInstance().play().catch(console.error);
|
||||
},
|
||||
|
||||
pause: () => {
|
||||
|
@ -131,7 +132,7 @@ const MusicKitInterop = {
|
|||
if (MusicKit.getInstance().isPlaying) {
|
||||
MusicKit.getInstance().pause();
|
||||
} else if (MusicKit.getInstance().nowPlayingItem != null) {
|
||||
MusicKit.getInstance().play().then(r => console.log(`[MusicKitInterop.playPause] Playing ${r}`));
|
||||
MusicKit.getInstance().play().catch(console.error);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue