简体   繁体   English

WebRTC MediaStream的麦克风活动级别

[英]Microphone activity level of WebRTC MediaStream

I would like some advice on how best to get the microphone activity level of an audio MediaStreamTrack javascript object in Chrome/Canary. 我想了解如何最好地获取Chrome / Canary中音频MediaStreamTrack javascript对象的麦克风活动级别。 The MediaStreamTrack object is an audio track of the MediaStream returned by getUserMedia , as part of the WebRTC javascript API. MediaStreamTrack对象是getUserMedia返回的MediaStream音轨,作为WebRTC javascript API的一部分。

When microphone has audio the green bar up and down very nice: 当麦克风有音频时,绿色条上下非常好:

在此输入图像描述

<script type="text/javascript">
navigator.webkitGetUserMedia({audio:true, video:true}, function(stream){
    // audioContext = new webkitAudioContext(); deprecated  OLD!!
    audioContext = new AudioContext(); // NEW!!
    analyser = audioContext.createAnalyser();
    microphone = audioContext.createMediaStreamSource(stream);
    javascriptNode = audioContext.createJavaScriptNode(2048, 1, 1);

    analyser.smoothingTimeConstant = 0.3;
    analyser.fftSize = 1024;

    microphone.connect(analyser);
    analyser.connect(javascriptNode);
    javascriptNode.connect(audioContext.destination);

    //canvasContext = $("#canvas")[0].getContext("2d");
    canvasContext = document.getElementById("test");
    canvasContext= canvasContext.getContext("2d");

    javascriptNode.onaudioprocess = function() {
        var array =  new Uint8Array(analyser.frequencyBinCount);
        analyser.getByteFrequencyData(array);
        var values = 0;

        var length = array.length;
        for (var i = 0; i < length; i++) {
            values += array[i];
        }

        var average = values / length;
        canvasContext.clearRect(0, 0, 60, 130);
        canvasContext.fillStyle = '#00ff00';
        canvasContext.fillRect(0,130-average,25,130);
    }

}  
);
</script>
<canvas id="test" style="background-color: black;"></canvas>

What you are looking for is webkitAudioContext and its createMediaStreamSource method. 您正在寻找的是webkitAudioContext及其createMediaStreamSource方法。

Here's a code sample that draws green bar to act like a VU meter: 这是一个代码示例,它绘制绿色条,就像VU表一样:

navigator.webkitGetUserMedia({audio:true, video:true}, function(stream){
    audioContext = new webkitAudioContext();
    analyser = audioContext.createAnalyser();
    microphone = audioContext.createMediaStreamSource(stream);
    javascriptNode = audioContext.createJavaScriptNode(2048, 1, 1);

    analyser.smoothingTimeConstant = 0.3;
    analyser.fftSize = 1024;

    microphone.connect(analyser);
    analyser.connect(javascriptNode);
    javascriptNode.connect(audioContext.destination);

    canvasContext = $("#canvas")[0].getContext("2d");

    javascriptNode.onaudioprocess = function() {
        var array =  new Uint8Array(analyser.frequencyBinCount);
        analyser.getByteFrequencyData(array);
        var values = 0;

        var length = array.length;
        for (var i = 0; i < length; i++) {
            values += array[i];
        }

        var average = values / length;
        canvasContext.clearRect(0, 0, 60, 130);
        canvasContext.fillStyle = '#00ff00';
        canvasContext.fillRect(0,130-average,25,130);
    }

}

More details about AudioContext 有关AudioContext的更多详细信息

UPDATE: modified the code using: 更新:修改代码使用:

navigator.mediaDevices.getUserMedia(constraints).then(
    function(stream){
        // code ... 
    }).catch(function(err) {
        // code ... 
});

Here is a fiddle: https://jsfiddle.net/elshnkhll/p07e5vcq/ 这是一个小提琴: https//jsfiddle.net/elshnkhll/p07e5vcq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM