Skip to content

Commit

Permalink
fix: skip songs with invalid media source
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Dec 21, 2022
1 parent e3d07e1 commit 886586d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export function APlayer({

const audioControl = useAudioControl({
initialVolume: volume,
onError() {
if (playlist.hasNextSong) {
playlist.next()
}
},
onEnded() {
if (playlist.hasNextSong) {
playlist.next()
Expand Down
16 changes: 15 additions & 1 deletion src/hooks/useAudioControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function useCreateAudioElement(
| "onEnded"
| "onWaiting"
| "onCanPlay"
| "onError"
>,
) {
const audioElementRef = useRef<HTMLAudioElement>()
Expand Down Expand Up @@ -55,6 +56,18 @@ function useCreateAudioElement(
}
}, [options?.onWaiting])

useEffect(() => {
const audio = audioElementRef.current

if (audio && options?.onError) {
audio.addEventListener("error", options.onError)

return () => {
audio.removeEventListener("error", options.onError)
}
}
}, [options?.onError])

useEffect(() => {
const audio = audioElementRef.current

Expand Down Expand Up @@ -100,7 +113,7 @@ type UseAudioControlOptions = CreateAudioElementOptions &
React.AudioHTMLAttributes<HTMLAudioElement>,
HTMLAudioElement
>,
"onEnded"
"onEnded" | "onError"
>

export function useAudioControl(options: UseAudioControlOptions) {
Expand Down Expand Up @@ -133,6 +146,7 @@ export function useAudioControl(options: UseAudioControlOptions) {
onCanPlay() {
setLoading(false)
},
onError: options.onError,
onEnded: options.onEnded,
})
const [isLoading, setLoading] = useState(false)
Expand Down

0 comments on commit 886586d

Please sign in to comment.