Skip to content

Commit 886586d

Browse files
committed
fix: skip songs with invalid media source
1 parent e3d07e1 commit 886586d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/components/player.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export function APlayer({
5454

5555
const audioControl = useAudioControl({
5656
initialVolume: volume,
57+
onError() {
58+
if (playlist.hasNextSong) {
59+
playlist.next()
60+
}
61+
},
5762
onEnded() {
5863
if (playlist.hasNextSong) {
5964
playlist.next()

src/hooks/useAudioControl.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function useCreateAudioElement(
2020
| "onEnded"
2121
| "onWaiting"
2222
| "onCanPlay"
23+
| "onError"
2324
>,
2425
) {
2526
const audioElementRef = useRef<HTMLAudioElement>()
@@ -55,6 +56,18 @@ function useCreateAudioElement(
5556
}
5657
}, [options?.onWaiting])
5758

59+
useEffect(() => {
60+
const audio = audioElementRef.current
61+
62+
if (audio && options?.onError) {
63+
audio.addEventListener("error", options.onError)
64+
65+
return () => {
66+
audio.removeEventListener("error", options.onError)
67+
}
68+
}
69+
}, [options?.onError])
70+
5871
useEffect(() => {
5972
const audio = audioElementRef.current
6073

@@ -100,7 +113,7 @@ type UseAudioControlOptions = CreateAudioElementOptions &
100113
React.AudioHTMLAttributes<HTMLAudioElement>,
101114
HTMLAudioElement
102115
>,
103-
"onEnded"
116+
"onEnded" | "onError"
104117
>
105118

106119
export function useAudioControl(options: UseAudioControlOptions) {
@@ -133,6 +146,7 @@ export function useAudioControl(options: UseAudioControlOptions) {
133146
onCanPlay() {
134147
setLoading(false)
135148
},
149+
onError: options.onError,
136150
onEnded: options.onEnded,
137151
})
138152
const [isLoading, setLoading] = useState(false)

0 commit comments

Comments
 (0)