Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

record audio from user and save to server

I'm trying to record audio from a website user and save the audio to my server. Many of the posts I have studied so far have referenced Matt Diamond's recorderjs. I attempted to recreate the demo at http://webaudiodemos.appspot.com/AudioRecorder/index.html by opening the source code through my browser. I copied the html, "audiodisplay.js", "recorder.js", and "main.js" and put them on my server. I also added the "recorderWorker.js" file from his GitHub site. In the recorder.js file, I changed var WORKER_PATH = 'js/recorderjs/recorderWorker.js' to var WORKER_PATH = 'recorderWorker.js';

When I run the demo I set up, I'm getting the "would you like to share your microphone.." warning and I can start the recording by pressing the mic icon on the right side. However, when I stop recording, the audio waveform doesn't show up below like in Matt's demo and the save icon doesn't become activated.

If I can get the demo up and running, the next problem I have is saving the wav file to the server instead of locally like in the demo. I've found several posts saying to use XMLHttpRequest(), however I can't really figure out how to connect those examples to recorderjs. Saving WAV File Recorded in Chrome to Server HTML5 & getUserMedia - Record Audio & Save to Web Server after Certain Time RecorderJS uploading recorded blob via AJAX

like image 484
user3080392 Avatar asked Dec 01 '14 02:12

user3080392


People also ask

How do I record and share audio?

Use a voice recording app On your home screen, navigate to the built-in Voice Recorder app. Hit Record, speak your message and send the audio clip to a friend. If you don't want to do this every time, you can also try to use your smartphone's built-in helper.

How do I record my desktop with internal and external audio?

Tap on Screen recorder and open Settings (gear icon) on the floating widget. Go to Audio source and select Internal audio. You can use a third-party screen recorder app from the Google Play Store if your phone doesn't have one built-in.


1 Answers

Using XMLHttpRequest to post wav or mp3 blobs to server is simple.

Just run this code wherever you have access to the blob element:

var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
  if(this.readyState === 4) {
      console.log("Server returned: ",e.target.responseText);
  }
};
var fd=new FormData();
fd.append("audio_data",blob, "filename");
xhr.open("POST","upload.php",true);
xhr.send(fd);

I prefer XMLHttpRequest to $.ajax() because it does not require jQuery.

Server-side, upload.php is as simple as:

$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
$output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea

//move the file from temp name to local folder using $output name
move_uploaded_file($input, $output)

Source: https://addpipe.com/blog/using-recorder-js-to-capture-wav-audio-in-your-html5-web-site/ Live demo: https://addpipe.com/simple-recorderjs-demo/

like image 200
Octavian Naicu Avatar answered Oct 17 '22 08:10

Octavian Naicu