add hardware control

This commit is contained in:
lelo 2025-03-21 22:30:34 +01:00
parent 8e3d5cb7c3
commit 5ace803cc0
2 changed files with 50 additions and 4 deletions

View File

@ -56,7 +56,7 @@ function renderContent(data) {
let symbol = '📄'; let symbol = '📄';
if (file.file_type === 'music') { if (file.file_type === 'music') {
symbol = '🔊'; symbol = '🔊';
currentMusicFiles.push({ path: file.path, index: idx }); currentMusicFiles.push({ path: file.path, index: idx, title: file.name.replace('.mp3', '') });
} else if (file.file_type === 'image') { } else if (file.file_type === 'image') {
symbol = '🖼️'; symbol = '🖼️';
} }
@ -235,9 +235,20 @@ document.querySelectorAll('.play-file').forEach(link => {
audioPlayer.play(); audioPlayer.play();
playerButton.innerHTML = pauseIcon; playerButton.innerHTML = pauseIcon;
const pathParts = relUrl.split('/'); const pathParts = relUrl.split('/');
const folderName = pathParts[pathParts.length - 2];
const fileName = pathParts.pop(); const fileName = pathParts.pop();
const pathStr = pathParts.join('/'); const pathStr = pathParts.join('/');
nowPlayingInfo.innerHTML = pathStr.replace(/\//g, ' > ') + '<br><span style="font-size: larger; font-weight: bold;">' + fileName.replace('.mp3', '') + '</span>'; nowPlayingInfo.innerHTML = pathStr.replace(/\//g, ' > ') + '<br><span style="font-size: larger; font-weight: bold;">' + fileName.replace('.mp3', '') + '</span>';
// write into hardware player
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: currentMusicFiles[currentMusicIndex].title,
artist: folderName,
artwork: [
{ src: '/static/icons/logo-512x512.png', sizes: '512x512', type: 'image/png' }
]
});
};
preload_audio(); preload_audio();
} }
} catch (error) { } catch (error) {
@ -308,7 +319,42 @@ window.addEventListener('popstate', function (event) {
}); });
// Add an event listener for the audio 'ended' event to auto-play the next music file. if ('mediaSession' in navigator) {
// Handler for the play action
navigator.mediaSession.setActionHandler('play', () => {
document.getElementById('globalAudio').play();
});
// Handler for the pause action
navigator.mediaSession.setActionHandler('pause', () => {
document.getElementById('globalAudio').pause();
});
// Handler for the previous track action
navigator.mediaSession.setActionHandler('previoustrack', () => {
if (currentMusicIndex > 0) {
const prevFile = currentMusicFiles[currentMusicIndex - 1];
const prevLink = document.querySelector(`.play-file[data-url="${prevFile.path}"]`);
if (prevLink) {
prevLink.click();
}
}
});
// Handler for the next track action
navigator.mediaSession.setActionHandler('nexttrack', () => {
if (currentMusicIndex >= 0 && currentMusicIndex < currentMusicFiles.length - 1) {
const nextFile = currentMusicFiles[currentMusicIndex + 1];
const nextLink = document.querySelector(`.play-file[data-url="${nextFile.path}"]`);
if (nextLink) {
nextLink.click();
}
}
});
}
// auto-play on audio 'ended' event logic
document.getElementById('globalAudio').addEventListener('ended', () => { document.getElementById('globalAudio').addEventListener('ended', () => {
// Check if there's a next file in the array. // Check if there's a next file in the array.
if (currentMusicIndex >= 0 && currentMusicIndex < currentMusicFiles.length - 1) { if (currentMusicIndex >= 0 && currentMusicIndex < currentMusicFiles.length - 1) {

View File

@ -102,8 +102,8 @@ audio.ontimeupdate = function() {
changeTimelinePosition(); changeTimelinePosition();
updateTimeInfo(); updateTimeInfo();
if ('mediaSession' in navigator && if ('mediaSession' in navigator &&
typeof navigator.mediaSession.setPositionState === 'function' && 'setPositionState' in navigator.mediaSession &&
!isNaN(audio.duration)) { !isNaN(audio.duration)) {
navigator.mediaSession.setPositionState({ navigator.mediaSession.setPositionState({
duration: audio.duration, duration: audio.duration,
playbackRate: audio.playbackRate, playbackRate: audio.playbackRate,