Made the media player more responsive and accurate. (#95)
This commit is contained in:
34
lib/shared/media.ts
Normal file
34
lib/shared/media.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { MprisPlayer } from "types/service/mpris";
|
||||
const mpris = await Service.import("mpris");
|
||||
|
||||
export const getCurrentPlayer = (activePlayer: MprisPlayer = mpris.players[0]): MprisPlayer => {
|
||||
const statusOrder = {
|
||||
Playing: 1,
|
||||
Paused: 2,
|
||||
Stopped: 3,
|
||||
};
|
||||
|
||||
if (mpris.players.length === 0) {
|
||||
return mpris.players[0];
|
||||
}
|
||||
|
||||
const isPlaying = mpris.players.some(
|
||||
(p) => p["play-back-status"] === "Playing",
|
||||
);
|
||||
|
||||
const playerStillExists = mpris.players.some(
|
||||
(p) => activePlayer["bus-name"] === p["bus-name"],
|
||||
);
|
||||
|
||||
const nextPlayerUp = mpris.players.sort(
|
||||
(a, b) =>
|
||||
statusOrder[a["play-back-status"]] -
|
||||
statusOrder[b["play-back-status"]],
|
||||
)[0];
|
||||
|
||||
if (isPlaying || !playerStillExists) {
|
||||
return nextPlayerUp;
|
||||
}
|
||||
|
||||
return activePlayer;
|
||||
}
|
||||
Reference in New Issue
Block a user