-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·298 lines (265 loc) · 8.05 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>useMedia React hooks</title>
<style>
html,
body {
margin: 0;
padding: 0;
background: rgba(0, 0, 0, 0.7);
}
#root {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.player {
padding: 20px;
}
.player .disc {
margin: 0 auto;
position: relative;
border-radius: 50%;
width: 355px;
height: 355px;
background-image: url("https://cdn.jsdelivr.net/gh/chenxiaoyao6228/cloudimg@main/2023/bg_music_1.png");
background-size: cover;
background-repeat: no-repeat;
}
.player .control {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
}
.player .control .duration {
margin: 20px 0;
padding: 0 100px 0 20px;
display: flex;
align-items: center;
color: #fff;
}
.player .control .play {
flex: 1;
display: flex;
justify-content: center;
justify-content: center;
}
.player .control .sound {
flex: 1;
height: 100px;
position: absolute;
right: 20px;
bottom: 20px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.rotate {
transform-origin: center;
animation: spin 2s linear infinite;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@babel/[email protected]/babel.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/antd/dist/antd.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const {
useState,
useCallback,
useRef,
useEffect,
useLayoutEffect,
useMemo,
} = React;
/*
* --------------------------useMeida hooks-----------------------------------------
* audio和video有差不多的api, useMedioControl都适用
*/
// 播放暂停控制
const useMediaPlay = (mediaRef) => {
const [playing, setPlaying] = useState(false);
useEffect(() => {
if (!mediaRef.current) return;
const audio = mediaRef.current;
audio.addEventListener("playing", () => {
setPlaying(true);
});
audio.addEventListener("pause", () => {
setPlaying(false);
});
}, []);
const play = () => {
mediaRef.current && mediaRef.current.play();
};
const pause = () => {
mediaRef.current && mediaRef.current.pause();
};
return {
playing,
play,
pause,
};
};
// 播放时间控制
const useMediaDuraion = (mediaRef) => {
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0.1);
useEffect(() => {
if (!mediaRef.current) return;
const audio = mediaRef.current;
audio.addEventListener("durationchange", () => {
setDuration(audio.duration);
});
audio.addEventListener("timeupdate", () => {
setCurrentTime(audio.currentTime);
});
}, []);
const updateCurrentTime = (value) => {
mediaRef.current.currentTime = value;
};
return {
currentTime,
updateCurrentTime,
duration,
};
};
// 声音操作控制
const useMediaVolume = (mediaRef) => {
// volume的范围: [0,1]
const [volume, setVolume] = useState(0.2);
useEffect(() => {
if (!mediaRef.current) return;
const audio = mediaRef.current;
audio.addEventListener("volumechange", () => {
setVolume(mediaRef.current.volume);
});
}, []);
const updateVolume = (value) => {
mediaRef.current.volume = value;
};
return {
volume,
updateVolume,
};
};
// 导出给外部使用的useMedia
const useMediaControl = (item) => {
const mediaRef = useRef(null);
useLayoutEffect(() => {
if (!mediaRef.current) {
mediaRef.current = document.createElement("audio");
mediaRef.current.src = item.src;
mediaRef.current.preload = "auto";
}
});
const { playing, play, pause } = useMediaPlay(mediaRef);
const { currentTime, updateCurrentTime, duration } =
useMediaDuraion(mediaRef);
const { volume, updateVolume } = useMediaVolume(mediaRef);
return {
state: {
playing,
currentTime,
duration,
volume,
},
action: {
play,
pause,
updateCurrentTime,
updateVolume,
},
};
};
// -------------------------------Demo----------------------------------
const { render } = ReactDOM;
const { createElement } = React;
const { Button, Slider, Progress } = antd;
const Demo = () => {
const musicList = useMemo(() => {
return [
{
id: "1",
src: "https://cdn.jsdelivr.net/gh/chenxiaoyao6228/cloudimg@main/2023/useMedia-test-music.mp3",
},
];
}, []);
const [music, setMusic] = useState(musicList[0]);
const audioControl = useMediaControl(music);
const {
state: { playing, currentTime, duration, volume },
action: { play, pause, updateCurrentTime, updateVolume },
} = audioControl;
const percentage = Math.floor((currentTime / duration) * 100);
const togglePlay = playing ? pause : play;
const handleDurationChange = (value) => {
updateCurrentTime((value / 100) * duration);
};
const handleVolumeChange = (value) => {
updateVolume(value / 100);
};
return (
<div className="player">
<div className={`disc ${playing ? "rotate" : ""}`} />
<div className="control">
<div className="duration">
<Slider
min={0}
max={100}
value={percentage}
onChange={handleDurationChange}
style={{
width: "calc(100vw - 300px)",
}}
/>
<div>
{formatTime(currentTime) + "/" + formatTime(duration)}
</div>
</div>
<div className="play">
<Button type="primary" onClick={togglePlay}>
{playing ? "暂停" : "播放"}
</Button>
</div>
<div className="sound">
<Slider
vertical
value={volume * 100}
onChange={handleVolumeChange}
/>
</div>
</div>
</div>
);
};
render(<Demo />, document.getElementById("root"));
function formatTime(s) {
const second = Math.floor(s);
const MIN = 60;
const HOUR = 60 * 60;
const hour = Math.floor(second / HOUR);
const min = Math.floor((second - hour * HOUR) / MIN);
const sec = second - hour * HOUR - min * MIN;
return `${prepend(hour)}:${prepend(min)}:${prepend(sec)}`;
function prepend(p) {
return p < 10 ? `0${p}` : p;
}
}
</script>
</body>
</html>