2017-09-25 91 views
2

朋友,我跟随RecordRTC捕获我网站上的音频,并成功地通过XMLHTTPRequest上传记录在PHP服务器上的文件。林载这里我的代码:在服务器上使用Laravel上传声音Blob

var audio_context; 
 
    var recorder; 
 
    var isMouseDown = false; 
 
    var fileType = 'audio'; 
 
    var fileName = 'ABCDEF.wav'; 
 

 
    function startUserMedia(stream) { 
 
    var input = audio_context.createMediaStreamSource(stream); 
 
    __log('Media stream created.'); 
 
\t console.log('Media stream created.'); 
 

 
    // Uncomment if you want the audio to feedback directly 
 
    //input.connect(audio_context.destination); 
 
    //__log('Input connected to audio context destination.'); 
 

 
    recorder = new Recorder(input); 
 
    __log('Recorder initialised.'); 
 
\t console.log('Recorder Initialized'); 
 
    } 
 

 
    function startRecording(button) 
 
    { 
 
    recorder && recorder.record(); 
 
    button.disabled = true; 
 
    button.nextElementSibling.disabled = false; 
 
    __log('Recording...'); 
 
\t console.log('Recording....'); 
 
    } 
 

 
    function stopRecording(button) 
 
    { 
 
    recorder && recorder.stop(); 
 
    button.disabled = true; 
 
    button.previousElementSibling.disabled = false; 
 
    __log('Stopped recording.'); 
 
\t console.log('Stopped Recording'); 
 
\t 
 

 
    // create WAV download slink using audio data blob 
 
    createDownloadLink(); 
 
    recorder.clear();  
 

 
    } 
 

 

 
    function createDownloadLink() 
 
    { 
 
    recorder && recorder.exportWAV(function(blob) 
 
\t { 
 
\t \t var counter = 0; 
 
\t \t 
 
\t \t var url = URL.createObjectURL(blob); 
 
     
 
\t \t var fileName = 'Recording'+counter+'.wav'; 
 
\t \t 
 
\t \t var fileObject = new File([blob], fileName, { 
 
          type: 'audio/wav' 
 
         }); 
 
\t \t \t \t \t \t 
 
\t \t var formData = new FormData(); 
 

 
         // recorded data 
 
\t \t formData.append('audio-blob', fileObject); 
 

 
         // file name 
 
     formData.append('audio-filename', fileObject.name); 
 
\t \t 
 
\t \t 
 
\t \t $.ajax({ 
 
          url: 'save.php', // replace with your own server URL 
 
          data: formData, 
 
          cache: false, 
 
          contentType: false, 
 
          processData: false, 
 
          type: 'POST', 
 
          success: function(response) { 
 
           if (response === 'success') { 
 
            alert('successfully uploaded recorded blob'); 
 
\t \t \t \t \t \t \t \t \t console.log('Successfully Uploaded Recorded Blob'); 
 
            // file path on server 
 
            var fileDownloadURL = '/uploads/' + fileObject.name; 
 

 
           
 
\t \t \t \t \t \t \t \t } else 
 
\t \t \t \t \t \t \t \t { 
 
            alert(response); // error/failure 
 
           } 
 
          } 
 
         }); 
 

 
    }); 
 

 
    } 
 

 

 
    window.onload = function init() { 
 
    try { 
 
     // webkit shim 
 
     window.AudioContext = window.AudioContext || window.webkitAudioContext; 
 
     navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; 
 
     window.URL = window.URL || window.webkitURL; 
 

 
     audio_context = new AudioContext; 
 
     __log('Audio context set up.'); 
 
     __log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!')); 
 
    } catch (e) { 
 
     alert('No web audio support in this browser!'); 
 
    } 
 

 
    navigator.getUserMedia({audio: true}, startUserMedia, function(e) { 
 
     __log('No live audio input: ' + e); 
 
    }); 
 
    };
<button onclick="startRecording(this);">record</button> 
 
    <button onclick="stopRecording(this);" disabled>stop</button>
,这是我Save.php文件:

<?php 
 
// upload directory 
 
$filePath = 'uploads/' . $_POST['audio-filename']; 
 

 
// path to ~/tmp directory 
 
$tempName = $_FILES['audio-blob']['tmp_name']; 
 

 
// move file from ~/tmp to "uploads" directory 
 
if (!move_uploaded_file($tempName, $filePath)) { 
 
    // failure report 
 
    echo 'Problem saving file: '.$tempName; 
 
    die(); 
 
} 
 

 
// success report 
 
echo 'success'; 
 
?>

现在,我在我的Laravel项目采取这种方式,即时通讯新的Laravel框架,并没有线索我怎么能让它发生。请帮助我找到解决方案。 问候

+0

您是否阅读过关于上传文件的文档以及如何存储它们? – sisve

+0

https://laravel.com/docs/5.5/filesystem#file-uploads – mimo

+0

是的,我读过。问题在这里即时处理我使用getusermedia()捕获的blob ...问题是上传到目录中。 –

回答

0

首先,你需要使用下面的代码来读取文件:

$blobInput = $request->file('audio-blob');

然后,您就可以将文件存储到你想要的目录:

Storage::put('here specify your path followed by your filename separated by /', file_get_contents($blobInput)); 
+0

你有没有实现过这个机制?如果是,那么请与我分享一些演示! –

+0

我已经在我目前正在开发的一个项目中实现它,但不幸的是我没有现场演示。如果您需要关于代码的其他信息,请告诉我。 –

相关问题