Skip to content

Commit dc977c2

Browse files
committed
Add playback range in audio element
1 parent ae31d08 commit dc977c2

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Diff for: assets/js/song.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ function songPage() {
5959
if (audioElement.duration == NaN) {
6060
audioElement.src = document.getElementsByClassName("source")[0].src;
6161
}
62-
63-
var start = document.getElementsByName("audio-start");
64-
if (start.length > 0 && start[0].value > 0) {
65-
audioElement.currentTime = start[0].value
66-
}
6762

6863
// initialize mediaSession metadata
6964
if ('mediaSession' in navigator) {
@@ -75,7 +70,7 @@ function songPage() {
7570
artwork.push({ src: coverImage.src + "?size=small", sizes: "120x120" });
7671
artwork.push({ src: coverImage.src, sizes: coverImage.dataset.size + "x" + coverImage.dataset.size });
7772
}
78-
73+
7974
navigator.mediaSession.metadata = new MediaMetadata({
8075
title: document.getElementById("song-title").value,
8176
artist: document.getElementById("song-artist").value,

Diff for: assets/js/song.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: store/music/entry.go

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package music
22

33
import (
44
"fmt"
5+
"math"
56
"path/filepath"
67
"strings"
78
"time"
@@ -128,6 +129,29 @@ type AudioSettings struct {
128129
End float64 `json:"end"`
129130
}
130131

132+
// TimeRange returns the playback range for HTML media elements
133+
// See https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery#specifying_playback_range
134+
func (e *Entry) PlaybackRange() string {
135+
// if end time == duration
136+
if e.AudioSettings.End <= 0 || math.Abs(e.AudioSettings.End-e.MusicData.Duration) < 0.001 {
137+
// if we also have no start time, then neither was set -- return nothing
138+
if e.AudioSettings.Start <= 0.001 {
139+
return ""
140+
}
141+
142+
// Only the start time is set
143+
return fmt.Sprintf("#t=%f", e.AudioSettings.Start)
144+
}
145+
146+
if e.AudioSettings.Start <= 0.001 {
147+
// Only the end time is set
148+
return fmt.Sprintf("#t=0,%f", e.AudioSettings.End)
149+
}
150+
151+
// both are set
152+
return fmt.Sprintf("#t=%f,%f", e.AudioSettings.Start, e.AudioSettings.End)
153+
}
154+
131155
type MusicData struct {
132156
Title string `json:"title"`
133157
Artist string `json:"artist"`

Diff for: templates/song.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@
132132
<div class="field">
133133
<div class="control">
134134
<audio preload="none" class="audio-controls" controls="">
135-
<source src="/song/{{.ID}}/audio">
136-
<source src="/song/{{.ID}}/mp3" type="audio/mpeg">
135+
<source src="/song/{{.ID}}/audio{{.PlaybackRange}}">
136+
<source src="/song/{{.ID}}/mp3{{.PlaybackRange}}" type="audio/mpeg">
137137
It seems like your browser doesn't support playing audio.
138138
</audio>
139139
</div>

0 commit comments

Comments
 (0)