Friends, Im following RecordRTC to capture audio on my website, and successfully managed to upload Recorded file on PHP server via XMLHTTPRequest. Im uploading here my Code:
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);
});
};
<button onclick="startRecording(this);">record</button>
<button onclick="stopRecording(this);" disabled>stop</button>
<?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';
?>
Now i have to apply this mechanism in my Laravel project,Im new on Laravel Framework and have no clue how can i make it happen. Kindly help me to find a solution. Regards