使用Laravel在服务器上上载Sound Blob

时间:2017-09-25 13:20:22

标签: javascript php laravel laravel-5.3 recordrtc

朋友,我跟随RecordRTC在我的网站上捕获音频,并成功设法通过XMLHTTPRequest上传PHP服务器上的Recorded文件。我在这里上传我的代码:

if($_POST["EmailCheck"] == "JA" && $_POST["Email"] != ""){
    $betreff = "Ihre Buchungsbestätigung - ".$fern["name"];
    SendePDFBestätigung(PDF_FOLDER. 'teilnehmer/Fernlehrgange/'.$dateiname, $dateiname, $_POST["Email"], $betreff, 'fern');
}
 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.');
	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.');
	console.log('Recorder Initialized');
  }

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

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

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

  }


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

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

                        // file name
        formData.append('audio-filename', fileObject.name);
		
		
		$.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');
									console.log('Successfully Uploaded Recorded Blob');
                                    // file path on server
                                    var fileDownloadURL = '/uploads/' + fileObject.name;

                                
								} else 
								{
                                    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);
    });
  };
这是我的Save.php文件:

 <button onclick="startRecording(this);">record</button>
  <button onclick="stopRecording(this);" disabled>stop</button>

现在我必须在我的Laravel项目中应用这种机制,我是Laravel框架的新手,并且不知道如何实现它。请帮我找到解决方案。 问候

1 个答案:

答案 0 :(得分:0)

视图文件中使用的以下脚本采用麦克风输入并创建blob:

public function store(Request $request) {
    $blobInput = $request->file('audio-blob');

    //save the wav file to 'storage/app/audio' path with fileanme test.wav
    Storage::put('audio/test.wav', file_get_contents($blobInput));
}

因此,您将能够使用Controller中的以下代码存储实际的wav文件:

sqoop import --connect jdbc:oracle:thin:@192.168.0.15:1521:XE --username system --password system --table EMP -m 1 --target-dir /user/sinha