adds skip, previous, current url
This commit is contained in:
@@ -17,7 +17,6 @@ export class MediaPlayer {
|
||||
private dataBuffer: string = '';
|
||||
|
||||
constructor() {
|
||||
// Create a random string of length 5
|
||||
const socketFilename = Math.random().toString(36).substring(2, 10);
|
||||
const socketPath = `/tmp/mpv-${socketFilename}`;
|
||||
|
||||
@@ -53,6 +52,13 @@ export class MediaPlayer {
|
||||
});
|
||||
}
|
||||
|
||||
public async getCurrentFile(): Promise<string> {
|
||||
return this.writeCommand("get_property", ["stream-open-filename"])
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
public async getPauseState(): Promise<boolean> {
|
||||
return this.writeCommand("get_property", ["pause"])
|
||||
.then((response) => {
|
||||
@@ -66,10 +72,6 @@ export class MediaPlayer {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
public async setVolume(volume: number) {
|
||||
return this.writeCommand("set_property", ["volume", volume]);
|
||||
}
|
||||
|
||||
public async getIdle(): Promise<boolean> {
|
||||
return this.writeCommand("get_property", ["idle"])
|
||||
@@ -79,19 +81,31 @@ export class MediaPlayer {
|
||||
}
|
||||
|
||||
public async append(url: string) {
|
||||
return this.writeCommand("loadfile", [url, "append-play"]);
|
||||
return this.modify(() => this.writeCommand("loadfile", [url, "append-play"]));
|
||||
}
|
||||
|
||||
public async play() {
|
||||
return this.writeCommand("set_property", ["pause", false]);
|
||||
return this.modify(() => this.writeCommand("set_property", ["pause", false]));
|
||||
}
|
||||
|
||||
public async pause() {
|
||||
return this.writeCommand("set_property", ["pause", true]);
|
||||
return this.modify(() => this.writeCommand("set_property", ["pause", true]));
|
||||
}
|
||||
|
||||
public async skip() {
|
||||
return this.modify(() => this.writeCommand("playlist-next", []));
|
||||
}
|
||||
|
||||
public async previous() {
|
||||
return this.modify(() => this.writeCommand("playlist-prev", []));
|
||||
}
|
||||
|
||||
public async deletePlaylistItem(index: number) {
|
||||
return this.writeCommand("playlist-remove", [index]);
|
||||
return this.modify(() => this.writeCommand("playlist-remove", [index]));
|
||||
}
|
||||
|
||||
public async setVolume(volume: number) {
|
||||
return this.modify(() => this.writeCommand("set_property", ["volume", volume]));
|
||||
}
|
||||
|
||||
public subscribe(ws: WebSocket) {
|
||||
@@ -101,6 +115,15 @@ export class MediaPlayer {
|
||||
public unsubscribe(ws: WebSocket) {
|
||||
this.eventSubscribers = this.eventSubscribers.filter(subscriber => subscriber !== ws);
|
||||
}
|
||||
|
||||
private async modify<T>(func: () => Promise<T>): Promise<T> {
|
||||
return func()
|
||||
.then((result) => {
|
||||
// Notify all subscribers
|
||||
this.handleEvent("user_modify", {});
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private async writeCommand(command: string, args: any[]): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
Reference in New Issue
Block a user