我使用 WebRTC,我正确接收流。如果我发送浏览器,一切正常,但是当我尝试使用 AudioContext-createAnalyser 检查频率时。它也继续工作,但我不再控制音频的音量。在这里我留下代码:
function startUserMedia(stream) {
var canvas, ctx, again, fbc_array, bars = 100, bar_x, bar_width, bar_height;
var context = new AudioContext();
var analyser = context.createAnalyser();
source = context.createMediaStreamSource(stream);
source.connect(analyser);
analyser.connect(context.destination);
canvas = document.getElementById("analyser");
ctx = canvas.getContext("2d");
frameLooper();
function frameLooper(){
window.requestAnimationFrame(frameLooper);
fbc_array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(fbc_array);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgb(30, 180, 255)";
for(var i = 0; i < bars; i++){
bar_x = i * 3;
bar_width = 2;
bar_height = -(fbc_array[i] / 2);
ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
}
}
}
提前致谢
编辑:
var connection = new RTCMultiConnection();
connection.socketURL = 'URL...';
connection.socketMessageEvent = 'message';
connection.session = { audio: true, video: false, oneway: true };
connection.mediaConstraints = { audio: true, video: false }
connection.sdpConstraints.mandatory = { OfferToReceiveAudio: false, OfferToReceiveVideo: false };
connection.onstream = function(event){
var mediaElement = event.mediaElement;
mediaElement.muted = true;
mediaElement.volume = 1;
mediaElement.id = event.streamid;
$("#elementHtml").append(mediaElement);
startUserMedia(event.stream);