【问题标题】:Sending Audio Blob to server将音频 Blob 发送到服务器
【发布时间】:2016-12-11 12:27:28
【问题描述】:

我正在尝试使用 jQuery 的 $.post 方法将 WebAudio API 和 Recorder.js 生成的音频 blob 发送到我的 Laravel 控制器。这是我正在尝试的。

$('#save_oralessay_question_btn').click(function(e){

      var question_content = $('#question_text_area').val();
      var test_id_fr = parseInt($('#test_id_fr').val());
      var question_type = parseInt($('#question_type').val());
      var rubric_id_fr = $('#rubric_id_fr').val();

      var reader = new FileReader();

      reader.onload = function(event){
           var form_data = new FormData();
           form_data.append('audio_data', event.target.result);

           var result = $.post('../questions',
                 {
                  question_content:question_content, 
                  question_type:question_type,
                  test_id_fr:test_id_fr,
                  rubric_id_fr:rubric_id_fr,

                  audio_data:form_data,
                  processData: false,
                  contentType: false
                },function(data){
                var html = "<div class='row  col-md-4'><div class='col-md-offset-6'><i class='fa fa-check fa-5x' aria-hidden='true'></i></div></div>";

                  swal({   title: "Success!",   text: data,   type: "success",  timer:2000 },function(){
                    //location.reload();
                  });

                })
                .done(function(data){

                }).fail(function(xhr, status, error){
                  sweetAlert("Error!", error, "error");
                });

                };
         reader.readAsDataURL(audio_files[0]); //this contains the blob recorded

            });

这给了

未捕获的类型错误:非法调用

编辑

这就是audio_files 所拥有的。

recorder && recorder.exportWAV(function(blob) {


    audio_files.push(blob);

    var url = URL.createObjectURL(blob);
    var li = document.createElement('li');
    var au = document.createElement('audio');
    var hf = document.createElement('a');

      //console.log(typeof(url));

      au.controls = true;
      au.src = url;
      au.autoplay=true;
      hf.href = url;
      hf.download = new Date().toISOString() + '.wav';
      hf.download = url +'.wav';
      li.appendChild(au);
      li.appendChild(hf);
      recordingslist.appendChild(li);
    });

编辑 2

我尝试了另一种使用 XmlHttpRequest 发送 blob 的方法。

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",audio_files[0]);
fd.append('question_content', question_content);
fd.append('question_type', question_type);
xhr.open("POST","../questions",true);
xhr.send(fd);

控制器 在控制器中,当我可以获取$request["audio_data"] 之类的数据时。但这给出了文件的路径C:\wamp\tmp\phpxxx 请分享如何使用这些数据。我已经看到了这个路径,但是在相应的目录中没有像phpxxx 这样的文件。

【问题讨论】:

  • audio_files 中保存的数据是什么?
  • 感谢罗里抽出宝贵时间。这是我要发送的blob
  • 好的,但是它们是如何生成的?你确定它们是二进制数据吗,否则错误会指示
  • 是的,它们是生成的。在当前文档上录制和播放之前的一切工作正常。

标签: jquery laravel-5.2 blob web-audio-api form-data


【解决方案1】:

您发送data URL 而不是Blob;如果预期结果是POST BlobFile 对象,则不需要FileReader。您可以发送File 对象本身,因为File 继承自Blob

$('#save_oralessay_question_btn').click(function(e) {

  var question_content = $('#question_text_area').val();
  var test_id_fr = parseInt($('#test_id_fr').val());
  var question_type = parseInt($('#question_type').val());
  var rubric_id_fr = $('#rubric_id_fr').val();

  var result = $.post('../questions', {
      question_content: question_content,
      question_type: question_type,
      test_id_fr: test_id_fr,
      rubric_id_fr: rubric_id_fr,    
      audio_data: audio_files[0],
      processData: false,
      contentType: false
    }, function(data) {
      var html = "<div class='row  col-md-4'><div class='col-md-offset-6'><i class='fa fa-check fa-5x' aria-hidden='true'></i></div></div>";

      swal({
        title: "Success!",
        text: data,
        type: "success",
        timer: 2000
      }, function() {
        //location.reload();
      });

    })
    .done(function(data) {

    }).fail(function(xhr, status, error) {
      sweetAlert("Error!", error, "error");
    });

});

【讨论】:

  • 请检查问题的编辑部分。这就是我得到 blob 的方式!
  • @NaumanZafar 将 audio_files[0] 替换为 e.target.files[0],查看更新后的帖子
  • 我已经检查过了。但它不起作用。你是对的,它应该可以工作,但我无法找出这种行为的原因
  • “但它不起作用。” 你能描述一下“不起作用”吗?尝试在不包含其他数据的情况下发送Blob,在php 使用file_get_contents("php://input"),另请参阅stackoverflow.com/questions/38676275/…stackoverflow.com/questions/38236047/…
  • 我检查了另一种发送BLOB 的方式。请检查更新的问题(编辑 2)。如果您有什么要评论的。
【解决方案2】:

要通过 request["audio_data"] 访问音频,只需将 .read() 添加到 end 并将其存储在变量中

x=request["audio_data"].read()

【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2020-01-07
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多