[英]set volume on audio tag with react
I want to control the audio volume with the power of React and not as usual with JS.我想用 React 的力量来控制音量,而不是像往常一样用 JS。
I am using Twilio for streaming audio and video.我正在使用 Twilio 流式传输音频和视频。 I want to write the code different than the
document.getElementById('audio');
我想编写不同于
document.getElementById('audio');
的代码
useEffect(() => {
const audioTrack = audioTracks[0];
const audio = document.getElementById('audio');
audio.volume = (guide.liveDetails.volume / 100);
if (audioTrack && mic) {
audioTrack.attach(audioRef.current);
return () => {
audioTrack.detach();
};
}
}, [audioTracks, mic, guide]);
return (
<>
{camera && <video ref={videoRef} autoPlay={true} />}
<audio id="audio" ref={audioRef} autoPlay={true} muted={!mic} />
</>
);
You can use useRef
instead of document as shown below您可以使用
useRef
代替文档,如下所示
const audioRef = useRef(); // on the top of your function component
and then use audioRef
to access the element.然后使用
audioRef
访问该元素。
audioRef.current.volume // use this where you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.