@audiowalk/sdk 1.0.1 → 1.0.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/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./player/player-controller"), exports);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BehaviorSubject, Subject } from "rxjs";
|
|
2
|
+
export interface PlayerControllerOptions {
|
|
3
|
+
playOnInit?: boolean;
|
|
4
|
+
autoSave?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare enum PlayerStatus {
|
|
7
|
+
"playing" = "playing",
|
|
8
|
+
"paused" = "paused",
|
|
9
|
+
"ended" = "ended"
|
|
10
|
+
}
|
|
11
|
+
export declare class PlayerController {
|
|
12
|
+
private readonly playerElement;
|
|
13
|
+
private options;
|
|
14
|
+
readonly onPlay: Subject<void>;
|
|
15
|
+
readonly onPause: Subject<void>;
|
|
16
|
+
readonly onStop: Subject<void>;
|
|
17
|
+
readonly currentTime: BehaviorSubject<number>;
|
|
18
|
+
readonly totalTime: BehaviorSubject<number>;
|
|
19
|
+
readonly progress: import("rxjs").Observable<number>;
|
|
20
|
+
readonly status: BehaviorSubject<PlayerStatus | null>;
|
|
21
|
+
readonly playing: import("rxjs").Observable<boolean>;
|
|
22
|
+
private file?;
|
|
23
|
+
constructor(playerElement: HTMLAudioElement, options?: PlayerControllerOptions);
|
|
24
|
+
open(file: string, metadata: MediaMetadataInit): void;
|
|
25
|
+
close(): void;
|
|
26
|
+
play(): void;
|
|
27
|
+
pause(): void;
|
|
28
|
+
seekTo(seconds: number): void;
|
|
29
|
+
back(): void;
|
|
30
|
+
forward(): void;
|
|
31
|
+
private savePosition;
|
|
32
|
+
private log;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=player-controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"player-controller.d.ts","sourceRoot":"","sources":["../../src/player/player-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAsB,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpE,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,oBAAY,YAAY;IACtB,SAAS,YAAY;IACrB,QAAQ,WAAW;IACnB,OAAO,UAAU;CAClB;AAED,qBAAa,gBAAgB;IAiBf,OAAO,CAAC,QAAQ,CAAC,aAAa;IAAoB,OAAO,CAAC,OAAO;IAhB7E,SAAgB,MAAM,gBAAuB;IAC7C,SAAgB,OAAO,gBAAuB;IAC9C,SAAgB,MAAM,gBAAuB;IAE7C,SAAgB,WAAW,0BAAkC;IAC7D,SAAgB,SAAS,0BAAkC;IAC3D,SAAgB,QAAQ,oCAEtB;IAEF,SAAgB,MAAM,uCAAkD;IAExE,SAAgB,OAAO,qCAA2D;IAElF,OAAO,CAAC,IAAI,CAAC,CAAS;gBAEO,aAAa,EAAE,gBAAgB,EAAU,OAAO,GAAE,uBAA4B;IAmE3G,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,iBAAiB;IAW9C,KAAK;IAaL,IAAI;IAOJ,KAAK;IAML,MAAM,CAAC,OAAO,EAAE,MAAM;IAOtB,IAAI;IAOJ,OAAO;IAQP,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,GAAG;CAOZ"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlayerController = exports.PlayerStatus = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
var PlayerStatus;
|
|
6
|
+
(function (PlayerStatus) {
|
|
7
|
+
PlayerStatus["playing"] = "playing";
|
|
8
|
+
PlayerStatus["paused"] = "paused";
|
|
9
|
+
PlayerStatus["ended"] = "ended";
|
|
10
|
+
})(PlayerStatus || (exports.PlayerStatus = PlayerStatus = {}));
|
|
11
|
+
class PlayerController {
|
|
12
|
+
constructor(playerElement, options = {}) {
|
|
13
|
+
this.playerElement = playerElement;
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.onPlay = new rxjs_1.Subject();
|
|
16
|
+
this.onPause = new rxjs_1.Subject();
|
|
17
|
+
this.onStop = new rxjs_1.Subject();
|
|
18
|
+
this.currentTime = new rxjs_1.BehaviorSubject(0);
|
|
19
|
+
this.totalTime = new rxjs_1.BehaviorSubject(0);
|
|
20
|
+
this.progress = (0, rxjs_1.combineLatest)([this.currentTime, this.totalTime]).pipe((0, rxjs_1.map)(([currentTime, totalTime]) => currentTime / totalTime));
|
|
21
|
+
this.status = new rxjs_1.BehaviorSubject(null);
|
|
22
|
+
this.playing = this.status.pipe((0, rxjs_1.map)((status) => status === "playing"));
|
|
23
|
+
navigator.mediaSession.setActionHandler("play", () => this.play());
|
|
24
|
+
navigator.mediaSession.setActionHandler("pause", () => this.pause());
|
|
25
|
+
navigator.mediaSession.setActionHandler("seekbackward", () => this.back());
|
|
26
|
+
navigator.mediaSession.setActionHandler("seekforward", () => this.forward());
|
|
27
|
+
navigator.mediaSession.setActionHandler("previoustrack", () => this.back());
|
|
28
|
+
navigator.mediaSession.setActionHandler("nexttrack", () => this.forward());
|
|
29
|
+
navigator.mediaSession.setActionHandler("seekto", (details) => {
|
|
30
|
+
// The fastSeek dictionary member will be true if the seek action is being called
|
|
31
|
+
// multiple times as part of a sequence and this is not the last call in that sequence.
|
|
32
|
+
if (details.fastSeek !== true && details.seekTime)
|
|
33
|
+
this.seekTo(details.seekTime);
|
|
34
|
+
});
|
|
35
|
+
this.status.subscribe((status) => {
|
|
36
|
+
switch (status) {
|
|
37
|
+
case "playing":
|
|
38
|
+
navigator.mediaSession.playbackState = "playing";
|
|
39
|
+
break;
|
|
40
|
+
case "paused":
|
|
41
|
+
navigator.mediaSession.playbackState = "paused";
|
|
42
|
+
break;
|
|
43
|
+
default:
|
|
44
|
+
case "ended":
|
|
45
|
+
navigator.mediaSession.playbackState = "none";
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
this.playerElement.addEventListener("play", () => {
|
|
50
|
+
this.onPlay.next();
|
|
51
|
+
this.status.next(PlayerStatus.playing);
|
|
52
|
+
});
|
|
53
|
+
this.playerElement.addEventListener("pause", () => {
|
|
54
|
+
this.onPause.next();
|
|
55
|
+
this.status.next(PlayerStatus.paused);
|
|
56
|
+
});
|
|
57
|
+
this.playerElement.addEventListener("ended", () => {
|
|
58
|
+
this.onStop.next();
|
|
59
|
+
this.status.next(PlayerStatus.ended);
|
|
60
|
+
});
|
|
61
|
+
this.playerElement.addEventListener("loadedmetadata", (event) => {
|
|
62
|
+
if (this.playerElement.duration) {
|
|
63
|
+
this.totalTime.next(this.playerElement.duration);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
this.playerElement.addEventListener("timeupdate", () => {
|
|
67
|
+
navigator.mediaSession.setPositionState({
|
|
68
|
+
duration: this.playerElement.duration,
|
|
69
|
+
playbackRate: this.playerElement.playbackRate,
|
|
70
|
+
position: this.playerElement.currentTime,
|
|
71
|
+
});
|
|
72
|
+
this.currentTime.next(this.playerElement.currentTime);
|
|
73
|
+
if (this.playerElement.duration) {
|
|
74
|
+
this.totalTime.next(this.playerElement.duration);
|
|
75
|
+
}
|
|
76
|
+
this.savePosition(this.playerElement.currentTime);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
open(file, metadata) {
|
|
80
|
+
this.file = file;
|
|
81
|
+
navigator.mediaSession.metadata = new MediaMetadata(metadata);
|
|
82
|
+
const position = localStorage.getItem(`progress-${this.file}`);
|
|
83
|
+
if (position && this.options.autoSave)
|
|
84
|
+
this.playerElement.currentTime = parseFloat(position);
|
|
85
|
+
if (this.options.playOnInit)
|
|
86
|
+
this.playerElement.play();
|
|
87
|
+
}
|
|
88
|
+
close() {
|
|
89
|
+
this.file = undefined;
|
|
90
|
+
this.playerElement.pause();
|
|
91
|
+
this.playerElement.src = "";
|
|
92
|
+
navigator.mediaSession.setActionHandler("play", null);
|
|
93
|
+
navigator.mediaSession.setActionHandler("pause", null);
|
|
94
|
+
navigator.mediaSession.setActionHandler("previoustrack", null);
|
|
95
|
+
navigator.mediaSession.setActionHandler("nexttrack", null);
|
|
96
|
+
navigator.mediaSession.playbackState = "none";
|
|
97
|
+
navigator.mediaSession.metadata = null;
|
|
98
|
+
}
|
|
99
|
+
play() {
|
|
100
|
+
var _a;
|
|
101
|
+
if (!this.file)
|
|
102
|
+
throw new Error("No file opened");
|
|
103
|
+
this.log("Called play");
|
|
104
|
+
(_a = this.playerElement) === null || _a === void 0 ? void 0 : _a.play();
|
|
105
|
+
}
|
|
106
|
+
pause() {
|
|
107
|
+
var _a;
|
|
108
|
+
if (!this.file)
|
|
109
|
+
throw new Error("No file opened");
|
|
110
|
+
this.log("Called pause");
|
|
111
|
+
(_a = this.playerElement) === null || _a === void 0 ? void 0 : _a.pause();
|
|
112
|
+
}
|
|
113
|
+
seekTo(seconds) {
|
|
114
|
+
if (!this.file)
|
|
115
|
+
throw new Error("No file opened");
|
|
116
|
+
this.log("Called seekTo");
|
|
117
|
+
this.playerElement.currentTime = seconds;
|
|
118
|
+
}
|
|
119
|
+
back() {
|
|
120
|
+
if (!this.file)
|
|
121
|
+
throw new Error("No file opened");
|
|
122
|
+
const position = this.playerElement.currentTime;
|
|
123
|
+
this.seekTo(Math.max(position - 10, 0));
|
|
124
|
+
}
|
|
125
|
+
forward() {
|
|
126
|
+
if (!this.file)
|
|
127
|
+
throw new Error("No file opened");
|
|
128
|
+
const position = this.playerElement.currentTime;
|
|
129
|
+
const duration = this.playerElement.duration;
|
|
130
|
+
this.seekTo(duration && duration > 0 ? Math.min(position + 10, duration) : position + 10);
|
|
131
|
+
}
|
|
132
|
+
savePosition(currentTime) {
|
|
133
|
+
if (!this.file)
|
|
134
|
+
return;
|
|
135
|
+
localStorage.setItem(`progress-${this.file}`, String(currentTime));
|
|
136
|
+
}
|
|
137
|
+
log(message) {
|
|
138
|
+
var _a, _b;
|
|
139
|
+
const time = ((_a = this.playerElement) === null || _a === void 0 ? void 0 : _a.currentTime) ? Math.round((_b = this.playerElement) === null || _b === void 0 ? void 0 : _b.currentTime) : null;
|
|
140
|
+
if (time)
|
|
141
|
+
message += ` @${time}s`;
|
|
142
|
+
console.log(`[PlayerController] ${message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.PlayerController = PlayerController;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audiowalk/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/audiowalk-cz/components.git"
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"dev": "tsc -w",
|
|
11
11
|
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
12
13
|
"release:patch": "npm version patch && git push && npm publish --access public"
|
|
13
14
|
},
|
|
14
15
|
"author": "",
|