Skip to content

Commit

Permalink
Handle webm infinity duration
Browse files Browse the repository at this point in the history
  • Loading branch information
samhirtarif committed Jul 6, 2023
1 parent bd4945d commit a79284b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Binary file added public/audios/audio.webm
Binary file not shown.
12 changes: 12 additions & 0 deletions src/components/AudioPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ const AudioPlayer: React.FC<
audio.ontimeupdate = ontimeupdate;
audio.onvolumechange = onvolumechange;
audio.onwaiting = onwaiting;

audio.addEventListener("ended", onAudioEnded);
setBlob(blob);
return blob;
})
Expand All @@ -248,8 +250,18 @@ const AudioPlayer: React.FC<
setDuration(buffer.duration);
});
});

return () => {
// clean up
audio.removeEventListener("ended", onAudioEnded);
};
}, []);

const onAudioEnded = (): void => {
setIsPlaying(false);
setAudioTime(0);
};

const playAudio = (): void => {
if (!audio.src) return;
if (audio.duration !== Infinity) setDuration(audio.duration);
Expand Down
4 changes: 2 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AudioPlayer } from "./components/AudioPlayer";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<AudioPlayer
src="audios/test.mp3"
src="audios/audio.webm"
minimal={false}
width={350}
trackHeight={75}
Expand All @@ -20,7 +20,7 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
skipDuration={2}
showLoopOption={true}
showVolumeControl={true}

onended={() => console.log("ENDED")}
// seekBarColor="purple"
// volumeControlColor="blue"
// hideSeekBar={true}
Expand Down

0 comments on commit a79284b

Please sign in to comment.