javascript WebRTC:是否可以控制麦克风和音量级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16942773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebRTC: Is it possible to control the microphone and volume levels
提问by liteshade06
I am working on a demo site which includes a slide-out widget that allows a user to place a call.
我正在开发一个演示站点,其中包含一个允许用户拨打电话的滑出式小部件。
I am using the SIPml5 tool along with the webrtc2sip back end for handling the call. That part is all set up and properly working. So now I am looking at seeing if I can control the microphone and volume levels using sliders in the widget. Is this even possible? I look everywhere online and haven't had much luck.
我正在使用 SIPml5 工具和 webrtc2sip 后端来处理呼叫。该部分已全部设置并正常工作。所以现在我正在考虑是否可以使用小部件中的滑块来控制麦克风和音量级别。这甚至可能吗?我在网上到处找,但运气不佳。
I did find a couple sites that showed me how I can control the volume of the audio tag within the jQuery slider code. So I tried setting it up like the code below:
我确实找到了几个网站,它们向我展示了如何在 jQuery 滑块代码中控制音频标签的音量。所以我试着像下面的代码一样设置它:
$(function() {
$( "#slider-spkr" ).slider({
orientation: "vertical",
range: "min",
min: 0,
max: 100,
value: 60,
slide: function( event, ui ) {
var value = $("#slider-spkr").slider("value");
document.getElementById("audio_remote").volume = (value / 100);
},
change: function() {
var value = $("#slider-spkr").slider("value");
document.getElementById("audio_remote").volume = (value / 100);
}
});
});
Unfortunately, this isn't working either. So I'm not sure if I am allowed to do this when using SIPml5, or if my jQuery code needs adjusted.
不幸的是,这也不起作用。所以我不确定在使用 SIPml5 时是否允许我这样做,或者我的 jQuery 代码是否需要调整。
Has anyone else had any luck with adding microphone/volume controls? Thanks for your help.
有没有其他人在添加麦克风/音量控制方面有任何运气?谢谢你的帮助。
回答by icanhazbroccoli
Afaik it's impossible to adjust microphone volume. But you can switch it on/off by using stream api:
Afaik 无法调节麦克风音量。但是您可以使用流 api 打开/关闭它:
function toggleMic(stream) { // stream is your local WebRTC stream
var audioTracks = stream.getAudioTracks();
for (var i = 0, l = audioTracks.length; i < l; i++) {
audioTracks[i].enabled = !audioTracks[i].enabled;
}
}
This code is for native webrtc api, not sipML5. It seems they haven't implemented it yet. Here isnot so clear receipt for it.
此代码用于本机 webrtc api,而不是 sipML5。他们似乎还没有实施。这里不是很清楚的收据。
回答by Kirill Slatin
Well it is possible, but currently only in Chrome and with some assumptions. I am not the auther, you can find inspiration for this code in this open-source library (SimpleWebRtc).
嗯,这是可能的,但目前仅在 Chrome 中并有一些假设。我不是作者,你可以在这个开源库 ( SimpleWebRtc) 中找到这段代码的灵感。
navigator.webkitGetUserMedia(constraints,
function(webRTCStream){
var context = new window.AudioContext();
var microphone = context.createMediaStreamSource(webRTCStream);
var gainFilter = context.createGain();
var destination = context.createMediaStreamDestination();
var outputStream = destination.stream;
microphone.connect(gainFilter);
gainFilter.connect(destination);
var filteredTrack = outputStream.getAudioTracks()[0];
webRTCStream.addTrack(filteredTrack);
var originalTrack = webRTCStream.getAudioTracks()[0];
webRTCStream.removeTrack(originalTrack);
},
function(err) {
console.log("The following error occured: " + err);
}
);
The trick is to modify the stream and then replace the audio track of current stream with audio track of modified stream (taken from MediaStreamDestination stream).
诀窍是修改流,然后用修改后的流的音轨(取自 MediaStreamDestination 流)替换当前流的音轨。
DISCLAIMER:
免责声明:
This doesn't work in FireFox as of version 35, since they merely didn't implement MediaStream.addTrack/removeTrack. I use this check currently
从版本 35 开始,这在 FireFox 中不起作用,因为它们只是没有实现 MediaStream.addTrack/removeTrack。我目前使用此支票
this.micVolumeIsSupported = function() {
var MediaStream = window.webkitMediaStream || window.MediaStream;
return !!MediaStream.prototype.addTrack && !!MediaStream.prototype.removeTrack;
};
_gainSupported = this.micVolumeIsSupported();
This has a limitation in Chrome due to a bugwith stopping stream with mixed up tracks. You might wish to restore these tracks before closing connection or on connection interruption;
这在 Chrome 中存在一个限制,因为它存在一个错误,即停止带有混合曲目的流。您可能希望在关闭连接或连接中断之前恢复这些轨道;
this.restoreTracks = function(){
if(_gainSupported && _tracksSubstituted){
webRTCStream.addTrack(originalTrack);
webRTCStream.removeTrack(filteredTrack);
_tracksSubstituted = false;
}
};
This works for me
这对我有用