1- import React , { useState , useEffect } from 'react'
2- import styles from './Player.module.scss' ;
1+ /* eslint-disable react/jsx-filename-extension */
2+ /* eslint-disable react/prop-types */
3+ import React , { useState , useEffect , useRef } from 'react' ;
34import Button from '@material-ui/core/Button' ;
45import ReactAudioPlayer from 'react-audio-player' ;
5- const Player = ( { playlist } ) => {
6- const [ currentIndex , setCurrentIndex ] = useState ( - 1 ) ;
6+ import styles from './Player.module.scss' ;
77
8- const startPlay = ( ) => {
8+ const Player = ( { playlist } ) => {
9+ const [ currentIndex , setCurrentIndex ] = useState ( - 1 ) ;
10+ const audioPlayerElement = useRef ( null ) ;
911
12+ const onPasukEnd = ( ) => {
13+ if ( currentIndex + 1 <= playlist . length - 1 ) {
14+ setCurrentIndex ( currentIndex + 1 ) ;
1015 }
16+ } ;
1117
12- const onPasukEnd = ( ) => {
13- if ( currentIndex + 1 <= playlist . length - 1 ) {
14- setCurrentIndex ( currentIndex + 1 )
18+ const next = ( ) => {
19+ if ( currentIndex + 1 <= playlist . length - 1 ) {
20+ setCurrentIndex ( currentIndex + 1 ) ;
21+ }
22+ } ;
1523
16- }
24+ const back = ( ) => {
25+ if ( currentIndex - 1 >= 0 ) {
26+ setCurrentIndex ( currentIndex - 1 ) ;
1727 }
28+ } ;
1829
19- const next = ( ) => {
20- if ( currentIndex + 1 <= playlist . length - 1 ) {
21- setCurrentIndex ( currentIndex + 1 )
22- }
30+ const pause = ( ) => {
31+ if ( audioPlayerElement . current ) {
32+ audioPlayerElement . current . audioEl . current . pause ( ) ;
2333 }
34+ } ;
2435
25- const back = ( ) => {
26- if ( currentIndex - 1 >= 0 ) {
27- setCurrentIndex ( currentIndex - 1 )
28- }
36+ const play = ( ) => {
37+ if ( audioPlayerElement . current ) {
38+ audioPlayerElement . current . audioEl . current . play ( ) ;
2939 }
40+ } ;
3041
31- useEffect ( ( ) => {
32- setCurrentIndex ( 0 )
33- } , [ playlist ] )
42+ useEffect ( ( ) => {
43+ setCurrentIndex ( 0 ) ;
44+ } , [ playlist ] ) ;
3445
35- if ( currentIndex < 0 ) {
36- return null ;
37- }
38- const currentItem = playlist [ currentIndex ] ;
46+ if ( currentIndex < 0 ) {
47+ return null ;
48+ }
49+ const currentItem = playlist [ currentIndex ] ;
3950
40- if ( ! currentItem ) {
41- return null ;
42- }
51+ if ( ! currentItem ) {
52+ return null ;
53+ }
4354
44- return (
45- < div className = { styles . player } >
46- < div className = { styles . image } >
47- < img src = { currentItem . image } />
48- </ div >
49- < div className = { styles . audio } >
50- < ReactAudioPlayer
51- src = { currentItem . audio }
52- controls
53- autoPlay
54- onEnded = { onPasukEnd }
55- />
56- </ div >
57- < div className = { styles . caption } >
58- פסוק{ currentIndex + 1 } מתוך { playlist . length }
59- </ div >
60- { < div >
61- < Button variant = "contained" color = "primary" onClick = { back } >
62- אחורה
63- </ Button >
55+ return (
56+ < div className = { styles . player } >
57+ < div className = { styles . image } >
58+ < img src = { currentItem . image } />
59+ </ div >
60+ < div className = { styles . audio } >
61+ < ReactAudioPlayer
62+ ref = { audioPlayerElement }
63+ src = { currentItem . audio }
64+ controls
65+ autoPlay
66+ onEnded = { onPasukEnd }
67+ />
68+ </ div >
69+ < div className = { styles . caption } >
70+ פסוק
71+ { currentIndex + 1 }
72+ { ' ' }
73+ מתוך
74+ { ' ' }
75+ { playlist . length }
76+ </ div >
77+ < div >
78+ < Button variant = "contained" color = "primary" onClick = { pause } >
79+ הפסק
80+ </ Button >
81+  
82+ < Button variant = "contained" color = "primary" onClick = { play } >
83+ המשך
84+ </ Button >
85+  
86+ < Button variant = "contained" color = "primary" onClick = { back } >
87+ פסוק אחורה
88+ </ Button >
6489  
6590
66- < Button variant = "contained" color = "primary" onClick = { next } >
67- קדימה
68- </ Button >
91+ < Button variant = "contained" color = "primary" onClick = { next } >
92+ פסוק הבא
93+ </ Button >
6994
70- </ div > }
71- </ div > )
72- }
95+ </ div >
96+ </ div >
97+ ) ;
98+ } ;
7399
74- export default Player ;
100+ export default Player ;
0 commit comments