从JavaScript中的Blob对象下载文件

时间:2018-12-13 13:52:27

标签: javascript getusermedia

我正在将blob转换为文件并将其存储在本地,但是该文件只能在 Media Player Classic 中播放,但没有寻求酒吧播放的音频。在其他播放器中甚至无法播放( vlc,Windows Media Player

RecordAudio = event=>{

 var constraints = { audio: true }

    if(event==='default'){

    navigator.mediaDevices.getUserMedia(constraints).then(stream=> {

      audioContext = new AudioContext();


      gumStream = stream;

      input = audioContext.createMediaStreamSource(stream);


     this.mediaRecorder = new MediaRecorder(stream)
     this.mediaRecorder.start(10);

          var chunk=[];

           this.mediaRecorder.ondataavailable = e => {
            if (e.data && e.data.size > 0) {
             chunk.push(e.data);
           }

           this.setState({chunks:chunk});
          };

      }).catch(function(err) {
            console.log(err);

      });

    }
    else{

      var fileName="blobfile";

     this.mediaRecorder.stop();

     gumStream.getAudioTracks()[0].stop();

     var blob = new Blob(this.state.chunks, {type: 'audio/mp3;codec=opus'});

    var formData=new FormData();

    formData.append("event_name",fileName);
    formData.append("file",new File([blob], fileName, {type: 'audio/mp3; codec=opus', lastModified: Date.now()})); // doesn't matter to put codec again until you change it.
    formData.append("fileExtension", "mp3");
     fetch('http://localhost:6020/uploadFile', {
    method: "POST", // *GET, POST, PUT, DELETE, etc.
    mode: "cors", // no-cors, cors, *same-origin
    cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached

    redirect: "follow", // manual, *follow, error
    referrer: "no-referrer", // no-referrer, *client
    body: formData, // body data type must match "Content-Type" header
    })
    .then(response => response.json())
    .then(function(data){ console.log( JSON.stringify( data ) ) });

     var a = document.createElement("a");
     document.body.appendChild(a);
     a.style = "display: none";

     const audioURL = window.URL.createObjectURL(blob);
     // append videoURL to list of saved videos for rendering
     const audios = this.state.audios.concat([audioURL]);
     this.setState({audios});
     console.log(audios);
     a.href = audioURL;
     a.download = "blobFile";
     a.click();
    window.URL.revokeObjectURL(audioURL);
    }
  }
  else{
   // error
  }

  }

我正在以 else 的状态下载文件并将其发送到服务器。但是,两个文件都不会播放,除非在媒体播放器经典版中仍在MPC中丢失

  • 这里的问题可能是编解码器,但我尝试过pcm也不能很好地工作。

音频文件已附加并在此处提供:recording

0 个答案:

没有答案