簡體   English   中英

通過反應在音頻標簽上設置音量

[英]set volume on audio tag with react

我想用 React 的力量來控制音量,而不是像往常一樣用 JS。

我正在使用 Twilio 流式傳輸音頻和視頻。 我想編寫不同於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} />
        </>
    );

您可以使用useRef代替文檔,如下所示

const audioRef = useRef(); // on the top of your function component

然后使用audioRef訪問該元素。

audioRef.current.volume // use this where you want

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM