无法从列表中更改所选麦克风ID的流

时间:2018-10-03 11:25:10

标签: audiocontext

我正在使用以下代码来显示麦克风输入可视化器。 我无法从应用程序的下拉列表中更改所选的MIC,仅使用默认值,仅更改默认的MIC流。我调试了代码,并从下拉列表中获取了所选麦克风的更新的设备ID。给出任何错误。

这是我的代码:-                  var Mike = null,analyser = null,javascriptNode = null;

        if (window.stream) {
            window.stream.getTracks().forEach(function(track) {
                track.stop();
                track.ended;
            });
            window.stream=null;
        }
        if (browserName == "Chrome") {
            var audioContraints={
                audio: true,
                video:false,
                deviceId: { exact: inputSelectedDeviceID }  
            };
        }
        else{
            var audioContraints={
                audio: true,
                video:false 
            };
        }

        if (navigator.getUserMedia) {
            navigator.getUserMedia(audioContraints, 
                  function(stream) {
                      if (browserName == "Chrome") {
                          debugger;
                          window.stream = stream;
                      }
                      prevMICId=inputSelectedDeviceID;
                      alert('Got Stream'+inputSelectedDeviceID);
                      audioContext = new AudioContext();
                      //if(audioContext)
                      //    audioContext.close();
                      if (microphone) microphone.disconnect();
                      if (analyser) analyser.disconnect();
                      if (javascriptNode){ javascriptNode.disconnect();
                          javascriptNode.disconnect(audioContext.destination);
                      }
                      // AudioContext = window.AudioContext || window.webkitAudioContext;

                      microphone = audioContext.createMediaStreamSource(window.stream);   
                      // microphone = audioContext.createMediaElementSource(stream);  

                      analyser = audioContext.createAnalyser();
                      analyser.smoothingTimeConstant = 0.8;
                      analyser.fftSize = 1024;

                      javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
                      javascriptNode.connect(audioContext.destination);

                      microphone.connect(analyser);
                      analyser.connect(javascriptNode);
                      javascriptNode.onaudioprocess =null;
                      javascriptNode.onended = function(endded) {
                          console.log("endedd ho gaya" + JSON.stringify(endded));
                      }
                      javascriptNode.onaudioprocess = function(audioProcessingEvent) {
                          console.log(JSON.stringify(audioProcessingEvent));
                          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;
                          //console.log(average   + "  " +values + "  " + length );
                          $( "div[id^='spike']" ).addClass('Rectangle-Inactive').removeClass('Rectangle-21');
                          for (var i = 1; i < average/10; i++) {
                              $( "#spike" +i).addClass('Rectangle-21').removeClass('Rectangle-Inactive'); 
                          }
                      } // end fn stream
                  },
                  function(err) {
                      console.log("The following error occured: " + err.name)
                  });
        }

0 个答案:

没有答案