@audiowalk/sdk 1.5.3 → 1.5.5

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.
@@ -34,7 +34,7 @@ export class MediaControlsController {
34
34
  .pipe(takeUntil(this.playerDetachEvent))
35
35
  .subscribe(([currentTime, totalTime]) => {
36
36
  navigator.mediaSession.setPositionState({
37
- duration: Number.isNaN(totalTime) ? 0 : totalTime,
37
+ duration: totalTime || 0,
38
38
  position: currentTime,
39
39
  });
40
40
  });
@@ -18,8 +18,8 @@ export declare class PlayerController {
18
18
  readonly onPause: Subject<void>;
19
19
  readonly onStop: Subject<void>;
20
20
  readonly currentTime: BehaviorSubject<number>;
21
- readonly totalTime: BehaviorSubject<number>;
22
- readonly progress: import("rxjs").Observable<number>;
21
+ readonly totalTime: BehaviorSubject<number | null>;
22
+ readonly progress: import("rxjs").Observable<number | null>;
23
23
  private trackId;
24
24
  readonly status: BehaviorSubject<PlayerStatus | null>;
25
25
  readonly playing: import("rxjs").Observable<boolean>;
@@ -29,13 +29,23 @@ export declare class PlayerController {
29
29
  private defaultOptions;
30
30
  private destroyEvent;
31
31
  private volume;
32
+ private fadeCancelEvent;
32
33
  constructor(trackId: string, trackUrl: string, options?: PlayerControllerParams);
33
34
  open(id: string, file: string): Promise<void>;
34
35
  destroy(now?: boolean): Promise<void>;
35
- play(): Promise<void>;
36
+ private destroyNow;
37
+ play(params?: {
38
+ fade?: boolean;
39
+ }): Promise<void>;
36
40
  pause(): Promise<void>;
37
- setVolume(volume: number): void;
41
+ stop(params?: {
42
+ fade?: boolean;
43
+ }): Promise<void>;
38
44
  seekTo(seconds: number): void;
45
+ setVolume(volume: number, params?: {
46
+ fade?: boolean;
47
+ }): Promise<void>;
48
+ fadeToVolume(volume: number): Promise<void>;
39
49
  back(seconds?: number): void;
40
50
  forward(seconds?: number): void;
41
51
  private savePosition;
@@ -1 +1 @@
1
- {"version":3,"file":"player-controller.d.ts","sourceRoot":"","sources":["../../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EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,sBAA2B;IAgE7E,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAQ7B,OAAO,CAAC,GAAG,GAAE,OAAe;IAgBlC,OAAO,CAAC,UAAU;IAQZ,IAAI,CAAC,MAAM,GAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAqC;IAiBlE,KAAK;IAQL,IAAI,CAAC,MAAM,GAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAqC;IAgBxE,MAAM,CAAC,OAAO,EAAE,MAAM;IAQhB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAO;IAYzD,YAAY,CAAC,MAAM,EAAE,MAAM;IA4BjC,IAAI,CAAC,OAAO,GAAE,MAAW;IAKzB,OAAO,CAAC,OAAO,GAAE,MAAW;YAMd,YAAY;IAK1B,OAAO,CAAC,GAAG;CAOZ"}
@@ -1,4 +1,4 @@
1
- import { BehaviorSubject, combineLatest, map, Subject, takeUntil } from "rxjs";
1
+ import { BehaviorSubject, combineLatest, filter, interval, map, Subject, takeUntil, takeWhile } from "rxjs";
2
2
  import { LocalStorage } from "../storage/local-storage";
3
3
  export var PlayerStatus;
4
4
  (function (PlayerStatus) {
@@ -11,8 +11,8 @@ export class PlayerController {
11
11
  onPause = new Subject();
12
12
  onStop = new Subject();
13
13
  currentTime = new BehaviorSubject(0);
14
- totalTime = new BehaviorSubject(0);
15
- progress = combineLatest([this.currentTime, this.totalTime]).pipe(map(([currentTime, totalTime]) => currentTime / totalTime));
14
+ totalTime = new BehaviorSubject(null);
15
+ progress = combineLatest([this.currentTime, this.totalTime]).pipe(map(([currentTime, totalTime]) => (totalTime ? currentTime / totalTime : null)));
16
16
  trackId = null;
17
17
  status = new BehaviorSubject(null);
18
18
  playing = this.status.pipe(map((status) => status === "playing"));
@@ -24,9 +24,11 @@ export class PlayerController {
24
24
  };
25
25
  destroyEvent = new Subject();
26
26
  volume = 1;
27
+ fadeCancelEvent = new Subject();
27
28
  constructor(trackId, trackUrl, options = {}) {
28
29
  this.options = { ...this.defaultOptions, ...options };
29
30
  this.playerElement = this.options.audioElement ?? new Audio();
31
+ this.playerElement.volume = this.volume;
30
32
  this.playerElement.addEventListener("play", () => {
31
33
  this.onPlay.next();
32
34
  this.status.next(PlayerStatus.playing);
@@ -35,12 +37,12 @@ export class PlayerController {
35
37
  this.onPause.next();
36
38
  this.status.next(PlayerStatus.paused);
37
39
  });
38
- if (!this.options.crossfade && !this.options.loop) {
39
- this.playerElement.addEventListener("ended", () => {
40
+ this.playerElement.addEventListener("ended", () => {
41
+ if (!this.options.crossfade && !this.options.loop) {
40
42
  this.onStop.next();
41
43
  this.status.next(PlayerStatus.ended);
42
- });
43
- }
44
+ }
45
+ });
44
46
  this.playerElement.addEventListener("loadedmetadata", (event) => {
45
47
  if (this.playerElement.duration) {
46
48
  this.totalTime.next(this.playerElement.duration);
@@ -58,21 +60,11 @@ export class PlayerController {
58
60
  if (this.options.crossfade && !this.options.loop) {
59
61
  combineLatest([this.currentTime, this.totalTime])
60
62
  .pipe(takeUntil(this.destroyEvent))
63
+ .pipe(filter(([currentTime, totalTime]) => !!totalTime)) // track is loaded
64
+ .pipe(filter(([currentTime, totalTime]) => currentTime >= totalTime - this.options.crossfadeTime / 1000)) // crossfading should start
61
65
  .subscribe(([currentTime, totalTime]) => {
62
- const crossfadeTime = this.options.crossfadeTime / 1000;
63
- if (currentTime >= totalTime - crossfadeTime) {
64
- this.playerElement.volume = (totalTime - currentTime) / crossfadeTime;
65
- if (this.status.value !== PlayerStatus.ended) {
66
- this.onStop.next();
67
- this.status.next(PlayerStatus.ended);
68
- }
69
- }
70
- else if (currentTime < crossfadeTime) {
71
- this.playerElement.volume = currentTime / crossfadeTime;
72
- }
73
- else {
74
- this.playerElement.volume = this.volume;
75
- }
66
+ if (this.status.value !== PlayerStatus.ended)
67
+ this.stop();
76
68
  });
77
69
  }
78
70
  if (this.options.loop) {
@@ -82,7 +74,7 @@ export class PlayerController {
82
74
  });
83
75
  }
84
76
  this.open(trackId, trackUrl);
85
- this.log("Initialized player", this.playerElement);
77
+ this.log("Initialized player", trackId);
86
78
  }
87
79
  async open(id, file) {
88
80
  this.trackId = id;
@@ -91,22 +83,40 @@ export class PlayerController {
91
83
  if (position && this.options.autoSave)
92
84
  this.playerElement.currentTime = position;
93
85
  }
94
- async destroy(now) {
95
- if (this.options.crossfade) {
96
- // destroy later to enable fade out
97
- setTimeout(() => this.destroy(true), this.options.crossfadeTime);
86
+ async destroy(now = false) {
87
+ this.log("Called destroy", now ? "now" : "");
88
+ if (this.status.value !== PlayerStatus.ended) {
89
+ await this.stop();
90
+ return this.destroyNow();
91
+ }
92
+ // if crossfade is enabled, we might be ending crossfade just now, so wait for the crossfade to finish
93
+ if (this.options.crossfade && now !== true) {
94
+ setTimeout(() => this.destroyNow(), this.options.crossfadeTime);
98
95
  }
99
96
  else {
100
- this.destroyEvent.next();
101
- await this.pause();
102
- this.playerElement.remove();
97
+ this.destroyNow();
103
98
  }
104
99
  }
105
- async play() {
100
+ destroyNow() {
101
+ this.log("Destroying player");
102
+ this.destroyEvent.next();
103
+ this.playerElement.src = "";
104
+ this.playerElement.pause();
105
+ this.playerElement.remove();
106
+ }
107
+ async play(params = { fade: this.options.crossfade }) {
106
108
  if (!this.playerElement.src)
107
109
  throw new Error("No file opened");
108
- this.log("Called play");
110
+ if (this.status.value === PlayerStatus.playing)
111
+ return;
112
+ this.log("Called play", params.fade ? "with fade" : "");
113
+ if (params.fade) {
114
+ this.playerElement.volume = 0;
115
+ }
109
116
  await this.playerElement?.play();
117
+ if (params.fade) {
118
+ await this.fadeToVolume(this.volume);
119
+ }
110
120
  }
111
121
  async pause() {
112
122
  if (!this.playerElement.src)
@@ -116,25 +126,65 @@ export class PlayerController {
116
126
  this.log("Called pause");
117
127
  this.playerElement?.pause();
118
128
  }
119
- setVolume(volume) {
120
- this.volume = volume;
121
- this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
129
+ async stop(params = { fade: this.options.crossfade }) {
130
+ this.log("Called stop", params.fade ? "with fade" : "");
131
+ if (this.status.value !== PlayerStatus.ended) {
132
+ this.status.next(PlayerStatus.ended);
133
+ this.onStop.next();
134
+ }
135
+ if (params.fade) {
136
+ await this.fadeToVolume(0);
137
+ }
138
+ this.playerElement.pause();
139
+ this.playerElement.currentTime = 0;
122
140
  }
123
141
  seekTo(seconds) {
124
142
  if (!this.playerElement.src)
125
143
  throw new Error("No file opened");
144
+ if (this.status.value === PlayerStatus.ended)
145
+ return;
126
146
  this.log("Called seekTo");
127
147
  this.playerElement.currentTime = seconds;
128
148
  }
149
+ async setVolume(volume, params = {}) {
150
+ this.log("Called set volume", volume, params.fade ? "with fade" : "");
151
+ this.volume = volume;
152
+ if (params.fade) {
153
+ await this.fadeToVolume(volume);
154
+ }
155
+ else {
156
+ this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
157
+ }
158
+ }
159
+ async fadeToVolume(volume) {
160
+ return new Promise((resolve, reject) => {
161
+ this.fadeCancelEvent.next();
162
+ this.volume = volume;
163
+ const fadeOutInterval = 100;
164
+ const fadeOutStep = (this.volume - this.playerElement.volume) / (this.options.crossfadeTime / fadeOutInterval);
165
+ if (fadeOutStep === 0)
166
+ return resolve();
167
+ interval(fadeOutInterval)
168
+ .pipe(takeUntil(this.fadeCancelEvent))
169
+ .pipe(takeUntil(this.destroyEvent))
170
+ .pipe(takeWhile(() => Math.abs(this.playerElement.volume - this.volume) > Math.abs(fadeOutStep)))
171
+ .subscribe({
172
+ next: () => {
173
+ this.playerElement.volume += fadeOutStep;
174
+ },
175
+ error: (error) => reject(error),
176
+ complete: () => {
177
+ this.playerElement.volume = this.volume;
178
+ resolve();
179
+ },
180
+ });
181
+ });
182
+ }
129
183
  back(seconds = 10) {
130
- if (!this.playerElement.src)
131
- throw new Error("No file opened");
132
184
  const position = this.playerElement.currentTime;
133
185
  this.seekTo(Math.max(position - seconds, 0));
134
186
  }
135
187
  forward(seconds = 10) {
136
- if (!this.playerElement.src)
137
- throw new Error("No file opened");
138
188
  const position = this.playerElement.currentTime;
139
189
  const duration = this.playerElement.duration;
140
190
  this.seekTo(duration && duration > 0 ? Math.min(position + seconds, duration) : position + seconds);
@@ -147,7 +197,7 @@ export class PlayerController {
147
197
  log(message, ...args) {
148
198
  const time = this.playerElement?.currentTime ? Math.round(this.playerElement?.currentTime) : null;
149
199
  if (time)
150
- message += ` @${time}s`;
200
+ args.push(`@${time}s`);
151
201
  console.log(`[PlayerController] ${message}`, ...args);
152
202
  }
153
203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audiowalk/sdk",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/audiowalk-cz/components.git"