网址谷歌浏览器的移动:从数据中播放音频 [英] Playing audio from data:url google chrome mobile
问题描述
我用这个code,以生成字节音频元素,它创建一个简单的WAV声音,我能够发挥在谷歌浏览器或桌面的Firefox,但如果我要玩,在谷歌Chrome移动设备,它起到什么,是这个问题,我能播放wav文件伊斯利在那里,但我不能够在谷歌浏览器手机播放音乐的二进制数据:
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<标题>防爆发电机和LT; /标题>
<脚本类型=文/ JavaScript的SRC =../../ JS / jQuery的-1.8.3.js>< / SCRIPT>
<脚本> 功能连接codeAudio16bit(数据,采样率){
变种N = data.length;
VAR整数= 0,I; // 16位单声道WAVE头模板
VAR标题=RIFF< ##> WAVEfmt \\ X10 \\ X00 \\ X00 \\ X00 \\ X01 \\ X00 \\ X01 \\ X00< ##>< ##> \\ X02 \\ X00 \\ X10 \\ x00data< ##&GT ;; //助手插入一个32位的小端的INT。
功能insertLong(值){
VAR字节=;
对于(I = 0; I&4; ++ⅰ){
字节+ = String.fromChar code(价值256%);
值= Math.floor(价值/ 256);
}
标题= header.replace('< ##>',字节);
} // CHUNKSIZE
insertLong(36 + N * 2); // 采样率
insertLong(采样率); // ByteRate
insertLong(采样率* 2); // Subchunk2Size
insertLong(N * 2); //输出声音数据
对于(VAR I = 0; I< N ++我){
变种样本= Math.round(Math.min(1,Math.max(-1,数据由[i]))* 32767);
如果(样品℃,){
样品+ = 65536; //补签署
}
头+ = String.fromChar code(样品256%);
头+ = String.fromChar code(Math.floor(样品/ 256));
} 回报的数据:音频/ WAV; BASE64,'+ BTOA(头);
}
功能generateTone(频率,采样率,持续时间){
VAR音= []
对于(VAR I = 0; I<期限*采样率; ++ I){
tone.push(Math.sin(2 * Math.PI *的i /(采样率/频率)));
}
返回音;
} $(文件)。就绪(函数(){
$(#机构)HTML(听起来......); VAR采样率= 44100; //赫兹
VAR频率= 400; //赫兹
无功时间= 2; //秒
VAR音= generateTone(频率,采样率,持续时间);
VAR base64EnodedTone = EN codeAudio16bit(音色,采样率);
VAR audioPlayer = $('<音频>')ATTR({SRC:base64EnodedTone。
控制:真
});
$(#机构)追加(audioPlayer)。 $(#机构)附加(&所述p为H.;完成&所述; P>中);
});
< / SCRIPT>
< /头> <身体GT;
< DIV ID =身体与GT;< / DIV>
< /身体GT;
< / HTML>
这肯定是一个错误。任何时候,你有工作在桌面上,也未能在Chrome浏览器(Android版),它很可能99.5%是一个错误。
我在这里提出的bug: https://开头code.google.com / p /铬/问题/细节ID = 253466 与错误的演示在这里:的 http://jsbin.com/ajocid/latest
有在桌面上其他解决方案EN code中的音频转换成一个Blob和玩,不幸的是,这并不为Android工作,要么在Chrome和需要修复。
目前可以希望,直到两个bug被修复最好是这上传到服务器,并从那里玩。
I use this code to generate a audio element from byte , it create a simple wav sound , I am able to play that in google chrome or firefox of DESKTOP , but when I want to play that in Google Chrome Mobile , it play nothing , where is the problem , I am able to play wav file easly but I am not able to play music from binary data in google chrome mobile :
<!DOCTYPE html>
<html>
<head>
<title>Explosion Generator</title>
<script type = "text/javascript" src="../../js/jquery-1.8.3.js" ></script>
<script>
function encodeAudio16bit(data, sampleRate) {
var n = data.length;
var integer = 0, i;
// 16-bit mono WAVE header template
var header = "RIFF<##>WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00<##><##>\x02\x00\x10\x00data<##>";
// Helper to insert a 32-bit little endian int.
function insertLong(value) {
var bytes = "";
for (i = 0; i < 4; ++i) {
bytes += String.fromCharCode(value % 256);
value = Math.floor(value / 256);
}
header = header.replace('<##>', bytes);
}
// ChunkSize
insertLong(36 + n * 2);
// SampleRate
insertLong(sampleRate);
// ByteRate
insertLong(sampleRate * 2);
// Subchunk2Size
insertLong(n * 2);
// Output sound data
for (var i = 0; i < n; ++i) {
var sample = Math.round(Math.min(1, Math.max(-1, data[i])) * 32767);
if (sample < 0) {
sample += 65536; // 2's complement signed
}
header += String.fromCharCode(sample % 256);
header += String.fromCharCode(Math.floor(sample / 256));
}
return 'data:audio/wav;base64,' + btoa(header);
}
function generateTone(freq, sampleRate, duration){
var tone = []
for(var i = 0; i < duration * sampleRate; ++i){
tone.push(Math.sin(2 * Math.PI * i / (sampleRate/freq)));
}
return tone;
}
$(document).ready(function(){
$("#body").html("Sounds...");
var sampleRate = 44100; // hz
var freq = 400; // hz
var duration = 2; // seconds
var tone = generateTone(freq, sampleRate, duration);
var base64EnodedTone = encodeAudio16bit(tone, sampleRate);
var audioPlayer = $('<audio>').attr({ src: base64EnodedTone,
controls:true
});
$("#body").append(audioPlayer);
$("#body").append("<p>done<p>");
});
</script>
</head>
<body>
<div id="body" ></div>
</body>
</html>
This is most certainly a bug. Any time that you have it working on desktop and it fails to work on Chrome for Android that it is 99.5% likely to be a bug.
I have raised the bug here: https://code.google.com/p/chromium/issues/detail?id=253466 with a demo of the error here: http://jsbin.com/ajocid/latest
There is another solution on the desktop to encode the audio into a Blob and play that, unfortunately that does not work either on Chrome for Android and needs to be fixed.
The best you can currently hope for until the two bugs are fixed is to upload this to a server and play it from there.
这篇关于网址谷歌浏览器的移动:从数据中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!