Skip to content

Commit

Permalink
Merge pull request #300 from l3vels/fix/audio
Browse files Browse the repository at this point in the history
fix: AudioPlayer duration
  • Loading branch information
Chkhikvadze authored Nov 10, 2023
2 parents a2c8f3e + 1a729e8 commit 61aac00
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions apps/ui/src/components/AudioPlayer/AudioPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const AudioPlayer = ({
if (progressBarRef.current === null) return

setCurrentTime(audioRef.current.currentTime)
setDuration(audioRef.current.duration)
// setDuration(audioRef.current.duration)

// Calculate the percentage of audio played
const percentage = (audioRef.current.currentTime / audioRef.current.duration) * 100
const percentage = (audioRef.current.currentTime / duration) * 100
progressBarRef.current.style.background = `linear-gradient(to right, #D6EBFF ${percentage}%, transparent 0)`

if (percentage === 100) {
Expand All @@ -63,15 +63,31 @@ const AudioPlayer = ({
}

useEffect(() => {
if (audioRef.current === null) return

audioRef.current.onloadedmetadata = () => {
if (audioRef.current === null) return

setDuration(audioRef.current.duration)
}
getDuration(audioUrl, function (duration: number) {
setDuration(duration)
})
}, [])

const getDuration = function (url: string, next: any) {
const _player = new Audio(url)
_player.addEventListener(
'durationchange',
function (e) {
if (this.duration != Infinity) {
const duration = this.duration
_player.remove()
next(duration)
}
},
false,
)
_player.load()
_player.currentTime = 24 * 60 * 60 //fake big time
_player.volume = 0
_player.play()
//waiting...
}

return (
<StyledRoot hasClose={onCloseClick ? true : false}>
<StyledButton onClick={togglePlay} className='play-pause-button' type='button'>
Expand Down

0 comments on commit 61aac00

Please sign in to comment.