feat: added proper updates through the mediasession api. fixes #198

This commit is contained in:
Rick van Lieshout 2023-04-27 14:29:28 +02:00
parent 07be74af9f
commit 6849952c41
5 changed files with 37 additions and 16 deletions

View File

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### New features ### New features
- Added proper updates through the MediaSession API
- You can now add custom CSS in the "advanced" settings tab - You can now add custom CSS in the "advanced" settings tab
- You can now configure the updateFrequency in the settings window - You can now configure the updateFrequency in the settings window
- Default value is set to 500 and will overwrite the hardcoded value of 100 - Default value is set to 500 and will overwrite the hardcoded value of 100

View File

@ -323,14 +323,14 @@ function updateMediaInfo(options, notify) {
if (options) { if (options) {
ipcRenderer.send(globalEvents.updateInfo, options); ipcRenderer.send(globalEvents.updateInfo, options);
if (store.get(settings.notifications) && notify) { if (store.get(settings.notifications) && notify) {
new Notification({ title: options.title, body: options.message, icon: options.icon }).show(); new Notification({ title: options.title, body: options.artists, icon: options.icon }).show();
} }
if (player) { if (player) {
player.metadata = { player.metadata = {
...player.metadata, ...player.metadata,
...{ ...{
"xesam:title": options.title, "xesam:title": options.title,
"xesam:artist": [options.message], "xesam:artist": [options.artists],
"xesam:album": options.album, "xesam:album": options.album,
"mpris:artUrl": options.image, "mpris:artUrl": options.image,
"mpris:length": convertDuration(options.duration) * 1000 * 1000, "mpris:length": convertDuration(options.duration) * 1000 * 1000,
@ -361,6 +361,23 @@ function getTrackID() {
return window.location; return window.location;
} }
function updateMediaSession(options) {
if ("mediaSession" in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: options.title,
artist: options.artists,
album: options.album,
artwork: [
{
src: options.icon,
sizes: "640x640",
type: "image/png",
},
],
});
}
}
/** /**
* Watch for song changes and update title + notify * Watch for song changes and update title + notify
*/ */
@ -377,7 +394,7 @@ setInterval(function () {
const currentStatus = getCurrentlyPlayingStatus(); const currentStatus = getCurrentlyPlayingStatus();
const options = { const options = {
title, title,
message: artistsString, artists: artistsString,
album: album, album: album,
status: currentStatus, status: currentStatus,
url: getTrackURL(), url: getTrackURL(),
@ -386,7 +403,7 @@ setInterval(function () {
"app-name": appName, "app-name": appName,
}; };
const titleOrArtistChanged = currentSong !== songDashArtistTitle; const titleOrArtistsChanged = currentSong !== songDashArtistTitle;
// update title, url and play info with new info // update title, url and play info with new info
setTitle(songDashArtistTitle); setTitle(songDashArtistTitle);
@ -415,7 +432,10 @@ setInterval(function () {
} }
}).then( }).then(
() => { () => {
updateMediaInfo(options, titleOrArtistChanged); updateMediaInfo(options, titleOrArtistsChanged);
if (titleOrArtistsChanged) {
updateMediaSession(options);
}
}, },
() => {} () => {}
); );

View File

@ -26,8 +26,8 @@ const observer = (event, arg) => {
...idleStatus, ...idleStatus,
...{ ...{
details: `Listening to ${mediaInfoModule.mediaInfo.title}`, details: `Listening to ${mediaInfoModule.mediaInfo.title}`,
state: mediaInfoModule.mediaInfo.artist state: mediaInfoModule.mediaInfo.artists
? mediaInfoModule.mediaInfo.artist ? mediaInfoModule.mediaInfo.artists
: "unknown artist(s)", : "unknown artist(s)",
startTimestamp: parseInt(now), startTimestamp: parseInt(now),
endTimestamp: parseInt(remaining), endTimestamp: parseInt(remaining),
@ -43,7 +43,7 @@ const observer = (event, arg) => {
...idleStatus, ...idleStatus,
...{ ...{
details: `Watching ${mediaInfoModule.mediaInfo.title}`, details: `Watching ${mediaInfoModule.mediaInfo.title}`,
state: mediaInfoModule.mediaInfo.artist, state: mediaInfoModule.mediaInfo.artists,
startTimestamp: parseInt(now), startTimestamp: parseInt(now),
endTimestamp: parseInt(remaining), endTimestamp: parseInt(remaining),
}, },

View File

@ -11,7 +11,7 @@ let expressInstance;
/** /**
* Function to enable tidal-hifi's express api * Function to enable tidal-hifi's express api
*/ */
expressModule.run = function(mainWindow) { expressModule.run = function (mainWindow) {
/** /**
* Shorthand to handle a fire and forget global event * Shorthand to handle a fire and forget global event
* @param {*} res * @param {*} res
@ -24,14 +24,14 @@ expressModule.run = function(mainWindow) {
const expressApp = express(); const expressApp = express();
expressApp.get("/", (req, res) => res.send("Hello World!")); expressApp.get("/", (req, res) => res.send("Hello World!"));
expressApp.get("/current", (req, res) => res.json(mediaInfo)); expressApp.get("/current", (req, res) => res.json({ ...mediaInfo, artist: mediaInfo.artists }));
expressApp.get("/image", (req, res) => { expressApp.get("/image", (req, res) => {
var stream = fs.createReadStream(mediaInfo.icon); var stream = fs.createReadStream(mediaInfo.icon);
stream.on("open", function() { stream.on("open", function () {
res.set("Content-Type", "image/png"); res.set("Content-Type", "image/png");
stream.pipe(res); stream.pipe(res);
}); });
stream.on("error", function() { stream.on("error", function () {
res.set("Content-Type", "text/plain"); res.set("Content-Type", "text/plain");
res.status(404).end("Not found"); res.status(404).end("Not found");
}); });
@ -54,7 +54,7 @@ expressModule.run = function(mainWindow) {
let port = store.get(settings.apiSettings.port); let port = store.get(settings.apiSettings.port);
expressInstance = expressApp.listen(port, "127.0.0.1", () => {}); expressInstance = expressApp.listen(port, "127.0.0.1", () => {});
expressInstance.on("error", function(e) { expressInstance.on("error", function (e) {
let message = e.code; let message = e.code;
if (e.code === "EADDRINUSE") { if (e.code === "EADDRINUSE") {
message = `Port ${port} in use.`; message = `Port ${port} in use.`;

View File

@ -2,14 +2,14 @@ const statuses = require("./../constants/statuses");
const mediaInfo = { const mediaInfo = {
title: "", title: "",
artist: "", artists: "",
album: "", album: "",
icon: "", icon: "",
status: statuses.paused, status: statuses.paused,
url: "", url: "",
current: "", current: "",
duration: "", duration: "",
image: "tidal-hifi-icon" image: "tidal-hifi-icon",
}; };
const mediaInfoModule = { const mediaInfoModule = {
mediaInfo, mediaInfo,
@ -20,7 +20,7 @@ const mediaInfoModule = {
*/ */
mediaInfoModule.update = function (arg) { mediaInfoModule.update = function (arg) {
mediaInfo.title = propOrDefault(arg.title); mediaInfo.title = propOrDefault(arg.title);
mediaInfo.artist = propOrDefault(arg.message); mediaInfo.artists = propOrDefault(arg.artists);
mediaInfo.album = propOrDefault(arg.album); mediaInfo.album = propOrDefault(arg.album);
mediaInfo.icon = propOrDefault(arg.icon); mediaInfo.icon = propOrDefault(arg.icon);
mediaInfo.url = propOrDefault(arg.url); mediaInfo.url = propOrDefault(arg.url);