-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Record and send audio via Chrome microphone (#1996)
Co-authored-by: Rohin Bhargava <[email protected]>
- Loading branch information
1 parent
6692418
commit 1c203cb
Showing
19 changed files
with
776 additions
and
221 deletions.
There are no files selected for viewing
This file contains 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 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
1 change: 1 addition & 0 deletions
1
...enerated/api/resources/api/resources/v1/resources/read/resources/type/types/Base64Type.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ated/api/resources/api/resources/v1/resources/register/resources/type/types/Base64Type.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 @@ | ||
.vercel |
This file contains 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
98 changes: 98 additions & 0 deletions
98
packages/fern-docs/ui/src/playground/form/PlaygroundAudioControls.tsx
This file contains 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,98 @@ | ||
import { FernButton, FernButtonGroup } from "@fern-docs/components"; | ||
import { Download, Octagon, Play } from "lucide-react"; | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
interface PlaygroundAudioControlsProps { | ||
audioUrl: string | null; | ||
fileName?: string; | ||
} | ||
|
||
export function PlaygroundAudioControls({ | ||
audioUrl, | ||
fileName = "recording.webm", | ||
}: PlaygroundAudioControlsProps) { | ||
const [isPlaying, setIsPlaying] = useState(false); | ||
const [currentTime, setCurrentTime] = useState(0); | ||
const [duration, setDuration] = useState(0); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
const audioRef = useRef<HTMLAudioElement | null>(null); | ||
|
||
useEffect(() => { | ||
if (audioRef.current) { | ||
audioRef.current.onended = () => setIsPlaying(false); | ||
audioRef.current.onloadedmetadata = () => { | ||
const audioDuration = audioRef.current?.duration; | ||
if (audioDuration && !isNaN(audioDuration) && isFinite(audioDuration)) { | ||
setDuration(Math.round(audioDuration)); | ||
setIsLoaded(true); | ||
} | ||
}; | ||
audioRef.current.ontimeupdate = () => { | ||
const currentTime = audioRef.current?.currentTime; | ||
if (currentTime && !isNaN(currentTime) && isFinite(currentTime)) { | ||
setCurrentTime(Math.round(currentTime)); | ||
} | ||
}; | ||
} | ||
}, []); | ||
|
||
const formatTime = (seconds: number) => { | ||
const mins = Math.floor(seconds / 60) | ||
.toString() | ||
.padStart(2, "0"); | ||
const secs = (seconds % 60).toString().padStart(2, "0"); | ||
return `${mins}:${secs}`; | ||
}; | ||
|
||
const handlePlayPause = async () => { | ||
if (!audioRef.current || !audioUrl) return; | ||
|
||
if (isPlaying) { | ||
audioRef.current.pause(); | ||
audioRef.current.currentTime = 0; | ||
setCurrentTime(0); | ||
} else { | ||
await audioRef.current.play(); | ||
} | ||
setIsPlaying(!isPlaying); | ||
}; | ||
|
||
const handleDownload = () => { | ||
if (!audioUrl) return; | ||
const a = document.createElement("a"); | ||
a.href = audioUrl; | ||
a.download = fileName; | ||
document.body.appendChild(a); | ||
a.click(); | ||
document.body.removeChild(a); | ||
}; | ||
|
||
if (!audioUrl) return null; | ||
|
||
return ( | ||
<div className="flex items-center gap-2"> | ||
<audio ref={audioRef} src={audioUrl} preload="metadata" /> | ||
{isLoaded && ( | ||
<span className="font-mono text-xs"> | ||
{`${formatTime(currentTime)}/${formatTime(duration)}`} | ||
</span> | ||
)} | ||
<FernButtonGroup> | ||
<FernButton | ||
icon={isPlaying ? <Octagon /> : <Play />} | ||
onClick={handlePlayPause} | ||
size="small" | ||
variant="minimal" | ||
disabled={!audioUrl} | ||
/> | ||
<FernButton | ||
icon={<Download />} | ||
onClick={handleDownload} | ||
size="small" | ||
variant="minimal" | ||
disabled={!audioUrl} | ||
/> | ||
</FernButtonGroup> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.