如何将WAV转换为任何压缩的音频格式-Javascript

时间:2019-04-01 11:34:51

标签: javascript

如何将WAV转换为任何压缩的音频格式? 目前,我正在使用此代码在Firebase存储器中记录和存储音频文件。文件大小(5秒)为1.05 MB,将其存储在Firebase上花费大量时间! 在我的代码中,我将转换为mp3,但我不介意将其转换为任何音频格式,因为日志将不会那么繁重。

JS代码:

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);
      });
   });
 });
}

0 个答案:

没有答案