@audiowalk/sdk 1.5.3 → 1.5.4

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,22 @@ 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
+ play(params?: {
37
+ fade?: boolean;
38
+ }): Promise<void>;
36
39
  pause(): Promise<void>;
37
- setVolume(volume: number): void;
40
+ stop(params?: {
41
+ fade?: boolean;
42
+ }): Promise<void>;
38
43
  seekTo(seconds: number): void;
44
+ setVolume(volume: number, params?: {
45
+ fade?: boolean;
46
+ }): void;
47
+ fadeToVolume(volume: number): Promise<void>;
39
48
  back(seconds?: number): void;
40
49
  forward(seconds?: number): void;
41
50
  private savePosition;
@@ -1 +1 @@
1
- {"version":3,"file":"player-controller.d.ts","sourceRoot":"","sources":["../../src/player/player-controller.ts"],"names":[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AQ,EAAE,MAAM,EAAE,OAAO,GAAE,sBAA2B;IAiE7E,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAQ7B,OAAO,CAAC,GAAG,GAAE,OAAe;IAa5B,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;IAexE,MAAM,CAAC,OAAO,EAAE,MAAM;IAQtB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAO;IAYnD,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,29 @@ 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
86
+ async destroy(now = false) {
87
+ this.log("Called destroy", now ? "now" : "");
88
+ if (this.options.crossfade && now !== true) {
97
89
  setTimeout(() => this.destroy(true), this.options.crossfadeTime);
90
+ return;
98
91
  }
99
- else {
100
- this.destroyEvent.next();
101
- await this.pause();
102
- this.playerElement.remove();
103
- }
92
+ this.log("Destroying player");
93
+ this.destroyEvent.next();
94
+ this.playerElement.remove();
104
95
  }
105
- async play() {
96
+ async play(params = { fade: this.options.crossfade }) {
106
97
  if (!this.playerElement.src)
107
98
  throw new Error("No file opened");
108
- this.log("Called play");
99
+ if (this.status.value === PlayerStatus.playing)
100
+ return;
101
+ this.log("Called play", params.fade ? "with fade" : "");
102
+ if (params.fade) {
103
+ this.playerElement.volume = 0;
104
+ }
109
105
  await this.playerElement?.play();
106
+ if (params.fade) {
107
+ await this.fadeToVolume(this.volume);
108
+ }
110
109
  }
111
110
  async pause() {
112
111
  if (!this.playerElement.src)
@@ -116,25 +115,64 @@ export class PlayerController {
116
115
  this.log("Called pause");
117
116
  this.playerElement?.pause();
118
117
  }
119
- setVolume(volume) {
120
- this.volume = volume;
121
- this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
118
+ async stop(params = { fade: this.options.crossfade }) {
119
+ this.log("Called stop", params.fade ? "with fade" : "");
120
+ if (this.status.value !== PlayerStatus.ended) {
121
+ this.onStop.next();
122
+ this.status.next(PlayerStatus.ended);
123
+ }
124
+ if (params.fade) {
125
+ await this.fadeToVolume(0);
126
+ }
127
+ this.playerElement.pause();
122
128
  }
123
129
  seekTo(seconds) {
124
130
  if (!this.playerElement.src)
125
131
  throw new Error("No file opened");
132
+ if (this.status.value === PlayerStatus.ended)
133
+ return;
126
134
  this.log("Called seekTo");
127
135
  this.playerElement.currentTime = seconds;
128
136
  }
137
+ setVolume(volume, params = {}) {
138
+ this.log("Called set volume", volume, params.fade ? "with fade" : "");
139
+ this.volume = volume;
140
+ if (params.fade) {
141
+ this.fadeToVolume(volume);
142
+ }
143
+ else {
144
+ this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
145
+ }
146
+ }
147
+ async fadeToVolume(volume) {
148
+ return new Promise((resolve, reject) => {
149
+ this.fadeCancelEvent.next();
150
+ this.volume = volume;
151
+ const fadeOutInterval = 100;
152
+ const fadeOutStep = (this.volume - this.playerElement.volume) / (this.options.crossfadeTime / fadeOutInterval);
153
+ if (fadeOutStep === 0)
154
+ return resolve();
155
+ interval(fadeOutInterval)
156
+ .pipe(takeUntil(this.fadeCancelEvent))
157
+ .pipe(takeUntil(this.destroyEvent))
158
+ .pipe(takeWhile(() => Math.abs(this.playerElement.volume - this.volume) > Math.abs(fadeOutStep)))
159
+ .subscribe({
160
+ next: () => {
161
+ this.playerElement.volume += fadeOutStep;
162
+ },
163
+ error: (error) => reject(error),
164
+ complete: () => {
165
+ this.playerElement.volume = this.volume;
166
+ resolve();
167
+ },
168
+ });
169
+ });
170
+ }
129
171
  back(seconds = 10) {
130
- if (!this.playerElement.src)
131
- throw new Error("No file opened");
132
172
  const position = this.playerElement.currentTime;
133
173
  this.seekTo(Math.max(position - seconds, 0));
134
174
  }
135
175
  forward(seconds = 10) {
136
- if (!this.playerElement.src)
137
- throw new Error("No file opened");
138
176
  const position = this.playerElement.currentTime;
139
177
  const duration = this.playerElement.duration;
140
178
  this.seekTo(duration && duration > 0 ? Math.min(position + seconds, duration) : position + seconds);
@@ -147,7 +185,7 @@ export class PlayerController {
147
185
  log(message, ...args) {
148
186
  const time = this.playerElement?.currentTime ? Math.round(this.playerElement?.currentTime) : null;
149
187
  if (time)
150
- message += ` @${time}s`;
188
+ args.push(`@${time}s`);
151
189
  console.log(`[PlayerController] ${message}`, ...args);
152
190
  }
153
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audiowalk/sdk",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/audiowalk-cz/components.git"