tunzo-player 1.0.6 → 1.0.8
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/README.md +1 -0
- package/dist/core/player.d.ts +16 -12
- package/dist/core/player.js +165 -71
- package/package.json +8 -1
- package/src/core/player.ts +203 -141
- package/tsconfig.json +2 -1
package/README.md
CHANGED
package/dist/core/player.d.ts
CHANGED
|
@@ -9,27 +9,31 @@ export declare class Player {
|
|
|
9
9
|
private static queue;
|
|
10
10
|
private static playlist;
|
|
11
11
|
private static selectedQuality;
|
|
12
|
-
|
|
13
|
-
static initialize(playlist
|
|
14
|
-
static play(song: any, index?: number): void
|
|
15
|
-
static
|
|
16
|
-
static
|
|
17
|
-
static
|
|
18
|
-
static
|
|
19
|
-
static
|
|
20
|
-
static
|
|
21
|
-
static
|
|
22
|
-
static
|
|
12
|
+
private static isInitialized;
|
|
13
|
+
static initialize(playlist?: any[], quality?: number): void;
|
|
14
|
+
static play(song: any, index?: number): Promise<void>;
|
|
15
|
+
private static setupMusicControls;
|
|
16
|
+
static pause(): Promise<void>;
|
|
17
|
+
static resume(): Promise<void>;
|
|
18
|
+
static stop(): Promise<void>;
|
|
19
|
+
static togglePlayPause(): Promise<void>;
|
|
20
|
+
static next(): Promise<void>;
|
|
21
|
+
static prev(): Promise<void>;
|
|
22
|
+
static autoNext(): Promise<void>;
|
|
23
|
+
static playRandom(): Promise<void>;
|
|
23
24
|
static toggleShuffle(): void;
|
|
24
25
|
static addToQueue(song: any): void;
|
|
25
26
|
static removeFromQueue(index: number): void;
|
|
26
27
|
static reorderQueue(from: number, to: number): void;
|
|
28
|
+
static seekTo(seconds: number): Promise<void>;
|
|
27
29
|
static getCurrentTime(): number;
|
|
28
30
|
static getDuration(): number;
|
|
29
|
-
static formatTime(
|
|
31
|
+
static formatTime(t: number): string;
|
|
30
32
|
static isPlayingSong(): boolean;
|
|
31
33
|
static getCurrentSong(): any;
|
|
32
34
|
static setQuality(index: number): void;
|
|
33
35
|
static getQueue(): any[];
|
|
34
36
|
static getPlaylist(): any[];
|
|
37
|
+
static getShuffleStatus(): boolean;
|
|
38
|
+
static destroy(): Promise<void>;
|
|
35
39
|
}
|
package/dist/core/player.js
CHANGED
|
@@ -1,108 +1,189 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Player = void 0;
|
|
13
|
+
const core_1 = require("@capacitor/core");
|
|
14
|
+
const capacitor_music_controls_plugin_1 = require("capacitor-music-controls-plugin");
|
|
4
15
|
class Player {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
16
|
+
static initialize(playlist = [], quality = 3) {
|
|
17
|
+
if (!core_1.Capacitor.isNativePlatform()) {
|
|
18
|
+
this.audio = new Audio();
|
|
19
|
+
}
|
|
20
|
+
this.playlist = [...playlist];
|
|
8
21
|
this.selectedQuality = quality;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
|
|
22
|
+
this.isInitialized = true;
|
|
23
|
+
}
|
|
24
|
+
static play(song_1) {
|
|
25
|
+
return __awaiter(this, arguments, void 0, function* (song, index = 0) {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
if (!this.isInitialized)
|
|
28
|
+
return;
|
|
29
|
+
if (!(song === null || song === void 0 ? void 0 : song.downloadUrl))
|
|
30
|
+
return;
|
|
31
|
+
yield this.stop();
|
|
32
|
+
this.currentSong = song;
|
|
33
|
+
this.currentIndex = index;
|
|
34
|
+
const url = ((_a = song.downloadUrl[this.selectedQuality]) === null || _a === void 0 ? void 0 : _a.url) || ((_b = song.downloadUrl[0]) === null || _b === void 0 ? void 0 : _b.url);
|
|
35
|
+
if (!url)
|
|
36
|
+
return;
|
|
37
|
+
if (!this.audio)
|
|
38
|
+
this.audio = new Audio();
|
|
39
|
+
this.audio.src = url;
|
|
40
|
+
this.audio.play().catch(console.error);
|
|
41
|
+
this.audio.onloadedmetadata = () => this.duration = this.audio.duration;
|
|
42
|
+
this.audio.ontimeupdate = () => this.currentTime = this.audio.currentTime;
|
|
43
|
+
this.audio.onended = () => this.autoNext();
|
|
44
|
+
yield this.setupMusicControls(song, true);
|
|
45
|
+
this.isPlaying = true;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
static setupMusicControls(song, isPlaying) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
if (!core_1.Capacitor.isNativePlatform())
|
|
51
|
+
return;
|
|
52
|
+
yield capacitor_music_controls_plugin_1.CapacitorMusicControls.destroy(); // clear existing
|
|
53
|
+
yield capacitor_music_controls_plugin_1.CapacitorMusicControls.create({
|
|
54
|
+
track: song.name || '',
|
|
55
|
+
artist: song.primaryArtists || '',
|
|
56
|
+
album: song.album || '',
|
|
57
|
+
cover: song.image || '',
|
|
58
|
+
isPlaying,
|
|
59
|
+
dismissable: true,
|
|
60
|
+
hasPrev: true,
|
|
61
|
+
hasNext: true,
|
|
62
|
+
});
|
|
63
|
+
capacitor_music_controls_plugin_1.CapacitorMusicControls.addListener('music-controls-next', () => this.next());
|
|
64
|
+
capacitor_music_controls_plugin_1.CapacitorMusicControls.addListener('music-controls-previous', () => this.prev());
|
|
65
|
+
capacitor_music_controls_plugin_1.CapacitorMusicControls.addListener('music-controls-pause', () => this.pause());
|
|
66
|
+
capacitor_music_controls_plugin_1.CapacitorMusicControls.addListener('music-controls-play', () => this.resume());
|
|
67
|
+
capacitor_music_controls_plugin_1.CapacitorMusicControls.addListener('music-controls-destroy', () => this.stop());
|
|
68
|
+
});
|
|
31
69
|
}
|
|
32
70
|
static pause() {
|
|
33
|
-
this
|
|
34
|
-
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
if (this.audio)
|
|
73
|
+
this.audio.pause();
|
|
74
|
+
this.isPlaying = false;
|
|
75
|
+
core_1.Capacitor.isNativePlatform() && capacitor_music_controls_plugin_1.CapacitorMusicControls.updateIsPlaying({ isPlaying: false });
|
|
76
|
+
});
|
|
35
77
|
}
|
|
36
78
|
static resume() {
|
|
37
|
-
this
|
|
38
|
-
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
if (this.audio)
|
|
81
|
+
yield this.audio.play();
|
|
82
|
+
this.isPlaying = true;
|
|
83
|
+
core_1.Capacitor.isNativePlatform() && capacitor_music_controls_plugin_1.CapacitorMusicControls.updateIsPlaying({ isPlaying: true });
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
static stop() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
if (this.audio) {
|
|
89
|
+
this.audio.pause();
|
|
90
|
+
this.audio.src = '';
|
|
91
|
+
}
|
|
92
|
+
this.isPlaying = false;
|
|
93
|
+
yield capacitor_music_controls_plugin_1.CapacitorMusicControls.destroy();
|
|
94
|
+
});
|
|
39
95
|
}
|
|
40
96
|
static togglePlayPause() {
|
|
41
|
-
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.
|
|
46
|
-
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
if (this.isPlaying) {
|
|
99
|
+
yield this.pause();
|
|
100
|
+
}
|
|
101
|
+
else if (this.currentSong) {
|
|
102
|
+
yield this.resume();
|
|
103
|
+
}
|
|
104
|
+
else if (this.playlist.length > 0) {
|
|
105
|
+
yield this.play(this.playlist[0], 0);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
47
108
|
}
|
|
48
109
|
static next() {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
if (this.queue.length > 0) {
|
|
112
|
+
const next = this.queue.shift();
|
|
113
|
+
const index = this.playlist.findIndex(s => s.id === next.id);
|
|
114
|
+
yield this.play(next, index);
|
|
115
|
+
}
|
|
116
|
+
else if (this.isShuffle) {
|
|
117
|
+
yield this.playRandom();
|
|
118
|
+
}
|
|
119
|
+
else if (this.currentIndex < this.playlist.length - 1) {
|
|
120
|
+
yield this.play(this.playlist[this.currentIndex + 1], this.currentIndex + 1);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
60
123
|
}
|
|
61
124
|
static prev() {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
if (this.currentTime > 3) {
|
|
127
|
+
yield this.seekTo(0);
|
|
128
|
+
}
|
|
129
|
+
else if (this.currentIndex > 0) {
|
|
130
|
+
yield this.play(this.playlist[this.currentIndex - 1], this.currentIndex - 1);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
68
133
|
}
|
|
69
134
|
static autoNext() {
|
|
70
|
-
this
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
yield this.next();
|
|
137
|
+
});
|
|
71
138
|
}
|
|
72
139
|
static playRandom() {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
if (this.playlist.length <= 1)
|
|
142
|
+
return;
|
|
143
|
+
let rand = this.currentIndex;
|
|
144
|
+
while (rand === this.currentIndex) {
|
|
145
|
+
rand = Math.floor(Math.random() * this.playlist.length);
|
|
146
|
+
}
|
|
147
|
+
yield this.play(this.playlist[rand], rand);
|
|
148
|
+
});
|
|
80
149
|
}
|
|
81
150
|
static toggleShuffle() {
|
|
82
151
|
this.isShuffle = !this.isShuffle;
|
|
83
152
|
}
|
|
84
153
|
static addToQueue(song) {
|
|
85
|
-
if (!this.queue.
|
|
154
|
+
if (!this.queue.find(s => s.id === song.id)) {
|
|
86
155
|
this.queue.push(song);
|
|
87
156
|
}
|
|
88
157
|
}
|
|
89
158
|
static removeFromQueue(index) {
|
|
90
|
-
this.queue.
|
|
159
|
+
if (index >= 0 && index < this.queue.length) {
|
|
160
|
+
this.queue.splice(index, 1);
|
|
161
|
+
}
|
|
91
162
|
}
|
|
92
163
|
static reorderQueue(from, to) {
|
|
93
|
-
|
|
164
|
+
if (from === to)
|
|
165
|
+
return;
|
|
166
|
+
const [item] = this.queue.splice(from, 1);
|
|
94
167
|
this.queue.splice(to, 0, item);
|
|
95
168
|
}
|
|
169
|
+
static seekTo(seconds) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
if (this.audio) {
|
|
172
|
+
this.audio.currentTime = seconds;
|
|
173
|
+
this.currentTime = seconds;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
96
177
|
static getCurrentTime() {
|
|
97
178
|
return this.currentTime;
|
|
98
179
|
}
|
|
99
180
|
static getDuration() {
|
|
100
181
|
return this.duration;
|
|
101
182
|
}
|
|
102
|
-
static formatTime(
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
return `${
|
|
183
|
+
static formatTime(t) {
|
|
184
|
+
const m = Math.floor(t / 60);
|
|
185
|
+
const s = Math.floor(t % 60);
|
|
186
|
+
return `${m}:${s < 10 ? '0' : ''}${s}`;
|
|
106
187
|
}
|
|
107
188
|
static isPlayingSong() {
|
|
108
189
|
return this.isPlaying;
|
|
@@ -114,20 +195,33 @@ class Player {
|
|
|
114
195
|
this.selectedQuality = index;
|
|
115
196
|
}
|
|
116
197
|
static getQueue() {
|
|
117
|
-
return this.queue;
|
|
198
|
+
return [...this.queue];
|
|
118
199
|
}
|
|
119
200
|
static getPlaylist() {
|
|
120
|
-
return this.playlist;
|
|
201
|
+
return [...this.playlist];
|
|
202
|
+
}
|
|
203
|
+
static getShuffleStatus() {
|
|
204
|
+
return this.isShuffle;
|
|
205
|
+
}
|
|
206
|
+
static destroy() {
|
|
207
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
+
yield this.stop();
|
|
209
|
+
this.playlist = [];
|
|
210
|
+
this.queue = [];
|
|
211
|
+
this.audio = null;
|
|
212
|
+
this.isInitialized = false;
|
|
213
|
+
});
|
|
121
214
|
}
|
|
122
215
|
}
|
|
123
216
|
exports.Player = Player;
|
|
124
|
-
Player.audio =
|
|
217
|
+
Player.audio = null;
|
|
125
218
|
Player.currentSong = null;
|
|
126
219
|
Player.currentIndex = 0;
|
|
127
220
|
Player.isPlaying = false;
|
|
128
221
|
Player.currentTime = 0;
|
|
129
222
|
Player.duration = 0;
|
|
130
|
-
Player.isShuffle =
|
|
223
|
+
Player.isShuffle = false;
|
|
131
224
|
Player.queue = [];
|
|
132
225
|
Player.playlist = [];
|
|
133
226
|
Player.selectedQuality = 3;
|
|
227
|
+
Player.isInitialized = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tunzo-player",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "A music playback service for Angular and Ionic apps with native audio control support.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,5 +18,12 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.8.3"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@capacitor-community/native-audio": "^7.0.0",
|
|
27
|
+
"capacitor-music-controls-plugin": "^6.1.0"
|
|
21
28
|
}
|
|
22
29
|
}
|
package/src/core/player.ts
CHANGED
|
@@ -1,152 +1,214 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core';
|
|
2
|
+
import { CapacitorMusicControls } from 'capacitor-music-controls-plugin';
|
|
3
|
+
|
|
1
4
|
export class Player {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
5
|
+
private static audio: HTMLAudioElement | null = null;
|
|
6
|
+
private static currentSong: any = null;
|
|
7
|
+
private static currentIndex = 0;
|
|
8
|
+
private static isPlaying = false;
|
|
9
|
+
private static currentTime = 0;
|
|
10
|
+
private static duration = 0;
|
|
11
|
+
private static isShuffle = false;
|
|
12
|
+
private static queue: any[] = [];
|
|
13
|
+
private static playlist: any[] = [];
|
|
14
|
+
private static selectedQuality = 3;
|
|
15
|
+
private static isInitialized = false;
|
|
16
|
+
|
|
17
|
+
static initialize(playlist: any[] = [], quality = 3) {
|
|
18
|
+
if (!Capacitor.isNativePlatform()) {
|
|
19
|
+
this.audio = new Audio();
|
|
20
|
+
}
|
|
21
|
+
this.playlist = [...playlist];
|
|
22
|
+
this.selectedQuality = quality;
|
|
23
|
+
this.isInitialized = true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static async play(song: any, index = 0) {
|
|
27
|
+
if (!this.isInitialized) return;
|
|
28
|
+
if (!song?.downloadUrl) return;
|
|
29
|
+
|
|
30
|
+
await this.stop();
|
|
31
|
+
|
|
32
|
+
this.currentSong = song;
|
|
33
|
+
this.currentIndex = index;
|
|
34
|
+
const url = song.downloadUrl[this.selectedQuality]?.url || song.downloadUrl[0]?.url;
|
|
35
|
+
|
|
36
|
+
if (!url) return;
|
|
37
|
+
|
|
38
|
+
if (!this.audio) this.audio = new Audio();
|
|
39
|
+
|
|
40
|
+
this.audio.src = url;
|
|
41
|
+
this.audio.play().catch(console.error);
|
|
42
|
+
|
|
43
|
+
this.audio.onloadedmetadata = () => this.duration = this.audio!.duration;
|
|
44
|
+
this.audio.ontimeupdate = () => this.currentTime = this.audio!.currentTime;
|
|
45
|
+
this.audio.onended = () => this.autoNext();
|
|
46
|
+
|
|
47
|
+
await this.setupMusicControls(song, true);
|
|
48
|
+
|
|
49
|
+
this.isPlaying = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private static async setupMusicControls(song: any, isPlaying: boolean) {
|
|
53
|
+
if (!Capacitor.isNativePlatform()) return;
|
|
54
|
+
|
|
55
|
+
await CapacitorMusicControls.destroy(); // clear existing
|
|
56
|
+
await CapacitorMusicControls.create({
|
|
57
|
+
track: song.name || '',
|
|
58
|
+
artist: song.primaryArtists || '',
|
|
59
|
+
album: song.album || '',
|
|
60
|
+
cover: song.image || '',
|
|
61
|
+
isPlaying,
|
|
62
|
+
dismissable: true,
|
|
63
|
+
hasPrev: true,
|
|
64
|
+
hasNext: true,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
CapacitorMusicControls.addListener('music-controls-next', () => this.next());
|
|
68
|
+
CapacitorMusicControls.addListener('music-controls-previous', () => this.prev());
|
|
69
|
+
CapacitorMusicControls.addListener('music-controls-pause', () => this.pause());
|
|
70
|
+
CapacitorMusicControls.addListener('music-controls-play', () => this.resume());
|
|
71
|
+
CapacitorMusicControls.addListener('music-controls-destroy', () => this.stop());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static async pause() {
|
|
75
|
+
if (this.audio) this.audio.pause();
|
|
76
|
+
this.isPlaying = false;
|
|
77
|
+
Capacitor.isNativePlatform() && CapacitorMusicControls.updateIsPlaying({ isPlaying: false });
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static async resume() {
|
|
82
|
+
if (this.audio) await this.audio.play();
|
|
83
|
+
this.isPlaying = true;
|
|
84
|
+
Capacitor.isNativePlatform() && CapacitorMusicControls.updateIsPlaying({ isPlaying: true });
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static async stop() {
|
|
89
|
+
if (this.audio) {
|
|
45
90
|
this.audio.pause();
|
|
46
|
-
this.
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
static resume() {
|
|
50
|
-
this.audio.play();
|
|
51
|
-
this.isPlaying = true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
static togglePlayPause() {
|
|
55
|
-
if (this.isPlaying) {
|
|
56
|
-
this.pause();
|
|
57
|
-
} else {
|
|
58
|
-
this.resume();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
static next() {
|
|
63
|
-
if (this.queue.length > 0) {
|
|
64
|
-
const nextQueued = this.queue.shift();
|
|
65
|
-
const index = this.playlist.findIndex(s => s.id === nextQueued.id);
|
|
66
|
-
this.play(nextQueued, index);
|
|
67
|
-
} else if (this.isShuffle) {
|
|
68
|
-
this.playRandom();
|
|
69
|
-
} else if (this.currentIndex < this.playlist.length - 1) {
|
|
70
|
-
this.play(this.playlist[this.currentIndex + 1], this.currentIndex + 1);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
static prev() {
|
|
75
|
-
if (this.currentIndex > 0) {
|
|
76
|
-
this.play(this.playlist[this.currentIndex - 1], this.currentIndex - 1);
|
|
77
|
-
}
|
|
91
|
+
this.audio.src = '';
|
|
78
92
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
this.
|
|
93
|
+
this.isPlaying = false;
|
|
94
|
+
await CapacitorMusicControls.destroy();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static async togglePlayPause() {
|
|
98
|
+
if (this.isPlaying) {
|
|
99
|
+
await this.pause();
|
|
100
|
+
} else if (this.currentSong) {
|
|
101
|
+
await this.resume();
|
|
102
|
+
} else if (this.playlist.length > 0) {
|
|
103
|
+
await this.play(this.playlist[0], 0);
|
|
86
104
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static async next() {
|
|
108
|
+
if (this.queue.length > 0) {
|
|
109
|
+
const next = this.queue.shift();
|
|
110
|
+
const index = this.playlist.findIndex(s => s.id === next.id);
|
|
111
|
+
await this.play(next, index);
|
|
112
|
+
} else if (this.isShuffle) {
|
|
113
|
+
await this.playRandom();
|
|
114
|
+
} else if (this.currentIndex < this.playlist.length - 1) {
|
|
115
|
+
await this.play(this.playlist[this.currentIndex + 1], this.currentIndex + 1);
|
|
97
116
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static async prev() {
|
|
120
|
+
if (this.currentTime > 3) {
|
|
121
|
+
await this.seekTo(0);
|
|
122
|
+
} else if (this.currentIndex > 0) {
|
|
123
|
+
await this.play(this.playlist[this.currentIndex - 1], this.currentIndex - 1);
|
|
101
124
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static async autoNext() {
|
|
128
|
+
await this.next();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static async playRandom() {
|
|
132
|
+
if (this.playlist.length <= 1) return;
|
|
133
|
+
let rand = this.currentIndex;
|
|
134
|
+
while (rand === this.currentIndex) {
|
|
135
|
+
rand = Math.floor(Math.random() * this.playlist.length);
|
|
136
|
+
}
|
|
137
|
+
await this.play(this.playlist[rand], rand);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static toggleShuffle() {
|
|
141
|
+
this.isShuffle = !this.isShuffle;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static addToQueue(song: any) {
|
|
145
|
+
if (!this.queue.find(s => s.id === song.id)) {
|
|
146
|
+
this.queue.push(song);
|
|
107
147
|
}
|
|
108
|
-
|
|
109
|
-
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static removeFromQueue(index: number) {
|
|
151
|
+
if (index >= 0 && index < this.queue.length) {
|
|
110
152
|
this.queue.splice(index, 1);
|
|
111
153
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
static formatTime(time: number): string {
|
|
127
|
-
const minutes = Math.floor(time / 60);
|
|
128
|
-
const seconds = Math.floor(time % 60);
|
|
129
|
-
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
static isPlayingSong(): boolean {
|
|
133
|
-
return this.isPlaying;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
static getCurrentSong(): any {
|
|
137
|
-
return this.currentSong;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
static setQuality(index: number) {
|
|
141
|
-
this.selectedQuality = index;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
static getQueue(): any[] {
|
|
145
|
-
return this.queue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static getPlaylist(): any[] {
|
|
149
|
-
return this.playlist;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static reorderQueue(from: number, to: number) {
|
|
157
|
+
if (from === to) return;
|
|
158
|
+
const [item] = this.queue.splice(from, 1);
|
|
159
|
+
this.queue.splice(to, 0, item);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static async seekTo(seconds: number) {
|
|
163
|
+
if (this.audio) {
|
|
164
|
+
this.audio.currentTime = seconds;
|
|
165
|
+
this.currentTime = seconds;
|
|
150
166
|
}
|
|
151
167
|
}
|
|
152
|
-
|
|
168
|
+
|
|
169
|
+
static getCurrentTime(): number {
|
|
170
|
+
return this.currentTime;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static getDuration(): number {
|
|
174
|
+
return this.duration;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static formatTime(t: number): string {
|
|
178
|
+
const m = Math.floor(t / 60);
|
|
179
|
+
const s = Math.floor(t % 60);
|
|
180
|
+
return `${m}:${s < 10 ? '0' : ''}${s}`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static isPlayingSong(): boolean {
|
|
184
|
+
return this.isPlaying;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static getCurrentSong(): any {
|
|
188
|
+
return this.currentSong;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static setQuality(index: number) {
|
|
192
|
+
this.selectedQuality = index;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static getQueue(): any[] {
|
|
196
|
+
return [...this.queue];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static getPlaylist(): any[] {
|
|
200
|
+
return [...this.playlist];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static getShuffleStatus(): boolean {
|
|
204
|
+
return this.isShuffle;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static async destroy() {
|
|
208
|
+
await this.stop();
|
|
209
|
+
this.playlist = [];
|
|
210
|
+
this.queue = [];
|
|
211
|
+
this.audio = null;
|
|
212
|
+
this.isInitialized = false;
|
|
213
|
+
}
|
|
214
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"esModuleInterop": true, // allow default imports from CommonJS
|
|
15
15
|
"forceConsistentCasingInFileNames": true,
|
|
16
16
|
"strict": true,
|
|
17
|
-
"skipLibCheck": true
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"types": ["@capacitor-community/native-audio"] // speed up builds, safe for libs
|
|
18
19
|
},
|
|
19
20
|
"include": ["src"] // only compile the src folder
|
|
20
21
|
}
|