@audiowalk/sdk 1.5.2 → 1.5.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/dist/helpers/objects.d.ts +2 -0
- package/dist/helpers/objects.d.ts.map +1 -0
- package/dist/helpers/objects.js +1 -0
- package/dist/player/player-controller.d.ts +10 -16
- package/dist/player/player-controller.d.ts.map +1 -1
- package/dist/player/player-controller.js +43 -55
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.d.ts","sourceRoot":"","sources":["../../src/helpers/objects.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { BehaviorSubject, Subject } from "rxjs";
|
|
2
|
+
import { PartialBy } from "../helpers/objects";
|
|
2
3
|
export interface PlayerControllerOptions {
|
|
3
4
|
autoSave?: boolean;
|
|
4
5
|
audioElement?: HTMLAudioElement;
|
|
5
6
|
loop?: boolean;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
fadeInterval?: number;
|
|
7
|
+
crossfade?: boolean;
|
|
8
|
+
crossfadeTime: number;
|
|
9
9
|
}
|
|
10
|
+
export type PlayerControllerParams = PartialBy<PlayerControllerOptions, "crossfadeTime">;
|
|
10
11
|
export declare enum PlayerStatus {
|
|
11
12
|
"playing" = "playing",
|
|
12
13
|
"paused" = "paused",
|
|
@@ -27,20 +28,13 @@ export declare class PlayerController {
|
|
|
27
28
|
private options;
|
|
28
29
|
private defaultOptions;
|
|
29
30
|
private destroyEvent;
|
|
30
|
-
private
|
|
31
|
-
|
|
32
|
-
constructor(trackId: string, trackUrl: string, options?: PlayerControllerOptions);
|
|
31
|
+
private volume;
|
|
32
|
+
constructor(trackId: string, trackUrl: string, options?: PlayerControllerParams);
|
|
33
33
|
open(id: string, file: string): Promise<void>;
|
|
34
|
-
destroy(): Promise<void>;
|
|
35
|
-
play(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
fadeIn(): Promise<void>;
|
|
39
|
-
fadeOut(): Promise<void>;
|
|
40
|
-
private clearFade;
|
|
41
|
-
pause(options?: {
|
|
42
|
-
fadeIn?: boolean;
|
|
43
|
-
}): Promise<void>;
|
|
34
|
+
destroy(now?: boolean): Promise<void>;
|
|
35
|
+
play(): Promise<void>;
|
|
36
|
+
pause(): Promise<void>;
|
|
37
|
+
setVolume(volume: number): void;
|
|
44
38
|
seekTo(seconds: number): void;
|
|
45
39
|
back(seconds?: number): void;
|
|
46
40
|
forward(seconds?: number): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"player-controller.d.ts","sourceRoot":"","sources":["../../src/player/player-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAsB,OAAO,
|
|
1
|
+
{"version":3,"file":"player-controller.d.ts","sourceRoot":"","sources":["../../src/player/player-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAsB,OAAO,EAAa,MAAM,MAAM,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAG/C,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,sBAAsB,GAAG,SAAS,CAAC,uBAAuB,EAAE,eAAe,CAAC,CAAC;AAEzF,oBAAY,YAAY;IACtB,SAAS,YAAY;IACrB,QAAQ,WAAW;IACnB,OAAO,UAAU;CAClB;AAED,qBAAa,gBAAgB;IAC3B,SAAgB,MAAM,gBAAuB;IAC7C,SAAgB,OAAO,gBAAuB;IAC9C,SAAgB,MAAM,gBAAuB;IAE7C,SAAgB,WAAW,0BAAkC;IAC7D,SAAgB,SAAS,0BAAkC;IAC3D,SAAgB,QAAQ,oCAEtB;IAEF,OAAO,CAAC,OAAO,CAAuB;IAEtC,SAAgB,MAAM,uCAAkD;IACxE,SAAgB,OAAO,qCAA2D;IAElF,OAAO,CAAC,aAAa,CAAmB;IAExC,OAAO,CAAC,YAAY,CAA0C;IAE9D,OAAO,CAAC,OAAO,CAA0B;IAEzC,OAAO,CAAC,cAAc,CAEpB;IAEF,OAAO,CAAC,YAAY,CAAuB;IAE3C,OAAO,CAAC,MAAM,CAAa;gBAEf,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,sBAA2B;IAyE7E,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAQ7B,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO;IAWrB,IAAI;IAOJ,KAAK;IAQX,SAAS,CAAC,MAAM,EAAE,MAAM;IAKxB,MAAM,CAAC,OAAO,EAAE,MAAM;IAOtB,IAAI,CAAC,OAAO,GAAE,MAAW;IAOzB,OAAO,CAAC,OAAO,GAAE,MAAW;YAQd,YAAY;IAK1B,OAAO,CAAC,GAAG;CAOZ"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BehaviorSubject, combineLatest, map, Subject,
|
|
1
|
+
import { BehaviorSubject, combineLatest, map, Subject, takeUntil } from "rxjs";
|
|
2
2
|
import { LocalStorage } from "../storage/local-storage";
|
|
3
3
|
export var PlayerStatus;
|
|
4
4
|
(function (PlayerStatus) {
|
|
@@ -20,11 +20,10 @@ export class PlayerController {
|
|
|
20
20
|
localStorage = new LocalStorage({ prefix: "player" });
|
|
21
21
|
options;
|
|
22
22
|
defaultOptions = {
|
|
23
|
-
|
|
23
|
+
crossfadeTime: 2000,
|
|
24
24
|
};
|
|
25
25
|
destroyEvent = new Subject();
|
|
26
|
-
|
|
27
|
-
fadePromiseResolve;
|
|
26
|
+
volume = 1;
|
|
28
27
|
constructor(trackId, trackUrl, options = {}) {
|
|
29
28
|
this.options = { ...this.defaultOptions, ...options };
|
|
30
29
|
this.playerElement = this.options.audioElement ?? new Audio();
|
|
@@ -36,10 +35,12 @@ export class PlayerController {
|
|
|
36
35
|
this.onPause.next();
|
|
37
36
|
this.status.next(PlayerStatus.paused);
|
|
38
37
|
});
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
if (!this.options.crossfade && !this.options.loop) {
|
|
39
|
+
this.playerElement.addEventListener("ended", () => {
|
|
40
|
+
this.onStop.next();
|
|
41
|
+
this.status.next(PlayerStatus.ended);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
43
44
|
this.playerElement.addEventListener("loadedmetadata", (event) => {
|
|
44
45
|
if (this.playerElement.duration) {
|
|
45
46
|
this.totalTime.next(this.playerElement.duration);
|
|
@@ -54,13 +55,24 @@ export class PlayerController {
|
|
|
54
55
|
if (this.options.autoSave) {
|
|
55
56
|
this.currentTime.pipe(takeUntil(this.destroyEvent)).subscribe((currentTime) => this.savePosition(currentTime));
|
|
56
57
|
}
|
|
57
|
-
if (this.options.
|
|
58
|
+
if (this.options.crossfade && !this.options.loop) {
|
|
58
59
|
combineLatest([this.currentTime, this.totalTime])
|
|
59
60
|
.pipe(takeUntil(this.destroyEvent))
|
|
60
|
-
.
|
|
61
|
-
.
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
.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
|
+
}
|
|
64
76
|
});
|
|
65
77
|
}
|
|
66
78
|
if (this.options.loop) {
|
|
@@ -79,58 +91,34 @@ export class PlayerController {
|
|
|
79
91
|
if (position && this.options.autoSave)
|
|
80
92
|
this.playerElement.currentTime = position;
|
|
81
93
|
}
|
|
82
|
-
async destroy() {
|
|
83
|
-
this.
|
|
84
|
-
|
|
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);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
this.destroyEvent.next();
|
|
101
|
+
await this.pause();
|
|
102
|
+
this.playerElement.remove();
|
|
103
|
+
}
|
|
86
104
|
}
|
|
87
|
-
async play(
|
|
105
|
+
async play() {
|
|
88
106
|
if (!this.playerElement.src)
|
|
89
107
|
throw new Error("No file opened");
|
|
90
108
|
this.log("Called play");
|
|
91
109
|
await this.playerElement?.play();
|
|
92
|
-
if (options.fadeIn !== undefined ? options.fadeIn : this.options.fadeIn)
|
|
93
|
-
await this.fadeIn();
|
|
94
110
|
}
|
|
95
|
-
async
|
|
96
|
-
this.clearFade();
|
|
97
|
-
return new Promise((resolve) => {
|
|
98
|
-
this.playerElement.volume = 0;
|
|
99
|
-
this.fadePromiseResolve = resolve;
|
|
100
|
-
this.fadeIntervalSubscription = setInterval(() => {
|
|
101
|
-
if (this.playerElement.volume >= 1) {
|
|
102
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
103
|
-
return resolve();
|
|
104
|
-
}
|
|
105
|
-
this.playerElement.volume = Math.min(this.playerElement.volume + (1 / this.options.fadeInterval) * 100, 1);
|
|
106
|
-
}, 100);
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
async fadeOut() {
|
|
110
|
-
this.clearFade();
|
|
111
|
-
return new Promise((resolve) => {
|
|
112
|
-
this.fadePromiseResolve = resolve;
|
|
113
|
-
this.fadeIntervalSubscription = setInterval(() => {
|
|
114
|
-
if (this.playerElement.volume <= 0) {
|
|
115
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
116
|
-
this.playerElement.pause();
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
this.playerElement.volume = Math.max(this.playerElement.volume - (1 / this.options.fadeInterval) * 100, 0);
|
|
120
|
-
}, 100);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
clearFade() {
|
|
124
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
125
|
-
this.fadePromiseResolve?.();
|
|
126
|
-
}
|
|
127
|
-
async pause(options = {}) {
|
|
111
|
+
async pause() {
|
|
128
112
|
if (!this.playerElement.src)
|
|
129
113
|
throw new Error("No file opened");
|
|
114
|
+
if (this.status.value === PlayerStatus.ended)
|
|
115
|
+
return;
|
|
130
116
|
this.log("Called pause");
|
|
131
117
|
this.playerElement?.pause();
|
|
132
|
-
|
|
133
|
-
|
|
118
|
+
}
|
|
119
|
+
setVolume(volume) {
|
|
120
|
+
this.volume = volume;
|
|
121
|
+
this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
|
|
134
122
|
}
|
|
135
123
|
seekTo(seconds) {
|
|
136
124
|
if (!this.playerElement.src)
|