@audiowalk/sdk 1.5.1 → 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 -15
- package/dist/player/player-controller.d.ts.map +1 -1
- package/dist/player/player-controller.js +44 -57
- 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,19 +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
|
-
pause(options?: {
|
|
41
|
-
fadeIn?: boolean;
|
|
42
|
-
}): Promise<void>;
|
|
34
|
+
destroy(now?: boolean): Promise<void>;
|
|
35
|
+
play(): Promise<void>;
|
|
36
|
+
pause(): Promise<void>;
|
|
37
|
+
setVolume(volume: number): void;
|
|
43
38
|
seekTo(seconds: number): void;
|
|
44
39
|
back(seconds?: number): void;
|
|
45
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,12 +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
|
-
|
|
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
|
+
}
|
|
63
76
|
});
|
|
64
77
|
}
|
|
65
78
|
if (this.options.loop) {
|
|
@@ -78,60 +91,34 @@ export class PlayerController {
|
|
|
78
91
|
if (position && this.options.autoSave)
|
|
79
92
|
this.playerElement.currentTime = position;
|
|
80
93
|
}
|
|
81
|
-
async destroy() {
|
|
82
|
-
this.
|
|
83
|
-
|
|
84
|
-
|
|
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
|
+
}
|
|
85
104
|
}
|
|
86
|
-
async play(
|
|
105
|
+
async play() {
|
|
87
106
|
if (!this.playerElement.src)
|
|
88
107
|
throw new Error("No file opened");
|
|
89
108
|
this.log("Called play");
|
|
90
|
-
this.playerElement?.play();
|
|
91
|
-
if (options.fadeIn !== undefined ? options.fadeIn : this.options.fadeIn)
|
|
92
|
-
await this.fadeIn();
|
|
93
|
-
}
|
|
94
|
-
async fadeIn() {
|
|
95
|
-
if (!this.options.fadeInterval) {
|
|
96
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
97
|
-
this.fadePromiseResolve?.();
|
|
98
|
-
}
|
|
99
|
-
return new Promise((resolve) => {
|
|
100
|
-
this.playerElement.volume = 0;
|
|
101
|
-
this.fadePromiseResolve = resolve;
|
|
102
|
-
this.fadeIntervalSubscription = setInterval(() => {
|
|
103
|
-
if (this.playerElement.volume >= 1) {
|
|
104
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
105
|
-
return resolve();
|
|
106
|
-
}
|
|
107
|
-
this.playerElement.volume += (1 / this.options.fadeInterval) * 100;
|
|
108
|
-
}, 100);
|
|
109
|
-
});
|
|
109
|
+
await this.playerElement?.play();
|
|
110
110
|
}
|
|
111
|
-
async
|
|
112
|
-
if (!this.options.fadeInterval) {
|
|
113
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
114
|
-
this.fadePromiseResolve?.();
|
|
115
|
-
}
|
|
116
|
-
return new Promise((resolve) => {
|
|
117
|
-
this.fadePromiseResolve = resolve;
|
|
118
|
-
this.fadeIntervalSubscription = setInterval(() => {
|
|
119
|
-
if (this.playerElement.volume <= 0) {
|
|
120
|
-
clearInterval(this.fadeIntervalSubscription);
|
|
121
|
-
this.playerElement.pause();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
this.playerElement.volume -= (1 / this.options.fadeInterval) * 100;
|
|
125
|
-
}, 100);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
async pause(options = {}) {
|
|
111
|
+
async pause() {
|
|
129
112
|
if (!this.playerElement.src)
|
|
130
113
|
throw new Error("No file opened");
|
|
114
|
+
if (this.status.value === PlayerStatus.ended)
|
|
115
|
+
return;
|
|
131
116
|
this.log("Called pause");
|
|
132
117
|
this.playerElement?.pause();
|
|
133
|
-
|
|
134
|
-
|
|
118
|
+
}
|
|
119
|
+
setVolume(volume) {
|
|
120
|
+
this.volume = volume;
|
|
121
|
+
this.playerElement.volume = Math.max(Math.min(volume, 1), 0);
|
|
135
122
|
}
|
|
136
123
|
seekTo(seconds) {
|
|
137
124
|
if (!this.playerElement.src)
|