请问jQuery的$。阿贾克斯或$ .load允许responseType arrayBuffer? [英] Does jQuery $.ajax or $.load allow for responseType arrayBuffer?

查看:1101
本文介绍了请问jQuery的$。阿贾克斯或$ .load允许responseType arrayBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始接触网络音频API,只是想知道是否有可能使用jQuery的$。阿贾克斯或$ .load功能,使XMLHtt prequest接收音频数据。做$就或$ .load支持responseType = arrayBuffer?

I'm getting started with the Web Audio API and just wondering if it's possible to use jQuery's $.ajax or $.load functions to make the XMLHttpRequest that receives the audio data. Do $.ajax or $.load support responseType=arrayBuffer?

编辑:

好了,这里是我到目前为止有:

Ok, so here's what I have so far:

function loadAudio() {
    $.ajax({
            url: sourceUrl
        }).done(function(response){
            return response;
        })
    }

但我需要返回一个ArrayBuffer。那么,如何将响应成ArrayBuffer?

but I need to return an ArrayBuffer. So how do I convert the response into an ArrayBuffer?

推荐答案

关于你的问题,好像jQuery的不支持它。使用它,因为我下面推荐之前,考虑检查,如果该功能是否可用。

About your question, it seems jQuery does not support it yet. Before using it as I suggested below, consider checking if the feature is available.

通过XHTMLRequest,你可以欺骗你的服务器并接收一个二进制串重新presenting你想从服务器的字节数。它完美地工作。

With XHTMLRequest, you can trick your server and receive a binary string representing the bytes you want from the server. It works perfectly.

var xhr = new XMLHttpRequest();
xhr.open('GET', '/your/audio/file.wav', true);

// Here is the hack
xhr.overrideMimeType('text/plain; charset=x-user-defined');

xhr.onreadystatechange = function(event) {
  if ( this.readyState == 4 && this.status == 200 ) {
    var binaryString = this.responseText;

    for (var i = 0, len = binaryString.length; i < len; ++i) {
      var c = binaryString.charCodeAt(i);
      var byte = c & 0xff; //it gives you the byte at i
      //Do your cool stuff...

    }
  }
};

xhr.send();

它的工作原理,这是常见的... ...但它仍然是一个黑客攻击。

It works, it's common... but... it is still a hack.

使用XHTML要求2级,您可以指定responseType为arraybuffer,并收到ArrayBuffer实际。这是更漂亮。问题是要检查,如果您的浏览器支持此功能。

With XHTML Request Level 2, you can specify the responseType as 'arraybuffer' and receive the ArrayBuffer actually. It is much nicer. The problem is to check if your browser support this feature.

var xhr = new XMLHttpRequest();
xhr.open('GET', '/your/audio/file.wav', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
  if (this.status == 200) {
    //Do your stuff here
  }
};

xhr.send();

希望我帮助。

Hope I helped.

这篇关于请问jQuery的$。阿贾克斯或$ .load允许responseType arrayBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆