通过Ajax获取音频文件
我需要使用 Ajax 获取音频文件并以字节数组的形式接收它。我的后端是 PHP 。
目前我正在使用
<?php
echo file_get_contents("./mainFile.mp3");
?>
这让我以字节数组的形式返回音频文件。但是我如何在 Javascript 中以字节数组的形式接收它。我在SO上提到了这个链接
但这似乎不是正确的方法。有人可以建议一下吗???
I need to fetch an audio file using Ajax and receive it in the form an array of bytes. My backend is on PHP.
Currently I am using
<?php
echo file_get_contents("./mainFile.mp3");
?>
This lets me return the audio file in the form an array of bytes. But how can I receive it in Javascript as a byte array. I referred this link on SO
But this does not seem to be the proper way. Can anyone please suggest something ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
保存名为:raw_audio.php 的文件
现在从 ajax 调用加载该 php 文件。
Save a file named: raw_audio.php
Now load that php file from your ajax call.
您需要将
overrideMimeType()
替换为"text/plain; charset=x-user-defined"
,如下所示:请注意,这是非常徒劳的,JavaScript 中的位操作非常麻烦仅在字符串+charCodeAt 上快速。正如我在我的一个应用程序中令人震惊地发现的那样,甚至比类型化数组还要快。
文本/纯文本并不重要,但字符集很重要。
You need to
overrideMimeType()
to"text/plain; charset=x-user-defined"
like so:Note that this is extremely futile, bit operations in javascript are extremely fast on strings+charCodeAt only. Even faster than on typed arrays, as I shockingly discovered in an application of mine.
The text/plain isn't important, but the charset is.
尝试使用“arraybuffer”响应类型:
Try using the 'arraybuffer' response type:
您要做的是以“二进制”方式通过 ajax 读取文件。我刚刚用谷歌搜索了二进制 ajax,这是出现的第一个结果...不确定它是否有用...
http://nagoon97.wordpress.com/2008/ 04/06/使用ajax读取二进制文件/
What you're looking to do is to read the file via ajax in a "binary" way. I just googled binary ajax and this is the first result that came up... not sure if it's useful...
http://nagoon97.wordpress.com/2008/04/06/reading-binary-files-using-ajax/