improved audio opener

This commit is contained in:
Djeeberjr 2021-08-01 13:30:23 +02:00
parent 5a31062b21
commit 7fd4411643

View File

@ -5,35 +5,23 @@ import { FileOpenerProps } from "../types/FileOpenerProps"
const AudioOpener: React.FC<FileOpenerProps> = (props) => { const AudioOpener: React.FC<FileOpenerProps> = (props) => {
let audio: HTMLAudioElement const audio = React.createRef<HTMLAudioElement>()
useEffect(()=>{ useEffect(()=>{
if (audio){ if(audio.current){
audio.pause() audio.current.volume = 0.1
audio.remove()
} }
},[audio])
audio = new Audio(genDownloadLink(props.file.id))
audio.volume = 0.1
},[props.file])
useEffect(()=>{ useEffect(()=>{
if(!props.active){ if (!props.active && audio.current){
audio.pause audio.current.pause()
console.debug("Pause")
} }
console.debug("active",props.active)
},[props.active]) },[props.active])
return ( return (
<div> <div>
<button onClick={()=>{ <audio src={genDownloadLink(props.file.id)} controls ref={audio}></audio>
if (audio){
(audio.paused && audio.play()) || audio.pause()
}
}}>
Play/Pause
</button>
</div> </div>
) )
} }