@@ -16,7 +16,9 @@ import {
1616 SvgExitFullscreen ,
1717 SvgPictureInPicture ,
1818 SvgExitPictureInPicture ,
19+ SvgMic ,
1920} from '../svg/player'
21+ import VolumeExtractor from '../use/volumeExtraction'
2022
2123function AudioWave ( props : { stream : MediaStream } ) {
2224 const refWave = useRef < HTMLDivElement > ( null )
@@ -43,6 +45,37 @@ function AudioWave(props: { stream: MediaStream }) {
4345 return < div ref = { refWave } > </ div >
4446}
4547
48+ function Mic ( props : { stream : MediaStream } ) {
49+ const [ volumeValue , setVolumeValue ] = useState ( 0 )
50+ const rest = 100 - volumeValue
51+ useEffect ( ( ) => {
52+ let done = false
53+ if ( props . stream . getAudioTracks ( ) . length ) {
54+ const extractor = new VolumeExtractor ( props . stream )
55+ function updateVolume ( ) {
56+ const value = extractor . calculateVolume ( )
57+ setVolumeValue ( value )
58+ if ( ! done ) requestAnimationFrame ( updateVolume )
59+ }
60+ updateVolume ( )
61+ }
62+ return ( ( ) => {
63+ done = true
64+ } )
65+ } , [ props . stream ] )
66+ return (
67+ < div
68+ className = "absolute top-0 right-0 rounded-xl p-1 m-2 transition-opacity duration-300"
69+ style = { {
70+ background : `linear-gradient(to bottom, white ${ rest } %, red ${ volumeValue } %)` ,
71+ opacity : volumeValue > 3 ? '1' : '0'
72+ } }
73+ >
74+ < SvgMic />
75+ </ div >
76+ )
77+ }
78+
4679export default function Player ( props : { stream : MediaStream , muted : boolean , audio ?: boolean , video ?: boolean , width : string } ) {
4780 const refVideo = useRef < HTMLVideoElement > ( null )
4881 const [ showAudio , setShowAudio ] = useState ( false )
@@ -193,6 +226,7 @@ export default function Player(props: { stream: MediaStream, muted: boolean, aud
193226 ? < AudioWave stream = { props . stream } />
194227 : null
195228 }
229+ < Mic stream = { props . stream } />
196230 < div
197231 className = { `absolute bottom-0 left-0 right-0 rounded-b-xl px-4 py-3 flex justify-between items-center transition-opacity duration-300 ${
198232 showControls ? 'opacity-100' : 'opacity-0 pointer-events-none'
0 commit comments