simplemusic: Fix track duration tracking

master
Andre 2025-06-08 16:59:44 -04:00
parent 3ffbf6e395
commit b29e7843e3
1 changed files with 18 additions and 9 deletions

View File

@ -100,16 +100,18 @@ function initialize() {
// Eat music events (๑ᵔ⤙ᵔ๑) // Eat music events (๑ᵔ⤙ᵔ๑)
Bangle.on("message", (type, message)=>{ Bangle.on("message", (type, message)=>{
if (type === "music" && !message.handled) { if (type.includes("music") && !message.handled) {
processMusicEvent(message); processMusicEvent(message);
message.handled = true; message.handled = true;
}; };
}); });
// Toggle play/pause if the button is pressed // Toggle play/pause if the button is pressed
/*
setWatch(function() { setWatch(function() {
sendCommand(appState.state === PlaybackState.paused ? Command.play : Command.pause, true); sendCommand(appState.state === PlaybackState.paused ? Command.play : Command.pause, true);
}, BTN, {edge:"rising", debounce:50, repeat:true}); }, BTN, {edge:"rising", debounce:50, repeat:true});
*/
// Goad Gadgetbridge into sending us the current track info // Goad Gadgetbridge into sending us the current track info
sendCommand(Command.volumeup, false); sendCommand(Command.volumeup, false);
@ -125,7 +127,7 @@ function draw() {
layout.render(); layout.render();
} }
/// Track how long the current song has been running. // Track how long the current song has been running.
let elapsedTimer; let elapsedTimer;
let position = 0; let position = 0;
function updateTime() { function updateTime() {
@ -136,6 +138,14 @@ function updateTime() {
if (Debug) console.log("Tick"); if (Debug) console.log("Tick");
} }
function clearTimer() {
position = 0;
if (elapsedTimer) {
clearInterval(elapsedTimer);
elapsedTimer = undefined;
};
}
/** /**
* Send a command via Bluetooth back to Gadgetbridge. * Send a command via Bluetooth back to Gadgetbridge.
* @param {Command} command Which command to execute * @param {Command} command Which command to execute
@ -143,14 +153,14 @@ function updateTime() {
*/ */
function sendCommand(command, buzz) { function sendCommand(command, buzz) {
if (buzz) Bangle.buzz(50); if (buzz) Bangle.buzz(50);
Bluetooth.println(JSON.stringify({ t: "music", n: command })); Bangle.musicControl(command);
} }
function processMusicEvent(event) { function processMusicEvent(event) {
if (Debug) console.log("State: " + event.state); if (Debug) console.log("State: " + event.state);
if (Debug) console.log("Position: " + event.position); if (Debug) console.log("Position: " + event.position);
if (event.position !== null) position = event.position; position = event.position;
switch(event.state) { switch(event.state) {
case PlaybackState.playing: case PlaybackState.playing:
@ -162,7 +172,7 @@ function processMusicEvent(event) {
case PlaybackState.paused: case PlaybackState.paused:
if (Debug) console.log("Paused"); if (Debug) console.log("Paused");
appState.state = event.state; appState.state = event.state;
clearInterval(elapsedTimer); clearTimer();
layout.playpause.label = " > "; layout.playpause.label = " > ";
break; break;
case PlaybackState.previous: case PlaybackState.previous:
@ -173,13 +183,12 @@ function processMusicEvent(event) {
break; break;
} }
// Render track info on song change // Re-render track info on song change
if (event.track != layout.title.label) { if (event.track != layout.title.label) {
clearInterval(elapsedTimer); position = 0;
elapsedTimer = setInterval(updateTime, 1000);
layout.title.label = event ? event.track : "Track N/A"; layout.title.label = event ? event.track : "Track N/A";
layout.artist.label = event ? event.artist : "Artist N/A"; layout.artist.label = event ? event.artist : "Artist N/A";
layout.duration.label = formatTime(0); layout.duration.label = formatTime(event.dur);
}; };
draw(); draw();