From 4f8edc0b43b1c87636bd4013bbd66eba5a9192d9 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 28 Nov 2024 20:26:00 -0500 Subject: [PATCH] add volume and seek slider functionality --- static/pyrite.js | 70 ++++++++++++++++++++++++++++++++++++++++---- templates/index.html | 11 ++++--- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/static/pyrite.js b/static/pyrite.js index 510b7a5..33b40ee 100644 --- a/static/pyrite.js +++ b/static/pyrite.js @@ -2,11 +2,14 @@ var player; var pos_int; var analyser; var audio_data; +var vol_prev; function init() { player = document.querySelector('#player'); - player.addEventListener("ended", nextTrack); + player.addEventListener('ended', nextTrack); + player.addEventListener('loadedmetadata', display_duration); + player.addEventListener('timeupdate', update_position); navigator.mediaSession.setActionHandler('play', playTrack); navigator.mediaSession.setActionHandler('pause', pauseTrack); @@ -14,7 +17,23 @@ function init() { navigator.mediaSession.setActionHandler('previoustrack', prevTrack); navigator.mediaSession.setActionHandler('nexttrack', nextTrack); - pos_int = setInterval(update_position, 300); + let seek = document.querySelector('#seek-slider'); + seek.addEventListener('input', function(){ + player.currentTime = seek.value; + display_current_time(); + }); + + let vol = document.querySelector('#volume-slider'); + vol.addEventListener('input', function(){ + player.volume = vol.value/100; + if (vol.value == 0) { + document.querySelector('#vol-btn').style.display = 'none'; + document.querySelector('#vol-off-btn').style.display = 'initial'; + } else { + document.querySelector('#vol-btn').style.display = 'initial'; + document.querySelector('#vol-off-btn').style.display = 'none'; + } + }); init_analyzer(); } @@ -74,15 +93,54 @@ function setMediaSession(track) { } function update_position() { - if (player.paused){return;} - if (player.duration > 0 === false){return;} navigator.mediaSession.setPositionState({ - duration: parseInt(player.duration), + duration: Math.floor(player.duration), playbackRate: player.playbackRate, - position: parseInt(player.currentTime) + position: Math.floor(player.currentTime) }); analyser.getByteTimeDomainData(audio_data); + + document.querySelector('#seek-slider').value = Math.floor(player.currentTime); + display_current_time(); +} + +function calculate_time(raw_secs) { + let mins = Math.floor(raw_secs / 60); + let secs = Math.round(raw_secs % 60); + let ret = mins + ':' + String(secs).padStart(2,'0'); + return ret; +} + +function display_duration() { + let dur = document.querySelector('#duration'); + dur.textContent = calculate_time(player.duration); + + document.querySelector('#seek-slider').max = Math.floor(player.duration); +} + +function display_current_time() { + cur = document.querySelector('#current-time'); + cur.textContent = calculate_time(player.currentTime); +} + +function volume_off() { + let vol = document.querySelector('#volume-slider'); + vol_prev = vol.value; + vol.value = 0; + player.volume = 0; + + document.querySelector('#vol-btn').style.display = 'none'; + document.querySelector('#vol-off-btn').style.display = 'initial'; +} + +function volume_restore() { + let vol = document.querySelector('#volume-slider'); + vol.value = vol_prev; + player.volume = vol_prev / 100; + + document.querySelector('#vol-btn').style.display = 'initial'; + document.querySelector('#vol-off-btn').style.display = 'none'; } function init_analyzer() { diff --git a/templates/index.html b/templates/index.html index d51a527..519b75f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -46,13 +46,16 @@ - - + +
- + 0:00/0:00