@axium/client 0.36.0 → 0.36.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Audio.svelte +12 -2
- package/lib/reactive/media.svelte.ts +51 -0
- package/package.json +1 -1
package/lib/Audio.svelte
CHANGED
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
pictureURL = $state<string>(),
|
|
19
19
|
audioInfo = $state<[string, (string | number | null)?][]>([]);
|
|
20
20
|
|
|
21
|
+
const media = new MediaState();
|
|
22
|
+
|
|
21
23
|
$effect(() => {
|
|
22
24
|
getMetadata(rest).then(result => {
|
|
23
25
|
if (!result) return;
|
|
@@ -31,10 +33,18 @@
|
|
|
31
33
|
['hashtag', common.track.no],
|
|
32
34
|
['compact-disc', common.disk.no],
|
|
33
35
|
] as const;
|
|
36
|
+
|
|
37
|
+
if ('mediaSession' in navigator) {
|
|
38
|
+
navigator.mediaSession.metadata = new MediaMetadata({
|
|
39
|
+
title: common.title,
|
|
40
|
+
album: common.album,
|
|
41
|
+
artist: common.artist,
|
|
42
|
+
artwork: pictureURL && picture ? [{ src: pictureURL, type: picture.format }] : [],
|
|
43
|
+
});
|
|
44
|
+
media.attachToSession();
|
|
45
|
+
}
|
|
34
46
|
});
|
|
35
47
|
});
|
|
36
|
-
|
|
37
|
-
const media = new MediaState();
|
|
38
48
|
</script>
|
|
39
49
|
|
|
40
50
|
<div class="Audio" onkeydown={media.keydown}>
|
|
@@ -107,4 +107,55 @@ export class MediaState {
|
|
|
107
107
|
break;
|
|
108
108
|
}
|
|
109
109
|
};
|
|
110
|
+
|
|
111
|
+
/** Seek to a time, clamped to the media's duration when known */
|
|
112
|
+
seek(time: number) {
|
|
113
|
+
this.currentTime = this.knownDuration ? Math.min(this.knownDuration, Math.max(0, time)) : Math.max(0, time);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
attachToSession(next?: (backward: boolean) => void) {
|
|
117
|
+
const session = globalThis.navigator?.mediaSession;
|
|
118
|
+
|
|
119
|
+
if (!session) return;
|
|
120
|
+
|
|
121
|
+
session.setActionHandler('play', () => {
|
|
122
|
+
if (this.ended) this.currentTime = 0;
|
|
123
|
+
this.paused = false;
|
|
124
|
+
});
|
|
125
|
+
session.setActionHandler('pause', () => {
|
|
126
|
+
this.paused = true;
|
|
127
|
+
});
|
|
128
|
+
session.setActionHandler('stop', () => {
|
|
129
|
+
this.paused = true;
|
|
130
|
+
this.currentTime = 0;
|
|
131
|
+
});
|
|
132
|
+
session.setActionHandler('seekbackward', details => {
|
|
133
|
+
if (!this.knownDuration) return;
|
|
134
|
+
this.seek(this.currentTime - (details.seekOffset ?? 10));
|
|
135
|
+
});
|
|
136
|
+
session.setActionHandler('seekforward', details => {
|
|
137
|
+
if (!this.knownDuration) return;
|
|
138
|
+
this.seek(this.currentTime + (details.seekOffset ?? 10));
|
|
139
|
+
});
|
|
140
|
+
session.setActionHandler('seekto', details => {
|
|
141
|
+
if (details.seekTime === undefined || details.seekTime === null) return;
|
|
142
|
+
if (details.fastSeek && this.element?.fastSeek) {
|
|
143
|
+
this.element.fastSeek(details.seekTime);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.seek(details.seekTime);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (!next) return;
|
|
150
|
+
|
|
151
|
+
session.setActionHandler('previoustrack', () => {
|
|
152
|
+
// Restart the current track instead when we're already a few seconds in
|
|
153
|
+
if (this.currentTime > 3) {
|
|
154
|
+
this.currentTime = 0;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
next(true);
|
|
158
|
+
});
|
|
159
|
+
session.setActionHandler('nexttrack', () => next(false));
|
|
160
|
+
}
|
|
110
161
|
}
|