@@ -4,7 +4,7 @@ import { ConvertTimeToText } from './helper'
4
4
interface Props {
5
5
tracks : string [ ]
6
6
startIndex ?: number
7
- children ?: ( ( props : PlayerType ) => ReactNode ) | ReactNode ;
7
+ children ?: ( ( props : PlayerType ) => ReactNode ) | ReactNode
8
8
}
9
9
// prop types should be like this interface
10
10
@@ -28,6 +28,8 @@ interface Props {
28
28
* @property {function } player.onScrubEnd
29
29
* @property {boolean } player.isPlaying
30
30
* @property {function } player.setIsPlaying
31
+ * @property {function } player.play
32
+ * @property {function } player.pause
31
33
* @property {function } player.toNextTrack
32
34
* @property {function } player.toPrevTrack
33
35
* @property {boolean } player.isRepeat
@@ -181,7 +183,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
181
183
* on scrub get the slider or range tag value and replace it with playing song progress
182
184
*/
183
185
184
- const onScrub = ( value : number ) => {
186
+ const onScrub = ( value : number ) : void => {
185
187
// Clear any timers already running
186
188
clearInterval ( intervalRef . current as NodeJS . Timeout )
187
189
audioRef . current . currentTime = value
@@ -190,6 +192,30 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
190
192
191
193
// -----------
192
194
195
+ /**
196
+ * play song
197
+ * @function
198
+ * @description play the current song
199
+ */
200
+
201
+ const play = ( ) : void => {
202
+ setIsPlaying ( true )
203
+ }
204
+
205
+ // -----------
206
+
207
+ /**
208
+ * pause song
209
+ * @function
210
+ * @description pause the current song
211
+ */
212
+
213
+ const pause = ( ) : void => {
214
+ setIsPlaying ( false )
215
+ }
216
+
217
+ // -----------
218
+
193
219
/**
194
220
* change scrub value
195
221
* @function
@@ -199,7 +225,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
199
225
* this optional function
200
226
*/
201
227
202
- const onScrubEnd = ( ) => {
228
+ const onScrubEnd = ( ) : void => {
203
229
// If not already playing, start
204
230
if ( ! isPlaying ) {
205
231
setIsPlaying ( true )
@@ -216,7 +242,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
216
242
* if its first song, play at last song in tracks list
217
243
*/
218
244
219
- const toPrevTrack = ( ) => {
245
+ const toPrevTrack = ( ) : void => {
220
246
if ( isShuffleList ) {
221
247
ShufflePlay ( )
222
248
} else {
@@ -237,7 +263,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
237
263
* if the last song, come at first song on tracks list
238
264
*/
239
265
240
- const toNextTrack = ( ) => {
266
+ const toNextTrack = ( ) : void => {
241
267
if ( isShuffleList ) {
242
268
ShufflePlay ( )
243
269
} else {
@@ -255,7 +281,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
255
281
* @description forward to 5s later of playing song
256
282
*/
257
283
258
- const forward = ( ) => {
284
+ const forward = ( ) : void => {
259
285
audioRef . current . currentTime += 5
260
286
}
261
287
@@ -265,7 +291,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
265
291
* @description backward to 5s before of Track progress
266
292
*/
267
293
268
- const backward = ( ) => {
294
+ const backward = ( ) : void => {
269
295
audioRef . current . currentTime -= 5
270
296
}
271
297
@@ -275,7 +301,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
275
301
* @description set the player speed to (0.5)
276
302
*/
277
303
278
- const playSlow = ( ) => {
304
+ const playSlow = ( ) : void => {
279
305
setSpeed ( 0.5 )
280
306
}
281
307
@@ -285,7 +311,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
285
311
* @description set the player speed to normal mode, (1)
286
312
*/
287
313
288
- const playNormal = ( ) => {
314
+ const playNormal = ( ) : void => {
289
315
setSpeed ( 1 )
290
316
}
291
317
@@ -295,7 +321,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
295
321
* @description set player speed to (2), it be play 2x faster than normal mode
296
322
*/
297
323
298
- const playFast = ( ) => {
324
+ const playFast = ( ) : void => {
299
325
setSpeed ( 2 )
300
326
}
301
327
@@ -307,7 +333,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
307
333
* get a random index from tracks length and play it
308
334
*/
309
335
310
- const ShufflePlay = ( ) => {
336
+ const ShufflePlay = ( ) : void => {
311
337
let songsLength : number = tracks . length - 1
312
338
let random : number = Math . floor ( Math . random ( ) * songsLength )
313
339
setTrackIndex ( random )
@@ -353,6 +379,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
353
379
354
380
useLayoutEffect ( ( ) => {
355
381
audioRef . current . pause ( )
382
+ setIsPlaying ( false ) ;
356
383
setIsLoading ( true )
357
384
setBuffered ( 0 )
358
385
@@ -389,7 +416,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
389
416
* if some time you need get player states seconds by seconds can use it.
390
417
*/
391
418
392
- const Logger = ( ) => {
419
+ const Logger = ( ) : void => {
393
420
console . log ( {
394
421
trackIndex,
395
422
duration : ConvertTimeToText ( duration ) ,
@@ -409,7 +436,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
409
436
// *********
410
437
// **
411
438
// ============== return data
412
- const data : PlayerType = {
439
+ const data : PlayerType = {
413
440
Logger, // log the states
414
441
isLoading, // loading state
415
442
isHaveError, // error state
@@ -424,6 +451,8 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
424
451
onScrubEnd,
425
452
isPlaying, // playing state
426
453
setIsPlaying, // playing state setter
454
+ play, // play current song
455
+ pause, // pause current song
427
456
toNextTrack, // play next song function
428
457
toPrevTrack, // play prevent song function
429
458
isRepeat, // repeat state
@@ -453,7 +482,7 @@ export const Reaplay = ({ tracks, startIndex = 0, children }: Props) => {
453
482
} )
454
483
}
455
484
456
- export type PlayerType = {
485
+ export interface PlayerType {
457
486
Logger : Function
458
487
isLoading : boolean
459
488
isHaveError : boolean
@@ -468,6 +497,8 @@ export type PlayerType = {
468
497
onScrubEnd : Function
469
498
isPlaying : boolean
470
499
setIsPlaying : Function
500
+ play : Function
501
+ pause : Function
471
502
toNextTrack : Function
472
503
toPrevTrack : Function
473
504
isRepeat : boolean
0 commit comments