2015-04-04 48 views
0

嗨我使用這個HTML代碼調用JS記錄語音,然後將其保存到我的服務器。我必須點擊「開始」,然後點擊「停止」錄製,錄製完成後點擊「上傳」。我想讓它更容易。所以用戶必須點擊「開始」和「停止」,然後它會自動在服務器上進行上傳。你能幫我解決這個問題,因爲我是新手,我不知道該怎麼做,謝謝。如何用一個按鈕錄製聲音並自動上傳?

<html> 
 
<body> 
 
    <button onclick="startRecording(this);">Start Recording</button> 
 
    <button onclick="stopRecording(this);" disabled>Stop Recording</button> 
 
    <button onclick="uploadIt(this);" id="uploadBtn" disabled>Upload Recording</button> 
 
    </body> 
 
</html> 
 

HTML調用JS提供記錄和上傳:

<script> 
 
    var audio_context; 
 
    var recorder; 
 
    function startRecording(button) { 
 
    recorder && recorder.clear(); 
 
    recorder && recorder.record(); // start recording 
 
    document.getElementById('uploadBtn').disabled = true; 
 
    button.disabled = true; //dont allow hit record button during recording 
 
    button.nextElementSibling.disabled = false; 
 
    } 
 

 
    function stopRecording(button) {  
 
    recorder && recorder.stop(); 
 
    button.disabled = true; 
 
    button.previousElementSibling.disabled = false; 
 
    
 
    createDownloadLink(); 
 
    document.getElementById('uploadBtn').disabled = false; //allow upload after recording 
 
    } 
 
    
 
    function uploadIt(button) { 
 
    recorder && recorder.exportWAV(function(blob) { // it calls function uploadAudioFromBlob from external file 
 
     uploadAudioFromBlob(blob); 
 
    }); 
 
    }

+0

您是否嘗試過在'stopRecording'中調用'uploadIt'? – holgac 2015-04-04 11:05:57

回答

0

@holgac感謝,我把上傳功能stopRecording功能和它的偉大工程:

function stopRecording(button) {  
 
    recorder && recorder.stop(); 
 
    button.disabled = true; 
 
    button.previousElementSibling.disabled = false; 
 
    
 
    recorder && recorder.exportWAV(function(blob) { 
 
     uploadAudioFromBlob(blob); 
 
    }); 
 
    }