1

How can I convert WAV to any compressed audio format? Currently, I'm using this code for recording and storing the audio file in Firebase storage. The size of the file (5 sec) is 1.05 MB, and it takes a lot of time to store it on firebase! In my code I convert to mp3, but I don't mind to convert to any of audio format as log it will be less heavy.

JS CODE:

function startRecoring(){
  navigator.mediaDevices.getUserMedia({ audio: true 
   }).then(function(microphone) {
  // preview camera during recording
  setSrcObject(microphone, document.getElementById('your-video-id'));
  // recording configuration/hints/parameters
  var recordingHints = {
      type: 'audio'
    };
  // initiating the recorder
   recorder = new StereoAudioRecorder(microphone, recordingHints);
  // starting recording here
  recorder.record();
 });
}

function stopRecording(USERNAME){
      // stop recording
      recorder.stop(function() {
          // get recorded blob
          var blob = recorder.blob;
          // generating a random file name
          var fileName = getFileName('mp3',USERNAME);
          // we need to upload "File" --- not "Blob"
          var fileObject = new File([blob], fileName, {
              type: 'audio/mp3'
          });


        //   var storageRef = firebase.storage().ref(USERNAME);
        //   storageRef.put(blob);
        //   nextrecord = true;
        var storageRef = firebase.storage().ref();
        var uploadTask = storageRef.child(USERNAME).put(blob);

       // Register three observers:
      // 1. 'state_changed' observer, called any time the state changes
      // 2. Error observer, called on failure
     // 3. Completion observer, called on successful completion
     uploadTask.on('state_changed', function(snapshot){
    //   Observe state change events such as progress, pause, and resume
    //   Get task progress, including the number of bytes uploaded and the       
    total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
    case firebase.storage.TaskState.PAUSED: // or 'paused'
    console.log('Upload is paused');
    break;
    case firebase.storage.TaskState.RUNNING: // or 'running'
    console.log('Upload is running');
    break;
  }
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: 
 https://firebasestorage.googleapis.com/...
 uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
 console.log('File available at', downloadURL);
      });
   });
 });
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    You'll need an MP3 encoder. Does StereoAudioRecorder not have one? Perhaps [this one](https://github.com/higuma/mp3-lame-encoder-js), or a recorder that has it built in [here](https://github.com/higuma/web-audio-recorder-js) – James Apr 01 '19 at 11:51
  • 1
    this is duplication of the question with good [answer](https://stackoverflow.com/questions/23676396/convert-wav-to-mp3-using-javascript) – Alex Shtromberg Apr 01 '19 at 13:54
  • 3
    Possible duplicate of [convert wav to mp3 using javascript](https://stackoverflow.com/questions/23676396/convert-wav-to-mp3-using-javascript) – SirPeople Apr 01 '19 at 13:58

0 Answers0