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 CHANGED
@@ -76,3 +76,4 @@ yarn add tunzo-player
76
76
  | 3 | High (160kbps) |
77
77
  | 4 | Ultra (320kbps) |
78
78
 
79
+ # tunzo-player
@@ -9,27 +9,31 @@ export declare class Player {
9
9
  private static queue;
10
10
  private static playlist;
11
11
  private static selectedQuality;
12
- /** Initialize with playlist and quality */
13
- static initialize(playlist: any[], quality?: number): void;
14
- static play(song: any, index?: number): void;
15
- static pause(): void;
16
- static resume(): void;
17
- static togglePlayPause(): void;
18
- static next(): void;
19
- static prev(): void;
20
- static seek(seconds: number): void;
21
- static autoNext(): void;
22
- static playRandom(): void;
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(time: number): string;
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
  }
@@ -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
- /** Initialize with playlist and quality */
6
- static initialize(playlist, quality = 3) {
7
- this.playlist = playlist;
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
- static play(song, index = 0) {
11
- var _a;
12
- if (!song || !song.downloadUrl)
13
- return;
14
- this.currentSong = song;
15
- this.currentIndex = index;
16
- this.audio.src = ((_a = song.downloadUrl[this.selectedQuality]) === null || _a === void 0 ? void 0 : _a.url) || '';
17
- this.audio.play();
18
- this.isPlaying = true;
19
- // Set duration
20
- this.audio.onloadedmetadata = () => {
21
- this.duration = this.audio.duration;
22
- };
23
- // Set current time
24
- this.audio.ontimeupdate = () => {
25
- this.currentTime = this.audio.currentTime;
26
- };
27
- // Auto-play next song
28
- this.audio.onended = () => {
29
- this.autoNext();
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.audio.pause();
34
- this.isPlaying = false;
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.audio.play();
38
- this.isPlaying = true;
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
- if (this.isPlaying) {
42
- this.pause();
43
- }
44
- else {
45
- this.resume();
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
- if (this.queue.length > 0) {
50
- const nextQueued = this.queue.shift();
51
- const index = this.playlist.findIndex(s => s.id === nextQueued.id);
52
- this.play(nextQueued, index);
53
- }
54
- else if (this.isShuffle) {
55
- this.playRandom();
56
- }
57
- else if (this.currentIndex < this.playlist.length - 1) {
58
- this.play(this.playlist[this.currentIndex + 1], this.currentIndex + 1);
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
- if (this.currentIndex > 0) {
63
- this.play(this.playlist[this.currentIndex - 1], this.currentIndex - 1);
64
- }
65
- }
66
- static seek(seconds) {
67
- this.audio.currentTime = seconds;
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.next();
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ yield this.next();
137
+ });
71
138
  }
72
139
  static playRandom() {
73
- if (this.playlist.length <= 1)
74
- return;
75
- let randomIndex;
76
- do {
77
- randomIndex = Math.floor(Math.random() * this.playlist.length);
78
- } while (randomIndex === this.currentIndex);
79
- this.play(this.playlist[randomIndex], randomIndex);
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.some(q => q.id === song.id)) {
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.splice(index, 1);
159
+ if (index >= 0 && index < this.queue.length) {
160
+ this.queue.splice(index, 1);
161
+ }
91
162
  }
92
163
  static reorderQueue(from, to) {
93
- const item = this.queue.splice(from, 1)[0];
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(time) {
103
- const minutes = Math.floor(time / 60);
104
- const seconds = Math.floor(time % 60);
105
- return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
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 = new 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 = true;
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.6",
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
  }
@@ -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
- private static audio = new Audio();
3
- private static currentSong: any = null;
4
- private static currentIndex = 0;
5
- private static isPlaying = false;
6
- private static currentTime = 0;
7
- private static duration = 0;
8
- private static isShuffle = true;
9
- private static queue: any[] = [];
10
- private static playlist: any[] = [];
11
- private static selectedQuality = 3;
12
-
13
- /** Initialize with playlist and quality */
14
- static initialize(playlist: any[], quality = 3) {
15
- this.playlist = playlist;
16
- this.selectedQuality = quality;
17
- }
18
-
19
- static play(song: any, index: number = 0) {
20
- if (!song || !song.downloadUrl) return;
21
-
22
- this.currentSong = song;
23
- this.currentIndex = index;
24
- this.audio.src = song.downloadUrl[this.selectedQuality]?.url || '';
25
- this.audio.play();
26
- this.isPlaying = true;
27
-
28
- // Set duration
29
- this.audio.onloadedmetadata = () => {
30
- this.duration = this.audio.duration;
31
- };
32
-
33
- // Set current time
34
- this.audio.ontimeupdate = () => {
35
- this.currentTime = this.audio.currentTime;
36
- };
37
-
38
- // Auto-play next song
39
- this.audio.onended = () => {
40
- this.autoNext();
41
- };
42
- }
43
-
44
- static pause() {
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.isPlaying = false;
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
- static seek(seconds: number) {
81
- this.audio.currentTime = seconds;
82
- }
83
-
84
- static autoNext() {
85
- this.next();
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
- static playRandom() {
89
- if (this.playlist.length <= 1) return;
90
-
91
- let randomIndex;
92
- do {
93
- randomIndex = Math.floor(Math.random() * this.playlist.length);
94
- } while (randomIndex === this.currentIndex);
95
-
96
- this.play(this.playlist[randomIndex], randomIndex);
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
- static toggleShuffle() {
100
- this.isShuffle = !this.isShuffle;
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
- static addToQueue(song: any) {
104
- if (!this.queue.some(q => q.id === song.id)) {
105
- this.queue.push(song);
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
- static removeFromQueue(index: number) {
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
- static reorderQueue(from: number, to: number) {
114
- const item = this.queue.splice(from, 1)[0];
115
- this.queue.splice(to, 0, item);
116
- }
117
-
118
- static getCurrentTime(): number {
119
- return this.currentTime;
120
- }
121
-
122
- static getDuration(): number {
123
- return this.duration;
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 // speed up builds, safe for libs
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
  }