AirenSoft / OvenLiveKit-Web

OvenLiveKit for Web is a JavaScript-based Live Streaming Encoder
MIT License
47 stars 22 forks source link

How I can enable/disable the microphone/camera without restarting the stream? #7

Open kolik29 opened 2 years ago

kolik29 commented 2 years ago

Hello! In example https://demo.ovenplayer.com/demo_input.html I can change audio settings and select "Without audio", but then streaming stopped. I can disable the microphone without stopping the stream?

bchah commented 2 years ago

I don't think the demo page does it, but here is what I do in my app. After calling OvenLiveKit.getUserMedia(constraints), you can enable / disable the video or audio track like so:

                let videoTrack = userMedia.getVideoTracks()[0];
                let audioTrack = userMedia.getAudioTracks()[0];

                $("#muteOwnVideo, #muteOwnAudio").off("click");

                $("#muteOwnVideo").on("click", () => {
                    let enabled = videoTrack.enabled;
                    videoTrack.enabled = !enabled;
                    let src = enabled ? "./img/icons/camera_off.svg" : "./img/icons/camera_on.svg";
                    $("#muteOwnVideo").attr("src", src);
                });

                $("#muteOwnAudio").on("click", () => {
                    let enabled = audioTrack.enabled;
                    audioTrack.enabled = !enabled;
                    let src = enabled ? "./img/icons/mic_muted.svg" : "./img/icons/mic_unmuted.svg";
                    $("#muteOwnAudio").attr("src", src);
                });

Obviously some of this is specific to my app but if you are using JS on the front end feel free to use this as a guiding point.