-
Notifications
You must be signed in to change notification settings - Fork 17
/
Player.js
160 lines (145 loc) Β· 4.63 KB
/
Player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import "./Player.css";
import { useEffect, useRef, useState } from "react";
import { connect, useDispatch } from "react-redux";
import { forwardsSvg, backwardsSvg, shuffleSvg } from "../svg";
import { setPlayerState, selectSongById } from "../actions";
import Progress from "./ProgressBar";
import SongTime from "./SongTime";
const Player = ({
selectedSongId,
defaultSong,
playerState,
songs,
selectSongById,
volume,
}) => {
const dispatch = useDispatch();
const [shuffled, setShuffled] = useState(false);
const audioRef = useRef();
let clicked = false;
const spaceDownFunc = (event) => {
if (event.keyCode === 32 && !clicked) {
clicked = true;
document.getElementsByClassName("main-control")[0].click();
}
};
const spaceUpFunc = (event) => {
if (event.keyCode === 32 && clicked) {
clicked = false;
}
};
useEffect(() => {
document.addEventListener("keydown", spaceDownFunc);
document.addEventListener("keyup", spaceUpFunc);
}, []);
if (selectedSongId < 0 || selectedSongId > songs.length - 1) {
selectSongById(0);
}
useEffect(() => {
if (audioRef.current) {
audioRef.current.volume = volume / 500;
}
}, [volume]);
const onMusicPlay = (e) => {
e.preventDefault();
if (!playerState) {
audioRef.current.play();
dispatch({ type: "PLAYER_STATE_SELECTED", payload: 1 });
} else {
audioRef.current.pause();
dispatch({ type: "PLAYER_STATE_SELECTED", payload: 0 });
}
};
const onBackwardClick = () => {
if (selectedSongId > 0) {
selectSongById(selectedSongId - 1);
}
};
const onForwardClick = () => {
if (selectedSongId < songs.length - 1) {
selectSongById(selectedSongId + 1);
}
};
useEffect(() => {
dispatch({ type: "PLAYER_STATE_SELECTED", payload: 1 });
audioRef.current.play();
// console.log(audioRef.current.duration);
document.getElementById("focus-link").click();
window.history.pushState({}, "", "/");
}, [selectedSongId, dispatch]);
useEffect(() => {
dispatch({ type: "PLAYER_STATE_SELECTED", payload: 0 });
audioRef.current.pause();
}, [dispatch]);
return (
<div id="player">
<SongTime />
<div
className="control"
id={shuffled ? `active` : null}
onClick={() => {
setShuffled(!shuffled);
// console.log("shuffle: " + !shuffled);
}}
>
{shuffleSvg}
</div>
<div className="control" onClick={onBackwardClick}>
{backwardsSvg}
</div>
<div className="main-control control" onClick={onMusicPlay}>
<i
className={`fas fa-${
playerState ? "pause" : "play"
}-circle`}
></i>
</div>
<div className="control" onClick={onForwardClick}>
{forwardsSvg}
</div>
<Progress />
<audio
id="main-track"
controls
src={songs[selectedSongId].url}
preload="true"
onEnded={() => {
selectSongById(
shuffled
? Math.round(Math.random() * songs.length)
: selectedSongId + 1
);
}}
onLoadedMetadata={() => {
dispatch({
type: "SET_DURATION",
payload: audioRef.current.duration,
});
setInterval(() => {
dispatch({
type: "SET_CURRENT_LOCATION",
payload: audioRef.current.currentTime,
});
}, 1000);
}}
ref={audioRef}
hidden
>
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
);
};
const mapStateToProps = (state) => {
return {
selectedSongId: state.selectedSongId,
defaultSong: state.songs[0],
playerState: state.playerState,
songs: state.songs,
volume: state.volume,
};
};
export default connect(mapStateToProps, { setPlayerState, selectSongById })(
Player
);