|
| 1 | +import { useMergedRefs } from '@rocket.chat/fuselage-hooks'; |
| 2 | +import React, { useState, useRef, forwardRef } from 'react'; |
| 3 | + |
| 4 | +import { Box, IconButton } from '../..'; |
| 5 | +import { Slider } from '../Slider'; |
| 6 | + |
| 7 | +const getMaskTime = (durationTime: number) => |
| 8 | + new Date(durationTime * 1000) |
| 9 | + .toISOString() |
| 10 | + .slice(durationTime > 60 * 60 ? 11 : 14, 19); |
| 11 | + |
| 12 | +function forceDownload(url: string, fileName?: string) { |
| 13 | + const xhr = new XMLHttpRequest(); |
| 14 | + xhr.open('GET', url, true); |
| 15 | + xhr.responseType = 'blob'; |
| 16 | + xhr.onload = function () { |
| 17 | + const urlCreator = window.URL || window.webkitURL; |
| 18 | + const imageUrl = urlCreator.createObjectURL(this.response); |
| 19 | + const tag = document.createElement('a'); |
| 20 | + tag.href = imageUrl; |
| 21 | + if (fileName) { |
| 22 | + tag.download = fileName; |
| 23 | + } |
| 24 | + document.body.appendChild(tag); |
| 25 | + tag.click(); |
| 26 | + document.body.removeChild(tag); |
| 27 | + }; |
| 28 | + xhr.send(); |
| 29 | +} |
| 30 | + |
| 31 | +export const AudioPlayer = forwardRef< |
| 32 | + HTMLAudioElement, |
| 33 | + { |
| 34 | + src: string; |
| 35 | + maxPlaybackSpeed?: number; |
| 36 | + minPlaybackSpeed?: number; |
| 37 | + playbackSpeedStep?: number; |
| 38 | + } |
| 39 | +>( |
| 40 | + ( |
| 41 | + { |
| 42 | + src, |
| 43 | + maxPlaybackSpeed = 2, |
| 44 | + minPlaybackSpeed = 0.5, |
| 45 | + playbackSpeedStep = 0.5, |
| 46 | + }, |
| 47 | + ref |
| 48 | + ) => { |
| 49 | + const audioRef = useRef<HTMLAudioElement>(null); |
| 50 | + const refs = useMergedRefs(ref, audioRef); |
| 51 | + const [isPlaying, setIsPlaying] = useState(false); |
| 52 | + const [currentTime, setCurrentTime] = useState(0); |
| 53 | + const [durationTime, setDurationTime] = useState(0); |
| 54 | + const [playbackSpeed, setPlaybackSpeed] = useState(1); |
| 55 | + |
| 56 | + const handlePlay = () => { |
| 57 | + const isPlaying = audioRef.current?.paused; |
| 58 | + |
| 59 | + if (isPlaying) { |
| 60 | + audioRef.current?.play(); |
| 61 | + } else { |
| 62 | + audioRef.current?.pause(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const handlePlaybackSpeed = (mod: 1 | -1) => { |
| 67 | + if (audioRef.current) { |
| 68 | + audioRef.current.playbackRate = Math.max( |
| 69 | + Math.min( |
| 70 | + audioRef.current.playbackRate + playbackSpeedStep * mod, |
| 71 | + maxPlaybackSpeed |
| 72 | + ), |
| 73 | + minPlaybackSpeed |
| 74 | + ); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const handleIncreasePlayBackSpeed = () => handlePlaybackSpeed(1); |
| 79 | + |
| 80 | + const handleDecreasePlayBackSpeed = () => handlePlaybackSpeed(-1); |
| 81 | + |
| 82 | + return ( |
| 83 | + <Box |
| 84 | + borderWidth='default' |
| 85 | + borderColor='extra-light' |
| 86 | + p='x16' |
| 87 | + width='fit-content' |
| 88 | + borderRadius='x4' |
| 89 | + > |
| 90 | + <Box display='flex' alignItems='center'> |
| 91 | + <IconButton |
| 92 | + large |
| 93 | + onClick={handlePlay} |
| 94 | + icon={isPlaying ? 'pause-unfilled' : 'play-unfilled'} |
| 95 | + /> |
| 96 | + <Box mi='x12' position='relative'> |
| 97 | + <Slider |
| 98 | + showOutput={false} |
| 99 | + value={currentTime} |
| 100 | + maxValue={durationTime} |
| 101 | + onChange={(value) => { |
| 102 | + if (audioRef.current) { |
| 103 | + audioRef.current.currentTime = value; |
| 104 | + } |
| 105 | + }} |
| 106 | + /> |
| 107 | + <Box |
| 108 | + display='flex' |
| 109 | + alignItems='center' |
| 110 | + justifyContent='space-between' |
| 111 | + color='secondary-info' |
| 112 | + fontScale='micro' |
| 113 | + position='absolute' |
| 114 | + width='100%' |
| 115 | + mb={'neg-x8'} |
| 116 | + > |
| 117 | + {getMaskTime(currentTime)} |
| 118 | + <Box |
| 119 | + fontScale='micro' |
| 120 | + display='flex' |
| 121 | + justifyContent='space-around' |
| 122 | + id='controllers' |
| 123 | + > |
| 124 | + <Box |
| 125 | + mi='x8' |
| 126 | + display='flex' |
| 127 | + alignItems='center' |
| 128 | + justifyContent='space-between' |
| 129 | + > |
| 130 | + <IconButton |
| 131 | + disabled={playbackSpeed <= minPlaybackSpeed} |
| 132 | + icon='h-bar' |
| 133 | + mini |
| 134 | + onClick={handleDecreasePlayBackSpeed} |
| 135 | + /> |
| 136 | + <Box mi='x8'>{playbackSpeed.toFixed(1)}x</Box> |
| 137 | + <IconButton |
| 138 | + disabled={playbackSpeed >= maxPlaybackSpeed} |
| 139 | + icon='plus' |
| 140 | + mini |
| 141 | + onClick={handleIncreasePlayBackSpeed} |
| 142 | + /> |
| 143 | + </Box> |
| 144 | + </Box> |
| 145 | + {getMaskTime(durationTime)} |
| 146 | + </Box> |
| 147 | + </Box> |
| 148 | + <IconButton |
| 149 | + is='a' |
| 150 | + href={src} |
| 151 | + download |
| 152 | + icon='download' |
| 153 | + large |
| 154 | + onClick={(e) => { |
| 155 | + const { host } = new URL(src); |
| 156 | + if (host !== window.location.host) { |
| 157 | + e.preventDefault(); |
| 158 | + forceDownload(src); |
| 159 | + } |
| 160 | + }} |
| 161 | + /> |
| 162 | + </Box> |
| 163 | + <audio |
| 164 | + style={{ display: 'none' }} |
| 165 | + onTimeUpdate={(e) => { |
| 166 | + setCurrentTime((e.target as HTMLAudioElement).currentTime); |
| 167 | + }} |
| 168 | + onLoadedData={(e) => { |
| 169 | + setDurationTime((e.target as HTMLAudioElement).duration); |
| 170 | + }} |
| 171 | + onEnded={() => setIsPlaying(false)} |
| 172 | + ref={refs} |
| 173 | + preload='metadata' |
| 174 | + onRateChange={(e) => { |
| 175 | + setPlaybackSpeed((e.target as HTMLAudioElement).playbackRate); |
| 176 | + }} |
| 177 | + onPlay={() => { |
| 178 | + setIsPlaying(true); |
| 179 | + }} |
| 180 | + onPause={() => { |
| 181 | + setIsPlaying(false); |
| 182 | + }} |
| 183 | + controls |
| 184 | + > |
| 185 | + <source src={src} type='audio/mpeg' /> |
| 186 | + </audio> |
| 187 | + </Box> |
| 188 | + ); |
| 189 | + } |
| 190 | +); |
0 commit comments