@@ -12,6 +12,7 @@ import {
12
12
PartialState ,
13
13
Options ,
14
14
MediaStatus ,
15
+ PlaylistTrack ,
15
16
} from "./types" ;
16
17
import getStore from "./store" ;
17
18
import App from "./components/App" ;
@@ -230,14 +231,14 @@ class Webamp {
230
231
}
231
232
232
233
/**
233
- * Seek backward n seconds in the curent track
234
+ * Seek backward n seconds in the current track
234
235
*/
235
236
seekBackward ( seconds : number ) {
236
237
this . store . dispatch ( Actions . seekBackward ( seconds ) ) ;
237
238
}
238
239
239
240
/**
240
- * Seek forward n seconds in the curent track
241
+ * Seek forward n seconds in the current track
241
242
*/
242
243
seekForward ( seconds : number ) {
243
244
this . store . dispatch ( Actions . seekForward ( seconds ) ) ;
@@ -250,6 +251,20 @@ class Webamp {
250
251
this . store . dispatch ( Actions . seekToTime ( seconds ) ) ;
251
252
}
252
253
254
+ /**
255
+ * Check if shuffle is enabled
256
+ */
257
+ isShuffleEnabled ( ) : boolean {
258
+ return Selectors . getShuffle ( this . store . getState ( ) ) ;
259
+ }
260
+
261
+ /**
262
+ * Check if repeat is enabled
263
+ */
264
+ isRepeatEnabled ( ) : boolean {
265
+ return Selectors . getRepeat ( this . store . getState ( ) ) ;
266
+ }
267
+
253
268
/**
254
269
* Play the next track
255
270
*/
@@ -264,6 +279,13 @@ class Webamp {
264
279
this . store . dispatch ( Actions . previous ( ) ) ;
265
280
}
266
281
282
+ /**
283
+ * Play a specific track by index
284
+ */
285
+ playTrack ( index : number ) : void {
286
+ this . store . dispatch ( Actions . playTrack ( index ) ) ;
287
+ }
288
+
267
289
/**
268
290
* Add an array of `Track`s to the end of the playlist.
269
291
*/
@@ -281,6 +303,13 @@ class Webamp {
281
303
this . store . dispatch ( Actions . loadMediaFiles ( tracks , LOAD_STYLE . PLAY ) ) ;
282
304
}
283
305
306
+ /**
307
+ * Get the current playlist in order.
308
+ */
309
+ getPlaylistTracks ( ) : PlaylistTrack [ ] {
310
+ return Selectors . getPlaylistTracks ( this . store . getState ( ) ) ;
311
+ }
312
+
284
313
/**
285
314
* Get the current "playing" status.
286
315
*/
@@ -325,14 +354,50 @@ class Webamp {
325
354
this . store . dispatch ( Actions . open ( ) ) ;
326
355
}
327
356
357
+ /**
358
+ * A callback which will be called whenever a the current track changes.
359
+ *
360
+ * The callback is passed the current track and the zero-based index of the
361
+ * current track's position within the playlist.
362
+ *
363
+ * Note: This is different from the `onTrackDidChange` callback which is only
364
+ * called when a new track first starts loading.
365
+ *
366
+ * @returns An "unsubscribe" function. Useful if at some point in the future
367
+ * you want to stop listening to these events.
368
+ */
369
+ onCurrentTrackDidChange (
370
+ cb : ( currentTrack : PlaylistTrack , trackIndex : number ) => void
371
+ ) : ( ) => void {
372
+ let previousTrack : PlaylistTrack | null = null ;
373
+ return this . store . subscribe ( ( ) => {
374
+ const state = this . store . getState ( ) ;
375
+ const currentTrack = Selectors . getCurrentTrack ( state ) ;
376
+ if ( currentTrack == null || currentTrack === previousTrack ) {
377
+ return ;
378
+ }
379
+ previousTrack = currentTrack ;
380
+ const trackIndex = Selectors . getCurrentTrackIndex ( state ) ;
381
+ cb ( currentTrack , trackIndex ) ;
382
+ } ) ;
383
+ }
384
+
328
385
/**
329
386
* A callback which will be called when a new track starts loading.
330
387
*
331
- * This can happen on startup when the first track starts buffering, or when a subsequent track starts playing.
332
- * The callback will be called with an object `({url: 'https://example.com/track.mp3'})` containing the URL of the track.
388
+ * This can happen on startup when the first track starts buffering, or when a
389
+ * subsequent track starts playing. The callback will be called with an
390
+ * object `({url: 'https://example.com/track.mp3'})` containing the URL of the
391
+ * track.
392
+ *
333
393
* Note: If the user drags in a track, the URL may be an ObjectURL.
334
394
*
335
- * @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
395
+ * Note: This is different from the `onCurrentTrackDidChange` callback which
396
+ * is called every time a track changes. This callback is only called when a
397
+ * new track starts loading.
398
+ *
399
+ * @returns An "unsubscribe" function. Useful if at some point in the future
400
+ * you want to stop listening to these events.
336
401
*/
337
402
onTrackDidChange ( cb : ( trackInfo : LoadedURLTrack | null ) => void ) : ( ) => void {
338
403
let previousTrackId : number | null = null ;
0 commit comments