diff --git a/assets/js/song.js b/assets/js/song.js index def91de..c316741 100644 --- a/assets/js/song.js +++ b/assets/js/song.js @@ -59,11 +59,6 @@ function songPage() { if (audioElement.duration == NaN) { audioElement.src = document.getElementsByClassName("source")[0].src; } - - var start = document.getElementsByName("audio-start"); - if (start.length > 0 && start[0].value > 0) { - audioElement.currentTime = start[0].value - } // initialize mediaSession metadata if ('mediaSession' in navigator) { @@ -75,7 +70,7 @@ function songPage() { artwork.push({ src: coverImage.src + "?size=small", sizes: "120x120" }); artwork.push({ src: coverImage.src, sizes: coverImage.dataset.size + "x" + coverImage.dataset.size }); } - + navigator.mediaSession.metadata = new MediaMetadata({ title: document.getElementById("song-title").value, artist: document.getElementById("song-artist").value, diff --git a/assets/js/song.min.js b/assets/js/song.min.js index f6301c8..2826f59 100644 --- a/assets/js/song.min.js +++ b/assets/js/song.min.js @@ -1 +1 @@ -function songPage(){function confirmDelete(evt){if(evt.preventDefault(),!confirm("Are you sure you want to delete this song?"))return!1;var formData=new FormData;return formData.set("delete","delete"),ajax(location.pathname,formData).post((function(status,obj){200===status?(isReload=!0,InstantClick.go("/")):document.getElementById("song-notif").innerText=obj.message||"Unknown error"})),!1}function confirmDeleteCover(evt){if(evt.preventDefault(),!confirm("Are you sure you want to delete the cover?"))return!1;var formData=new FormData;return formData.set("delete-cover","delete-cover"),ajax(location.pathname,formData).post((function(status,obj){200!==status&&(document.getElementById("song-notif").innerText=obj.message||"Unknown error")})),!1}registerCover(),document.getElementById("delete-button").addEventListener("click",confirmDelete);var dc=document.getElementById("delete-cover");function saveVolumeChange(evt){localStorage.setItem("audio-volume",evt.target.volume)}dc&&dc.addEventListener("click",confirmDeleteCover);var audioElement=document.getElementsByTagName("audio")[0];NaN==audioElement.duration&&(audioElement.src=document.getElementsByClassName("source")[0].src);var start=document.getElementsByName("audio-start");if(start.length>0&&start[0].value>0&&(audioElement.currentTime=start[0].value),"mediaSession"in navigator){var coverSizeSpan=document.querySelector(".cover-image-size"),coverImage=document.getElementById("song-cover"),artwork=[];coverSizeSpan&&coverImage&&(artwork.push({src:coverImage.src+"?size=small",sizes:"120x120"}),artwork.push({src:coverImage.src,sizes:coverImage.dataset.size+"x"+coverImage.dataset.size})),navigator.mediaSession.metadata=new MediaMetadata({title:document.getElementById("song-title").value,artist:document.getElementById("song-artist").value,album:document.getElementById("song-album").value,artwork:artwork})}audioElement.load(),audioElement.volume=localStorage.getItem("audio-volume")||0,0==audioElement.volume&&(audioElement.volume=.5),audioElement.addEventListener("volumechange",saveVolumeChange);var songDownloadButton=document.getElementById("download-song-button");function removeLoading(evt){songDownloadButton.classList.remove("is-loading")}function downloadButtonClicked(){songDownloadButton.classList.add("is-loading")}songDownloadButton.addEventListener("click",downloadButtonClicked),songDownloadButton.addEventListener("blur",removeLoading)} \ No newline at end of file +function songPage(){function confirmDelete(evt){if(evt.preventDefault(),!confirm("Are you sure you want to delete this song?"))return!1;var formData=new FormData;return formData.set("delete","delete"),ajax(location.pathname,formData).post((function(status,obj){200===status?(isReload=!0,InstantClick.go("/")):document.getElementById("song-notif").innerText=obj.message||"Unknown error"})),!1}function confirmDeleteCover(evt){if(evt.preventDefault(),!confirm("Are you sure you want to delete the cover?"))return!1;var formData=new FormData;return formData.set("delete-cover","delete-cover"),ajax(location.pathname,formData).post((function(status,obj){200!==status&&(document.getElementById("song-notif").innerText=obj.message||"Unknown error")})),!1}registerCover(),document.getElementById("delete-button").addEventListener("click",confirmDelete);var dc=document.getElementById("delete-cover");function saveVolumeChange(evt){localStorage.setItem("audio-volume",evt.target.volume)}dc&&dc.addEventListener("click",confirmDeleteCover);var audioElement=document.getElementsByTagName("audio")[0];if(NaN==audioElement.duration&&(audioElement.src=document.getElementsByClassName("source")[0].src),"mediaSession"in navigator){var coverSizeSpan=document.querySelector(".cover-image-size"),coverImage=document.getElementById("song-cover"),artwork=[];coverSizeSpan&&coverImage&&(artwork.push({src:coverImage.src+"?size=small",sizes:"120x120"}),artwork.push({src:coverImage.src,sizes:coverImage.dataset.size+"x"+coverImage.dataset.size})),navigator.mediaSession.metadata=new MediaMetadata({title:document.getElementById("song-title").value,artist:document.getElementById("song-artist").value,album:document.getElementById("song-album").value,artwork:artwork})}audioElement.load(),audioElement.volume=localStorage.getItem("audio-volume")||0,0==audioElement.volume&&(audioElement.volume=.5),audioElement.addEventListener("volumechange",saveVolumeChange);var songDownloadButton=document.getElementById("download-song-button");function removeLoading(evt){songDownloadButton.classList.remove("is-loading")}function downloadButtonClicked(){songDownloadButton.classList.add("is-loading")}songDownloadButton.addEventListener("click",downloadButtonClicked),songDownloadButton.addEventListener("blur",removeLoading)} \ No newline at end of file diff --git a/store/music/entry.go b/store/music/entry.go index e954aa0..1353e99 100644 --- a/store/music/entry.go +++ b/store/music/entry.go @@ -2,6 +2,7 @@ package music import ( "fmt" + "math" "path/filepath" "strings" "time" @@ -128,6 +129,29 @@ type AudioSettings struct { End float64 `json:"end"` } +// TimeRange returns the playback range for HTML media elements +// See https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery#specifying_playback_range +func (e *Entry) PlaybackRange() string { + // if end time == duration + if e.AudioSettings.End <= 0 || math.Abs(e.AudioSettings.End-e.MusicData.Duration) < 0.001 { + // if we also have no start time, then neither was set -- return nothing + if e.AudioSettings.Start <= 0.001 { + return "" + } + + // Only the start time is set + return fmt.Sprintf("#t=%f", e.AudioSettings.Start) + } + + if e.AudioSettings.Start <= 0.001 { + // Only the end time is set + return fmt.Sprintf("#t=0,%f", e.AudioSettings.End) + } + + // both are set + return fmt.Sprintf("#t=%f,%f", e.AudioSettings.Start, e.AudioSettings.End) +} + type MusicData struct { Title string `json:"title"` Artist string `json:"artist"` diff --git a/templates/song.html b/templates/song.html index be8cd14..1712788 100644 --- a/templates/song.html +++ b/templates/song.html @@ -132,8 +132,8 @@