@axium/client 0.36.1 → 0.36.3
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 +3 -2
- package/lib/reactive/media.svelte.ts +69 -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;
|
|
@@ -39,11 +41,10 @@
|
|
|
39
41
|
artist: common.artist,
|
|
40
42
|
artwork: pictureURL && picture ? [{ src: pictureURL, type: picture.format }] : [],
|
|
41
43
|
});
|
|
44
|
+
media.attachToSession();
|
|
42
45
|
}
|
|
43
46
|
});
|
|
44
47
|
});
|
|
45
|
-
|
|
46
|
-
const media = new MediaState();
|
|
47
48
|
</script>
|
|
48
49
|
|
|
49
50
|
<div class="Audio" onkeydown={media.keydown}>
|
|
@@ -76,11 +76,13 @@ export class MediaState {
|
|
|
76
76
|
e.preventDefault();
|
|
77
77
|
if (!this.knownDuration) break;
|
|
78
78
|
this.currentTime = Math.max(0, this.currentTime - 10);
|
|
79
|
+
this.updateAttached();
|
|
79
80
|
break;
|
|
80
81
|
case 'ArrowRight':
|
|
81
82
|
e.preventDefault();
|
|
82
83
|
if (!this.knownDuration) break;
|
|
83
84
|
this.currentTime = Math.min(this.knownDuration, this.currentTime + 10);
|
|
85
|
+
this.updateAttached();
|
|
84
86
|
break;
|
|
85
87
|
case 'ArrowUp':
|
|
86
88
|
this.volume = Math.min(1, this.volume + 0.1);
|
|
@@ -107,4 +109,71 @@ export class MediaState {
|
|
|
107
109
|
break;
|
|
108
110
|
}
|
|
109
111
|
};
|
|
112
|
+
|
|
113
|
+
/** Seek to a time, clamped to the media's duration when known */
|
|
114
|
+
seek(time: number) {
|
|
115
|
+
this.currentTime = this.knownDuration ? Math.min(this.knownDuration, Math.max(0, time)) : Math.max(0, time);
|
|
116
|
+
this.updateAttached();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected isAttached: boolean = false;
|
|
120
|
+
|
|
121
|
+
protected updateAttached() {
|
|
122
|
+
if (!this.isAttached) return;
|
|
123
|
+
|
|
124
|
+
globalThis.navigator?.mediaSession?.setPositionState({
|
|
125
|
+
duration: this.knownDuration,
|
|
126
|
+
position: this.currentTime,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
attachToSession(next?: (backward: boolean) => void) {
|
|
131
|
+
const session = globalThis.navigator?.mediaSession;
|
|
132
|
+
|
|
133
|
+
if (!session) return;
|
|
134
|
+
|
|
135
|
+
this.isAttached = true;
|
|
136
|
+
|
|
137
|
+
session.setActionHandler('play', () => {
|
|
138
|
+
if (this.ended) this.currentTime = 0;
|
|
139
|
+
this.paused = false;
|
|
140
|
+
this.updateAttached();
|
|
141
|
+
});
|
|
142
|
+
session.setActionHandler('pause', () => {
|
|
143
|
+
this.paused = true;
|
|
144
|
+
});
|
|
145
|
+
session.setActionHandler('stop', () => {
|
|
146
|
+
this.paused = true;
|
|
147
|
+
this.currentTime = 0;
|
|
148
|
+
this.updateAttached();
|
|
149
|
+
});
|
|
150
|
+
session.setActionHandler('seekbackward', details => {
|
|
151
|
+
if (!this.knownDuration) return;
|
|
152
|
+
this.seek(this.currentTime - (details.seekOffset ?? 10));
|
|
153
|
+
});
|
|
154
|
+
session.setActionHandler('seekforward', details => {
|
|
155
|
+
if (!this.knownDuration) return;
|
|
156
|
+
this.seek(this.currentTime + (details.seekOffset ?? 10));
|
|
157
|
+
});
|
|
158
|
+
session.setActionHandler('seekto', details => {
|
|
159
|
+
if (details.seekTime === undefined || details.seekTime === null) return;
|
|
160
|
+
if (details.fastSeek && this.element?.fastSeek) {
|
|
161
|
+
this.element.fastSeek(details.seekTime);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this.seek(details.seekTime);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (!next) return;
|
|
168
|
+
|
|
169
|
+
session.setActionHandler('previoustrack', () => {
|
|
170
|
+
// Restart the current track instead when we're already a few seconds in
|
|
171
|
+
if (this.currentTime > 3) {
|
|
172
|
+
this.currentTime = 0;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
next(true);
|
|
176
|
+
});
|
|
177
|
+
session.setActionHandler('nexttrack', () => next(false));
|
|
178
|
+
}
|
|
110
179
|
}
|