此问题并不是100%出现。没想到国外大神已经有处理此问题的经验

原贴地址: https://stackoverflow.com/questions/10365335/decodeaudiodata-returning-a-null-error

看了大神的解决办法,受到了启发,

此错误大概就是XMLHttpRequest请求回来的数据缺少必要的数据头,导致.decodeAudioData  无法成功解析音乐文件。

根据自己当前的情况,我将音乐长度延长到1s+(之前不足0.2s),使用Adobe Audition重新输出后,问题再没有复现。

如果你的音乐是动态合成或者自己无法控制的,可以按国外大神的办法尝试处理(项目时间紧张,目前仅先记录到bolg 并没有测试他的代码)

为了避免原贴失效或无法访问,这里直接搬一下

The real reason is that both createBuffer and decodeAudioData right now have a Bug and throw weird vague DOM exception 12 for files they should normally play. But we should be aware that this is new and evolving technology and be thankful even for web audio api as it is now since its small miracle that happened to us.

They are missing stream syncing on header boundary that any reasonable decoder of streaming audio format should start with. And mp3 or many aac/adts files are streaming fileformats. streaming means that you can cut them anywhere or insert append anything (various tags even image artwork) decoder shouldnt care about unknown data. decoder should just seek until he finds header he knows and can decode.

I thrown together this temporary solution that seeks to nearest frame header start and passes data from this offset only.

mp3 or mp2 all start header for every audio frame (every around 200bytes) with 0XFFE and aac(adts) on oxFFF syncword that is there just for this reason. therefore both will sync on 0xFFE. Here is the code I currently use to play previously not played files.

What I hate is that arrayBuffer doesnt have subarray() like its typed childs to return just different view from different offset instead of whole new array copy that slice() returns. if only webaudio api accepted typedarrays as input but unfortunately the only way to create arraybuffer back seems huge slice() copy. thankfully usually only one or two seeks are needed.

Forcing Web Audio Api to not being Picky about files

node={};
node.url='usual_mp3_with_tags_or_album_artwork.mp3'; function syncStream(node){ // should be done by api itself. and hopefully will.
var buf8 = new Uint8Array(node.buf);
buf8.indexOf = Array.prototype.indexOf;
var i=node.sync, b=buf8;
while(1) {
node.retry++;
i=b.indexOf(0xFF,i); if(i==-1 || (b[i+1] & 0xE0 == 0xE0 )) break;
i++;
}
if(i!=-1) {
var tmp=node.buf.slice(i); //carefull there it returns copy
delete(node.buf); node.buf=null;
node.buf=tmp;
node.sync=i;
return true;
}
return false;
} function decode(node) {
try{
context.decodeAudioData(node.buf,
function(decoded){
node.source = context.createBufferSource();
node.source.connect(context.destination);
node.source.buffer=decoded;
node.source.noteOn(0);
},
function(){ // only on error attempt to sync on frame boundary
if(syncStream(node)) decode(node);
});
} catch(e) {
log('decode exception',e.message);
}
} function playSound(node) {
node.xhr = new XMLHttpRequest();
node.xhr.onload=function(){
node.buf=node.xhr.response;
node.sync=0;
node.retry=0;
decode(node);
}
node.xhr.open("GET", node.url, true);
node.xhr.responseType = "arraybuffer";
node.xhr.send();
}

  

audioContext.decodeAudioData 返回null 错误的更多相关文章

  1. fopen函数打开文件总是返回NULL错误

    有时候,调用fopen函数用来打开文件,但是总会返回NULL.对于此类问题.一定是一下两种原因之一造成的. 1.路径错误.(路径中斜杠和反斜杠的问题) 2.文件在另一个进程中被打开,再次打开当然不行( ...

  2. json_decode返回NULL

    最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...

  3. Atitit. null错误的设计 使用Optional来处理null

    Atitit. null错误的设计 使用Optional来处理null 然后,我们再看看null还会引入什么问题. 看看下面这个代码: String address = person.getCount ...

  4. findViewById返回null

    Q:findViewById返回null? A: 代码逻辑错误: 最终,发现错误竟然是在layout文件中把android:id写成了android:name. android:name=" ...

  5. C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍

    一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...

  6. 一个开发原则:永远不要返回NULL

    看一篇文章:10个经典的java开发原则,里面一个原则:永远不要返回NULL. 说实在的,我对这个原则体会不是很深,平时在使用对象前,检查是否为null已经成了习惯,也是我要求开发人员的一个标准动作. ...

  7. curl post请求总是返回417错误

    在进行post请求的时候, curl总是返回417错误 在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步. 发送一个 ...

  8. new会返回NULL空指针吗

    c++中的new会返回NULL空指针吗 https://stackoverflow.com/questions/3389420/will-new-operator-return-null On a s ...

  9. fopen() 返回 NULL, 奇葩原因:当前进程打开多个句柄,忘记关闭。(bug)

    今天在测试一个程序的时候,突然第一次弹出错误对话框,提示: 创建新文件失败. fopen() 返回 NULL 我又重启程序测试,还是提示同样的错误. 经过几分钟的检查,发现一个规律:当处理到500多个 ...

随机推荐

  1. 56)PHP,模型类的设计思想

    一张表对应一个模型类-----Mode

  2. 微信中的APP、公众号、小程序的openid及unionid介绍

    微信中的APP.公众号.小程序的openid及unionid介绍 1.unionid 如果开发者拥有多个移动应用.网站应用.和公众帐号(包括小程序),可通过 UnionID 来区分用户的唯一性,因为只 ...

  3. 关于Apache Commons-Lang3的使用

    在日常工作中,我们经常要使用到一些开源工具包,比如String,Date等等.有时候我们并不清楚有这些工具类的存在,造成在开发过程中重新实现导致时间浪费,且开发的代码质量不佳.而apache其实已经提 ...

  4. Linux 设置开机启动项的几种方法

    方法一:编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本. 所以我们可以直接在/etc/rc.local中添加启动脚本. $ vim /etc/rc.loca ...

  5. RadixSort(基数排序)原理及C++代码实现

    基数排序是一种思想很值得学习的排序方法. 它突破了正常的排序思维:先排高位,如果高位相同再排次高位,直至最低.它的思想是利用稳定排序从低位开始排,最后再排最高位. 另外它用来划分的位不一定是一位一位的 ...

  6. 安装与使用django-restframework

    django-restframework 一.安装与使用 1.安装 >: pip3 install djangorestframework 2.使用 在settings.py中注册: INSTA ...

  7. SAGE|DNA微阵列|RNA-seq|lncRNA|scripture|tophat|cufflinks|NONCODE|MA|LOWESS|qualitile归一化|permutation test|SAM|FDR|The Bonferroni|Tukey's|BH|FWER|Holm's step-down|q-value|

    生物信息学-基因表达分析 为了丰富中心法则,研究人员使用不断更新的技术研究lncRNA的方方面面,其中技术主要是生物学上的微阵列芯片技术和表达数据分析方法,方方面面是指lncRNA的位置特征. Bac ...

  8. <JZOJ5944>信标

    emmm树形dp?好像是的 搬一个题解证明过来 由于在n>1时答案至少为1,我们枚举一个必须放的根, 所有深度不同的点就被区分开了. 设一个节点有c个儿子, 发现必须在其中至少c−1个儿子的子树 ...

  9. js实现数组去重怎么实现?

    方法1. 创建一个新的临时数组来保存数组中已有的元素 var a = new Array(1,2,2,2,2,5,3,2,9,5,6,3); Array.prototype.unique1 = fun ...

  10. 正则提取关键字符-python代码实现

    原文地址:http://www.bugingcode.com/blog/python_re_extraction_key.html 关于python的正则使用在以前的文章中 http://www.bu ...