2015-04-16 53 views
-2

嗨我有這個源代碼來在瀏覽器中錄製音頻。 Record.js調用另一個腳本來提供錄製音頻並將其保存到服務器。一鍵運行更多功能JavaScript(在瀏覽器中錄製音頻)

的index.html

<button type="submit" onclick="toggleRecording()" data-run="0"></button> 

record.js

//starts by click on button 
    function toggleRecording() { 
    var run = parseInt(getAttribute('data-run')); // 

     if(run === 1) { 
     recorder && recorder.stop(); 
     recorder && recorder.exportWAV(function(blob) { 
     uploadAudioFromBlob(blob); 
     }); 
     __log('Recording is stopped.'); 
     button.setAttribute('data-run', 0); 

    } 

    else { 
     recorder && recorder.clear(); 
     recorder && recorder.record(); 
     __log('Speak...'); 
     button.setAttribute('data-run', 1); 
    } 
    } 

    function __log(e, data) {  
showInfo("\n" + e + " " + (data || '')); 
    } 

    var audio_context; 
    var recorder; 
    function startUserMedia(stream) { 
    var input = audio_context.createMediaStreamSource(stream); 
    recorder = new Recorder(input); 
    __log('Systém for recording is available.'); 
    } 

    function startRecording(button) { 
    recorder && recorder.clear(); 
    recorder && recorder.record(); 
    button.nextElementSibling.disabled = false; 
    __log('Talk...'); 
    } 

    window.onload=function init() { 
    try { 
     window.AudioContext = window.AudioContext || window.webkitAudioContext; 
     navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; 
     window.URL = window.URL || window.webkitURL;  
     audio_context = new AudioContext;  
    } catch (e) { 
     alert('This browser do not support audio!'); 
    }  
    navigator.getUserMedia({audio: true}, startUserMedia, function(e) { 
     __log('No audio was detected: ' + e); 
    }); 
    }; 

現在這個記錄系統在這個步驟工作:

  1. function init()個運行immediatelly當頁面被加載和用戶之後允許麥克風在消息startusermedia功能 2點擊按鈕之後運行運行toggleRecording(button)功能,其開始記錄音頻
  2. 按鈕第二次點擊運行toggleRecording功能,其停止記錄音頻

如果可能,我想在這個步驟中使用這個系統: 1.首先點擊按鈕運行函數「init」和「startusermedia」和「togglerecording」,這樣記錄將在點擊 後立即開始2.點擊將調用「toggleRecording」功能停止錄音

本主題是有關我剛纔的問題:Run 2 functions with one button Javascript

回答

0

如果我正確地得到你的問題,你只需要像這樣:

var recording = false; 
var recordButton = document.getElementById('recordButton'); 
function startRecording() { 
    if(!recording) 
    { 
     init(); 
     startusermedia(recordButton); 
    } 

    toggleRecording(recordButton); 
    recording = !recording; 

} 

而且按鈕:

<button type="submit" onclick="startRecording();" data-run="0" id="recordButton"></button> 
+0

檢查編輯變化。 – Carorus

+0

謝謝。但在控制檯中有一個錯誤:「Uncaught TypeError:無法讀取屬性'getAttribute'null ......」(腳本中的第25行) – Nikolaus

+0

我剛剛在此行中看到一次調用getAttribute 'var run = parseInt的getAttribute( '數據運行')); //' 哪些BTW應該修復爲 'var run = parseInt(button.getAttribute('data-run')); /'/ – Carorus

相關問題