2017-05-25 210 views
0

我一直在敲打我的頭靠在這個牆上兩天,我真的希望有人能夠幫助這一點。靜音麥克風音頻getUserMedia

我已經從https://higuma.github.io/wav-audio-encoder-js/ + https://github.com/higuma/wav-audio-encoder-js這裏取了一個getUserMedia麥克風錄音機的代碼。我已經剝離了我不需要的組件 - 不知何故,在這個過程中,我已經設法使得生成的文件沒有音頻通過。

它看起來像格式正確 - 但完全沉默。我得到0錯誤工作。

// navigator.getUserMedia shim 
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; 
// URL shim 
window.URL = window.URL || window.webkitURL; 

// audio context + .createScriptProcessor shim 
var audioContext = new AudioContext; 
if (audioContext.createScriptProcessor == null) { 
    audioContext.createScriptProcessor = audioContext.createJavaScriptNode; 
} 

// selectors 
var $microphone = $('#microphone'); 
var $cancel = $('#cancel'); 
var $recordingList = $('#recording-list'); 
var $timeDisplay = $('#time-display'); 
var $microphoneLevel = $('#microphone-level'); 

var microphone = undefined; 
var input = audioContext.createGain(); 
var mixer = audioContext.createGain(); 
var microphoneLevel = audioContext.createGain(); 
microphoneLevel.gain.value = 0; 
microphoneLevel.connect(mixer); 
var processor = undefined; 
var startTime = null; 
var encoder = undefined; 

// obtaining microphone input 
$microphone.click(function() { 
    navigator.getUserMedia({ audio: true }, 
     function(stream) { 
     microphone = audioContext.createMediaStreamSource(stream); 
     microphone.connect(microphoneLevel); 
     console.log(microphone); 
     }, 
     function(error) { 
     window.alert("Could not get audio input"); 
     }); 
}); 

// start/stop recording 
$microphone.click(function() { 
    if (startTime != null) { 
    stopRecording(true); 
    } else { 
    startRecording(); 
    } 
}); 

// cancel recording (without saving) 
$cancel.click(function() { 
    stopRecording(false); 
}); 

// microphone level slider 
$microphoneLevel.on('input', function() { 
    var level = $microphoneLevel[0].valueAsNumber/100; 
    microphoneLevel.gain.value = level * level; 
}); 

function startRecording() { 
    startTime = Date.now(); 
    $microphone.html('Stop'); 
    $cancel.removeClass("hidden"); 
    startRecordingProcess(); 
} 

function startRecordingProcess() { 
    processor = audioContext.createScriptProcessor(1024, 2, 2); 
    input.connect(processor); 
    processor.connect(audioContext.destination); 
    // wav encoder 
    encoder = new WavAudioEncoder(audioContext.sampleRate, 2); 
    processor.onaudioprocess = function(event) { 
    encoder.encode(getBuffers(event)); 
    }; 
} 

function getBuffers(event) { 
    var buffers = []; 
    for (var ch = 0; ch < 2; ++ch) { 
    buffers[ch] = event.inputBuffer.getChannelData(ch); 
    } 
    return buffers; 
} 

function stopRecording(finish) { 
    startTime = null; 
    $timeDisplay.html('00:00'); 
    $microphone.html('<i class="start fa fa-microphone fa-5x" aria-hidden="true"></i>'); 
    $cancel.addClass('hidden'); 
    stopRecordingProcess(finish); 
} 

function stopRecordingProcess(finish) { 
    input.disconnect(); 
    processor.disconnect(); 

    if (finish) { // if microphone pressed 
    saveRecording(encoder.finish()); 
    } else { // if cancel pressed 
    encoder.cancel(); 
    } 
} 

function saveRecording(blob) { 
    var url = URL.createObjectURL(blob); 
    var html = "<p class='recording' recording='" + url + "'><a class='btn btn-default' href='" + url + "' download='recording.wav'>Save Recording</a></p>"; 
    $recordingList.prepend($(html)); 

    // once we have done all the processing, upload the file to beyond verbal 
    // uploadFile(blob); 
} 

// update the recording timer 
function minuteSeconds(n) { return (n < 10 ? "0" : "") + n; } 
function updateDateTime() { 
    if (startTime !== null) { 
    var sec = Math.floor((Date.now() - startTime)/1000); 
    $timeDisplay.html(minuteSeconds(sec/60 | 0) + ":" + minuteSeconds(sec % 60)); 
    } 
} 
window.setInterval(updateDateTime, 200); 

如果有人遇到過,我會非常感激修復。

謝謝大家的時間,並有一個很好的晝/夜

回答

0

首先檢查您用一般recording demo麥克風。
如果它的工作,你可以嘗試只傳遞音頻流&所需的MIME類型媒體錄音機的基本錄音。與microphoneLevel.gain.value = 0; 將其更改爲microphoneLevel.gain.value = 1; //or 2

gain = 0
如果你想用這個webaudio方面發揮,

上午懷疑問題意味着我們音頻靜音。 gain = 1默認音頻電平
gain = 0.1 - 0.9是減少音量級別
gain = above 1.1增加音量

打印在控制檯中的電平值上

// microphone level slider 
$microphoneLevel.on('input', function() { 
    var level = $microphoneLevel[0].valueAsNumber/100; 
    console.log('value: ' + $microphoneLevel[0].valueAsNumber + ' Level: ' + level); 
    microphoneLevel.gain.value = level * level; // if level is zero, then its silent 
    // its better if you have a predefined level values based slider position instead of multiplying it 
}); 

見我demosource