Dev for external HTML now playing view
Posted: Fri Jan 17, 2025 3:12 pm
-
The Music Manager for Serious Collectors
https://mediamonkey.com/forum/
Code: Select all
"use strict";
// Function to write the current track information to a JSON file
function write Now Playing ToJSON() {
const track = app.player. getCurrentTrack();
if (!track) {
console.log("No track is currently playing.");
return;
}
// Prepare track information
const trackInfo = {
artist: track. artist || "Unknown Artist",
title: track. name || "Unknown Title",
album: track. album || "Unknown Album",
artwork: track. artworkPath || "No Artwork Available"
};
const jsonContent = JSON. stringify(trackInfo, null, 2);
// Define the file path to save the JSON file
const filePath = "\\now_playing. json";
try {
// Save the JSON content to the file
app.filesystem.saveText ToFileAsync(filePath, jsonContent, function() {
console.log("Now Playing JSON saved to: " + filePath);
});
} catch (error) {
console.error("Error saving JSON file:", error);
}
}
// Listen for playback state changes and call writeNow PlayingToJSON when playback starts or is resumed
app.listen(app.player, 'playback State', function(newState) {
if (newState === 'play' || newState === 'unpause') {
writeNowPlayingToJSON();
}
});
Code: Select all
app.filesystem.saveText ToFileAsync(filePath, jsonContent, function() {
console.log("Now Playing JSON saved to: " + filePath);
});
Code: Select all
app.filesystem.saveTextToFileAsync(filePath, jsonContent, {});
Code: Select all
track.getThumbAsync(500 /* PX */, 500 /* PX */, function(thumbPath) {
let trackInfo = {
title: track.title,
artist: track.artist,
artworkPath: thumbPath
}
app.filesystem.saveTextToFileAsync( filePath, JSON.stringify( trackInfo));
});