-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
audio player and fix sending feedback
- Loading branch information
Showing
6 changed files
with
158 additions
and
34 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
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,92 @@ | ||
import { ActionIcon, Group, Paper, Slider, Text } from "@mantine/core"; | ||
import { IconPlayerPause, IconPlayerPlay } from "@tabler/icons-react"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import classes from "./index.module.css"; | ||
|
||
interface AudioPlayerProps { | ||
src: string; | ||
compact?: boolean; | ||
} | ||
|
||
export function AudioPlayer({ src, compact = false }: AudioPlayerProps) { | ||
const [isPlaying, setIsPlaying] = useState(false); | ||
const [currentTime, setCurrentTime] = useState(0); | ||
const [duration, setDuration] = useState(0); | ||
const audioRef = useRef<HTMLAudioElement>(null); | ||
|
||
useEffect(() => { | ||
const audio = audioRef.current; | ||
if (!audio) return; | ||
|
||
const updateTime = () => setCurrentTime(audio.currentTime); | ||
const handleLoadedMetadata = () => setDuration(audio.duration); | ||
const handleEnded = () => setIsPlaying(false); | ||
|
||
audio.addEventListener("timeupdate", updateTime); | ||
audio.addEventListener("loadedmetadata", handleLoadedMetadata); | ||
audio.addEventListener("ended", handleEnded); | ||
|
||
return () => { | ||
audio.removeEventListener("timeupdate", updateTime); | ||
audio.removeEventListener("loadedmetadata", handleLoadedMetadata); | ||
audio.removeEventListener("ended", handleEnded); | ||
}; | ||
}, []); | ||
|
||
const togglePlay = () => { | ||
if (!audioRef.current) return; | ||
|
||
if (isPlaying) { | ||
audioRef.current.pause(); | ||
} else { | ||
audioRef.current.play(); | ||
} | ||
setIsPlaying(!isPlaying); | ||
}; | ||
|
||
const handleSliderChange = (value: number) => { | ||
if (!audioRef.current) return; | ||
audioRef.current.currentTime = value; | ||
setCurrentTime(value); | ||
}; | ||
|
||
const formatTime = (time: number) => { | ||
const minutes = Math.floor(time / 60); | ||
const seconds = Math.floor(time % 60); | ||
return `${minutes}:${seconds.toString().padStart(2, "0")}`; | ||
}; | ||
|
||
return ( | ||
<Paper className={classes.audioPlayer} p="xs"> | ||
<audio ref={audioRef} src={src} /> | ||
<Group gap="xs"> | ||
<ActionIcon | ||
variant="subtle" | ||
onClick={togglePlay} | ||
size={compact ? "sm" : "md"} | ||
> | ||
{isPlaying ? ( | ||
<IconPlayerPause size={16} /> | ||
) : ( | ||
<IconPlayerPlay size={16} /> | ||
)} | ||
</ActionIcon> | ||
|
||
<Slider | ||
value={currentTime} | ||
onChange={handleSliderChange} | ||
max={duration} | ||
min={0} | ||
size="xs" | ||
style={{ flex: 1 }} | ||
/> | ||
|
||
{!compact && ( | ||
<Text size="xs" c="dimmed" w={45}> | ||
{formatTime(currentTime)} | ||
</Text> | ||
)} | ||
</Group> | ||
</Paper> | ||
); | ||
} |
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
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