Playerbar.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { useEffect, useState } from "react"
  2. import { connect } from "react-redux"
  3. import * as action from "../actions"
  4. const Playerbar = ({ player, playTrack, pauseTrack, switchTrack, setTrackVolume, setCurrentTime }) => {
  5. let [_player, setPlayer] = useState()
  6. useEffect(() => {
  7. setPlayer(player)
  8. }, [player, _player])
  9. return (
  10. <>
  11. {_player?.track ?
  12. <footer style={{display: `${_player?.track ? 'block' : 'none'}`, width: '95%', margin:'0 auto'}}>
  13. <input
  14. style={{width:'100%'}}
  15. type="range" min="0" max={_player?.duration} step="any"
  16. value={_player?.currentTime}
  17. onChange={(e) => setCurrentTime(e.target.value)}
  18. />
  19. <div style={{display:"flex", justifyContent:"space-between", width:'95%', margin:'0 auto'}}>
  20. <small style={{margin: '0 10px'}}>
  21. {Math.floor((_player?.currentTime / 60) % 60) < 10?
  22. `0` + Math.floor((_player?.currentTime / 60) % 60) : Math.floor((_player?.currentTime / 60) % 60)} :
  23. {Math.floor(_player?.currentTime % 60) < 10?
  24. `0`+ Math.floor(_player?.currentTime % 60) : Math.floor(_player?.currentTime % 60)}
  25. </small>
  26. {_player ? (_player.track?.id3?.artist && _player.track?.id3?.title ?
  27. <strong>{_player.track?.id3?.artist} - {_player.track?.id3?.title}</strong> :
  28. <strong>{_player.track?.originalFileName}</strong>) : ''}
  29. <small style={{margin: '0 10px'}}>
  30. {Math.floor((_player?.duration / 60) % 60) < 10?
  31. `0` + Math.floor((_player?.duration / 60) % 60) : Math.floor((_player?.duration / 60) % 60)} :
  32. {Math.floor(_player?.duration % 60) < 10?
  33. `0`+ Math.floor(_player?.duration % 60) : Math.floor(_player?.duration % 60)}
  34. </small>
  35. </div>
  36. <div style={{marginTop:'0.5%'}}>
  37. <button
  38. style={{ fontSize: '2vh' }}
  39. onClick={() => switchTrack(false, _player?.playlistIndex, _player?.playlist)}
  40. >
  41. {_player?.playlistIndex === 0 ? `\u23F4` : `\u23EE`}
  42. </button>
  43. {_player?.isPlaying ?
  44. <button style={{ fontSize: '2vh' }} onClick={() => pauseTrack()}>{`\u23F8`}</button> :
  45. <button style={{ fontSize: '2vh' }} onClick={() => playTrack()}>{`\u23F5`}</button>
  46. }
  47. <button
  48. style={{ fontSize: '2vh' }}
  49. onClick={() => switchTrack(true, _player?.playlistIndex, _player?.playlist)}
  50. >
  51. {_player?.playlistIndex === _player?.playlist?.length - 1 ? `\u23F5` : `\u23ED`}
  52. </button>
  53. <input style={{marginLeft: '1%'}} type="range" min="0" max="1" step="any" onChange={(e) => setTrackVolume(e.target.value)} />
  54. </div>
  55. <small>{_player?.playlistIndex}</small>
  56. </footer> : <div style={{width:'inherit', height:'inherit', padding:'2em'}}>C'mon, Push the play button on some track :)</div>}
  57. </>
  58. )
  59. }
  60. export const PlayerbarConnect = connect(
  61. state => ({ player: state.player || {} }),
  62. {
  63. playTrack: action.playTrack,
  64. pauseTrack: action.pauseTrack,
  65. switchTrack: action.switchTrack,
  66. setTrackVolume: action.setTrackVolume,
  67. setCurrentTime: action.setNewTrackCurrentTime
  68. }
  69. )(Playerbar)