feat: show audio bars during audio clip playback
This commit is contained in:
@@ -2,6 +2,8 @@ import { useEffect, useRef, useState } from "react";
|
|||||||
import type { Particle } from "@/api/types";
|
import type { Particle } from "@/api/types";
|
||||||
import { useDownloadUrl } from "@/hooks/use-download-url";
|
import { useDownloadUrl } from "@/hooks/use-download-url";
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
|
import { AudioLevelBars } from "@/components/audio/audio-level-bars";
|
||||||
|
import { useAudioSource } from "@/components/audio/use-audio-source";
|
||||||
|
|
||||||
type MediaParticle = Extract<Particle, { type: "media" }>;
|
type MediaParticle = Extract<Particle, { type: "media" }>;
|
||||||
|
|
||||||
@@ -43,9 +45,12 @@ export function MediaParticleView({
|
|||||||
|
|
||||||
const videoRef = useRef<HTMLVideoElement>(null);
|
const videoRef = useRef<HTMLVideoElement>(null);
|
||||||
const audioRef = useRef<HTMLAudioElement>(null);
|
const audioRef = useRef<HTMLAudioElement>(null);
|
||||||
|
const isAudio = particle.properties.mime_type?.startsWith("audio/");
|
||||||
const [currentTimeMs, setCurrentTimeMs] = useState(0);
|
const [currentTimeMs, setCurrentTimeMs] = useState(0);
|
||||||
|
|
||||||
const isAudio = particle.properties.mime_type?.startsWith("audio/");
|
// WORKAROUND: useAudioSource needs an audio element, but audioRef is only set after mount
|
||||||
|
const [audioEl, setAudioEl] = useState<HTMLAudioElement | null>(null);
|
||||||
|
const audioSource = useAudioSource(audioEl);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = isAudio ? audioRef.current : videoRef.current;
|
const el = isAudio ? audioRef.current : videoRef.current;
|
||||||
@@ -76,7 +81,10 @@ export function MediaParticleView({
|
|||||||
return (
|
return (
|
||||||
<div className="relative flex h-full w-full items-center justify-center bg-black/90">
|
<div className="relative flex h-full w-full items-center justify-center bg-black/90">
|
||||||
<audio
|
<audio
|
||||||
ref={audioRef}
|
ref={(el) => {
|
||||||
|
audioRef.current = el;
|
||||||
|
setAudioEl(el);
|
||||||
|
}}
|
||||||
crossOrigin="anonymous"
|
crossOrigin="anonymous"
|
||||||
src={url}
|
src={url}
|
||||||
autoPlay
|
autoPlay
|
||||||
@@ -86,6 +94,12 @@ export function MediaParticleView({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{audioSource && (
|
||||||
|
<div className="z-10 absolute bottom-15">
|
||||||
|
<AudioLevelBars sourceNode={audioSource.sourceNode} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<DurationPill
|
<DurationPill
|
||||||
currentTimeMs={currentTimeMs}
|
currentTimeMs={currentTimeMs}
|
||||||
totalDurationMs={particle.properties.duration_ms}
|
totalDurationMs={particle.properties.duration_ms}
|
||||||
|
|||||||
Reference in New Issue
Block a user