-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Audio recording feature #5263
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
Open
TheNexter
wants to merge
10
commits into
usememos:main
Choose a base branch
from
TheNexter:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Audio recording feature #5263
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
abcab8d
Audio recording
TheNexter 9027ece
Better design audio record button
TheNexter bbc82f9
Custom audio player that match design of memos
TheNexter 61a3142
Audio player redesign, better looking version
TheNexter 25f1bcf
Little refine
TheNexter 063128e
little css fix
TheNexter 02b36d5
Lint fix (sorry)
TheNexter 1cd7973
Apply suggestion made by Copilot
TheNexter 6e6ade0
Fix lint
TheNexter eb7f779
Apply new proposition from Copilot + little refine in player design (…
TheNexter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { PauseIcon, PlayIcon } from "lucide-react"; | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import { Button } from "@/components/ui/button"; | ||
|
|
||
| interface Props { | ||
| src: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| const AudioPlayer = ({ src, className = "" }: Props) => { | ||
| const audioRef = useRef<HTMLAudioElement>(null); | ||
| const [isPlaying, setIsPlaying] = useState(false); | ||
| const [currentTime, setCurrentTime] = useState(0); | ||
| const [duration, setDuration] = useState(0); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| const audio = audioRef.current; | ||
| if (!audio) return; | ||
|
|
||
| // Reset state when src changes | ||
| setIsPlaying(false); | ||
| setCurrentTime(0); | ||
| setDuration(0); | ||
| setIsLoading(true); | ||
|
|
||
| const handleLoadedMetadata = () => { | ||
| if (audio.duration && !isNaN(audio.duration) && isFinite(audio.duration)) { | ||
| setDuration(audio.duration); | ||
| } | ||
| setIsLoading(false); | ||
| }; | ||
|
|
||
| const handleTimeUpdate = () => { | ||
| setCurrentTime(audio.currentTime); | ||
| if (audio.duration && !isNaN(audio.duration) && isFinite(audio.duration)) { | ||
| setDuration((prev) => (prev === 0 ? audio.duration : prev)); | ||
| } | ||
| }; | ||
|
|
||
| const handleEnded = () => { | ||
| setIsPlaying(false); | ||
| setCurrentTime(0); | ||
| }; | ||
|
|
||
| const handleLoadedData = () => { | ||
| // For files without proper duration in metadata, | ||
| // try to get it after some data is loaded | ||
| if (audio.duration && !isNaN(audio.duration) && isFinite(audio.duration)) { | ||
| setDuration(audio.duration); | ||
| setIsLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| audio.addEventListener("loadedmetadata", handleLoadedMetadata); | ||
| audio.addEventListener("loadeddata", handleLoadedData); | ||
| audio.addEventListener("timeupdate", handleTimeUpdate); | ||
| audio.addEventListener("ended", handleEnded); | ||
|
|
||
| return () => { | ||
| audio.removeEventListener("loadedmetadata", handleLoadedMetadata); | ||
| audio.removeEventListener("loadeddata", handleLoadedData); | ||
| audio.removeEventListener("timeupdate", handleTimeUpdate); | ||
| audio.removeEventListener("ended", handleEnded); | ||
| }; | ||
| }, [src]); | ||
|
|
||
| useEffect(() => { | ||
| const handlePlayAudio = (e: Event) => { | ||
| const customEvent = e as CustomEvent; | ||
| if (customEvent.detail !== audioRef.current && isPlaying) { | ||
| audioRef.current?.pause(); | ||
| setIsPlaying(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("play-audio", handlePlayAudio); | ||
| return () => { | ||
| document.removeEventListener("play-audio", handlePlayAudio); | ||
| }; | ||
| }, [isPlaying]); | ||
|
|
||
| const togglePlayPause = async () => { | ||
| const audio = audioRef.current; | ||
| if (!audio) return; | ||
|
|
||
| if (isPlaying) { | ||
| audio.pause(); | ||
| setIsPlaying(false); | ||
| } else { | ||
| try { | ||
| // Stop other audio players | ||
| const event = new CustomEvent("play-audio", { detail: audio }); | ||
| document.dispatchEvent(event); | ||
|
|
||
| await audio.play(); | ||
| setIsPlaying(true); | ||
| } catch (error) { | ||
| console.error("Failed to play audio:", error); | ||
| setIsPlaying(false); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const handleSeek = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const audio = audioRef.current; | ||
| if (!audio) return; | ||
|
|
||
| const newTime = parseFloat(e.target.value); | ||
| audio.currentTime = newTime; | ||
| setCurrentTime(newTime); | ||
| }; | ||
|
|
||
| const formatTime = (time: number): string => { | ||
| if (!isFinite(time) || isNaN(time)) return "0:00"; | ||
|
|
||
| const minutes = Math.floor(time / 60); | ||
| const seconds = Math.floor(time % 60); | ||
| return `${minutes}:${seconds.toString().padStart(2, "0")}`; | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={`flex items-center gap-2 ${className}`}> | ||
| <audio ref={audioRef} src={src} preload="metadata" /> | ||
| <Button | ||
| variant="ghost" | ||
| size="sm" | ||
| onClick={togglePlayPause} | ||
| disabled={isLoading} | ||
| className="shrink-0 p-0 h-5 w-5 hover:bg-transparent text-muted-foreground hover:text-foreground" | ||
| aria-label={isPlaying ? "Pause audio" : "Play audio"} | ||
| > | ||
| {isPlaying ? <PauseIcon className="w-5 h-5" /> : <PlayIcon className="w-5 h-5" />} | ||
| </Button> | ||
| <input | ||
| type="range" | ||
| min="0" | ||
| max={duration || 0} | ||
| value={currentTime} | ||
| onChange={handleSeek} | ||
| disabled={isLoading || !duration} | ||
| className="w-full min-w-[128px] h-1 rounded-md bg-secondary cursor-pointer appearance-none [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3 [&::-webkit-slider-thumb]:bg-primary [&::-webkit-slider-thumb]:rounded-full [&::-moz-range-thumb]:w-3 [&::-moz-range-thumb]:h-3 [&::-moz-range-thumb]:bg-primary [&::-moz-range-thumb]:border-none [&::-moz-range-thumb]:rounded-full" | ||
| aria-label="Seek audio position" | ||
| /> | ||
| <span className="text-sm text-muted-foreground whitespace-nowrap"> | ||
| {formatTime(currentTime)} / {formatTime(duration)} | ||
| </span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default AudioPlayer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the component unmounts or the recording UI is hidden while recording is in progress, the media stream and timer are not cleaned up. This will leave the microphone active and the timer running. Add cleanup logic in a
useEffectcleanup function to callcancelRecordingwhen the component unmounts.