小皮2017-04-10 15:49:021楼

调相关的 API 即可:http://apistore.baidu.com/apiworks/servicedetail/867.html

接口返回的 Base64 音频文件代码,你可以简单的拼成:

var audioUrl = "data:audio/mp3;base64," + xhr.respoonse.retData;

或者通过 Blob 将文件转换为二进制:

var data = atob( xhr.response.retData ),

result = new Unit8Array(data.length),

audioUrl;

for(var i=0,l=data.length; i

audioUrl = window.URL.createObjectURL( new Blob([result.buffer], {type: "audio/mp3"}) );

这样都能拿到一个地址,最后使用 Audio 播放即可:

new Audio( audioUrl ).play();

最后提供一个演示地址:http://word2audio.coding.io

参考资料:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐