@axium/client 0.36.1 → 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 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}>
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/client",
3
- "version": "0.36.1",
3
+ "version": "0.36.2",
4
4
  "author": "James Prevett <jp@jamespre.dev>",
5
5
  "funding": {
6
6
  "type": "individual",