-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuseAudioPlayer.js
More file actions
71 lines (61 loc) · 1.93 KB
/
useAudioPlayer.js
File metadata and controls
71 lines (61 loc) · 1.93 KB
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
import { useRef, useState, useEffect, useCallback } from 'react';
import { DEFAULT_VOLUME } from './constants';
export const useAudioPlayer = (song, onSongEnd) => {
const audioRef = useRef(null);
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [volume, setVolumeState] = useState(DEFAULT_VOLUME);
useEffect(() => {
const audio = new Audio();
audio.volume = DEFAULT_VOLUME;
audioRef.current = audio;
const onTime = () => setCurrentTime(audio.currentTime);
const onLoaded = () => setDuration(audio.duration);
const onEnded = () => {
setIsPlaying(false);
if (onSongEnd) {
onSongEnd();
}
};
audio.addEventListener('timeupdate', onTime);
audio.addEventListener('loadedmetadata', onLoaded);
audio.addEventListener('ended', onEnded);
return () => {
audio.removeEventListener('timeupdate', onTime);
audio.removeEventListener('loadedmetadata', onLoaded);
audio.removeEventListener('ended', onEnded);
audio.pause();
};
}, []);
useEffect(() => {
if (song?.previewUrl && audioRef.current) {
audioRef.current.src = song.previewUrl;
audioRef.current.play().catch(() => {});
setIsPlaying(true);
}
}, [song?.trackId]);
const togglePlay = useCallback(() => {
if (!audioRef.current?.src) {
return;
}
if (isPlaying) {
audioRef.current.pause();
} else {
audioRef.current.play().catch(() => {});
}
setIsPlaying(!isPlaying);
}, [isPlaying]);
const seek = useCallback((time) => {
if (audioRef.current) {
audioRef.current.currentTime = time;
}
}, []);
const setVolume = useCallback((v) => {
setVolumeState(v);
if (audioRef.current) {
audioRef.current.volume = v;
}
}, []);
return { isPlaying, currentTime, duration, volume, togglePlay, seek, setVolume };
};