2017-09-25 14 views
2

Friends、Im RecordRTCに続き、私のウェブサイトで音声をキャプチャし、XMLHTTPRequest経由でPHPサーバに記録ファイルをアップロードすることに成功しました。ここでイムアップロード私のコード:Laravelを使用してサーバにSound 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 Frameworkの新機能で、どうすればそれを実現できるかわかりません。親切に私が解決策を見つけるのを助けます。次に、あなたがあなたの目的のディレクトリにファイルを保存することができるようになります

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

: よろしく

+0

アップロードされたファイルのドキュメントとその保存方法を読んだことがありますか? – sisve

+0

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

+0

はい、私はそれを読んでいます。問題は私がgetusermedia()を使ってキャプチャしたBLOBを扱っているところです...問題はディレクトリにアップロードすることです。 –

答えて

0

まず、次のコードを使用してファイルを読み込む必要があります

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

これまでurselfによってこのメカニズムを実装していますか?はいの場合は、デモを私と共有してください! –

+0

私は現在取り組んでいるプロジェクトでそれを実装しましたが、残念ながらまだ提供するライブデモはありません。コードに関する追加情報が必要な場合は、私に知らせてください。 –

関連する問題