This commit is contained in:
Sabda Yagra 2025-07-17 08:02:30 +07:00
parent 4675b997f4
commit cabbd2d1da
2 changed files with 27 additions and 28 deletions

View File

@ -1,9 +1,13 @@
import React, { useRef, useState, useEffect } from "react"; import React, { useRef, useState, useEffect } from "react";
const AudioPlayer = (props: {urlAudio: string}) => { interface AudioPlayerProps {
urlAudio: string;
fileName: string; // ✅ Tambahkan props ini
}
const AudioPlayer: React.FC<AudioPlayerProps> = ({ urlAudio, fileName }) => {
const audioRef = useRef<HTMLAudioElement>(null); const audioRef = useRef<HTMLAudioElement>(null);
const [currentTime, setCurrentTime] = useState(0); const [currentTime, setCurrentTime] = useState(0);
const {urlAudio} = props
const playAudio = () => { const playAudio = () => {
audioRef.current?.play(); audioRef.current?.play();
@ -29,7 +33,6 @@ const AudioPlayer = (props: {urlAudio: string}) => {
}; };
audio.addEventListener("timeupdate", updateTime); audio.addEventListener("timeupdate", updateTime);
return () => { return () => {
audio.removeEventListener("timeupdate", updateTime); audio.removeEventListener("timeupdate", updateTime);
}; };
@ -37,24 +40,20 @@ const AudioPlayer = (props: {urlAudio: string}) => {
const formatTime = (time: number) => { const formatTime = (time: number) => {
const minutes = Math.floor(time / 60); const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60) const seconds = Math.floor(time % 60).toString().padStart(2, "0");
.toString()
.padStart(2, "0");
return `${minutes}:${seconds}`; return `${minutes}:${seconds}`;
}; };
return ( return (
<div style={{ textAlign: "center", marginTop: "50px" }}> <div className="mt-2">
<h2>Pemutar Audio</h2> <h2 className="text-lg font-semibold">{fileName}</h2>
<audio ref={audioRef} src={urlAudio} /> <audio ref={audioRef} src={urlAudio} controls className="mt-1 w-full" />
<div style={{ marginTop: "10px" }}> {/* <div className="mt-2 space-x-2">
<button onClick={playAudio}> Play</button> <button onClick={playAudio}> Play</button>
<button onClick={pauseAudio}> Pause</button> <button onClick={pauseAudio}> Pause</button>
<button onClick={stopAudio}> Stop</button> <button onClick={stopAudio}> Stop</button>
</div> </div>
<div style={{ marginTop: "10px" }}> <div className="mt-1 text-sm text-gray-500">{formatTime(currentTime)}</div> */}
<span>{formatTime(currentTime)}</span>
</div>
</div> </div>
); );
}; };

View File

@ -303,12 +303,15 @@ export default function FormAudioDetail() {
const filesData = details?.files || []; const filesData = details?.files || [];
const audioFiles = filesData.filter( const audioFiles = filesData.filter(
(file: any) => (file: any) =>
file.contentType && file.contentType.startsWith("video/webm") file.contentType &&
(file.contentType.startsWith("audio/") ||
file.contentType.includes("webm"))
); );
const fileUrls = audioFiles.map((file: { secondaryUrl: string }) => const fileUrls = audioFiles.map((file: { secondaryUrl: string }) =>
file.secondaryUrl ? file.secondaryUrl : "default-audio.mp3" file.secondaryUrl ? file.secondaryUrl : "default-audio.mp3"
); );
console.log("Audio file URLs:", fileUrls);
setDetailThumb(fileUrls); setDetailThumb(fileUrls);
@ -560,16 +563,13 @@ export default function FormAudioDetail() {
<div className="w-full"> <div className="w-full">
{detailThumb.map((url, index) => ( {detailThumb.map((url, index) => (
<div key={index}> <div key={index}>
{/* <WavesurferPlayer {files.map((file, index) => (
url={url} <AudioPlayer
waveColor="red" key={index}
height={80} urlAudio={file.url}
onReady={(ws: any) => onReady(ws, index)} fileName={file.fileName}
/> />
<Button onClick={() => onPlayPause(index)}> ))}
{isPlayingIndex === index ? "Pause" : "Play"}
</Button> */}
<AudioPlayer urlAudio={url} />
</div> </div>
))} ))}
</div> </div>