Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Howler #41

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@shelf/jest-mongodb": "^4.3.2",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.1.0",
"@types/howler": "^2.2.12",
"@types/jest": "^29.5.14",
"@types/node": "^20",
"@types/react": "^18",
Expand Down
17 changes: 17 additions & 0 deletions src/app/audioplayer/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";
import { useState, useRef } from "react";
import Navbar from "@/components/Navbar";
import AudioPlayer from "@/components/AudioPlayer";

export default function Page() {
return (
<main>
<Navbar />
<h1 className="text-3xl font-bold underline">Hello world!</h1> {/* sample audio formatting */}
<AudioPlayer
audioURL="https://og83eubqb6o3yptf.public.blob.vercel-storage.com/Bill_Deneen_Kathleen_Final-KDIk27mD6mgRGoXc2MyH7NLrmvXFAR.wav"
name="Test Audio"
/>
</main>
);
}
2 changes: 0 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Navbar from "@/components/Navbar";

export default function Home() {
return (
<main>
<Navbar />
<h1 className="text-3xl font-bold underline">Hello world!</h1>{" "}
</main>
);
}
87 changes: 87 additions & 0 deletions src/components/AudioPlayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use client";
import React, { useState, useEffect, useRef } from "react";
import { Howl } from "howler";

interface AudioPlayerProps {
audioURL: string;
name: string;
}

const AudioPlayer: React.FC<AudioPlayerProps> = ({ audioURL, name }) => {
const [isPlaying, setIsPlaying] = useState<boolean>(false);
const [progress, setProgress] = useState<number>(0);
const soundRef = useRef<Howl | null>(null);

// Howl instance creation/update on audioURL change
useEffect(() => {
if (audioURL) {
if (soundRef.current) {
soundRef.current.unload();
}
soundRef.current = new Howl({
src: [audioURL],
html5: true,
onend: () => {
setIsPlaying(false);
setProgress(0);
},
});
}
return () => {
if (soundRef.current) {
soundRef.current.unload();
}
};
}, [audioURL]);

// To handle playing the audio
const handlePlay = () => {
if (soundRef.current) {
soundRef.current.play();
setIsPlaying(true);
}
};

// To handle pausing the audio.
const handlePause = () => {
if (soundRef.current) {
soundRef.current.pause();
setIsPlaying(false);
}
};

// Attempt to update progress bar
useEffect(() => {
let intervalId: NodeJS.Timeout;
if (isPlaying && soundRef.current) {
intervalId = setInterval(() => {
if (!soundRef.current) return;
const currentTime = soundRef.current.seek() as number;
const duration = soundRef.current.duration();
if (duration > 0) {
setProgress((currentTime / duration) * 100);
}
}, 1000);
}
return () => clearInterval(intervalId);
}, [isPlaying]);

return (
<div className="audio-player">
<h3>{name}</h3>
<div>
<button onClick={handlePlay} disabled={isPlaying}>
Play
</button>
<button onClick={handlePause} disabled={!isPlaying}>
Pause
</button>
</div>
<div>
<progress value={progress} max="100" style={{ width: "100%" }} />
</div>
</div>
);
};

export default AudioPlayer;
Loading