本文主要介紹 AMR(Aduio) 播放 AMR 格式 Base64碼 音頻。 1、必備資料 github AMR 開源庫 :https://github.com/jpemartins/amr.js 用心把這個項目看一遍,對於我下麵說的話,可以忽略啦,代碼是最好的文章,哈哈~~ 2、核心 JS 庫
本文主要介紹 AMR(Aduio) 播放 AMR 格式 Base64碼 音頻。
1、必備資料 github AMR 開源庫 :https://github.com/jpemartins/amr.js 用心把這個項目看一遍,對於我下麵說的話,可以忽略啦,代碼是最好的文章,哈哈~~
2、核心 JS 庫 :amr.js 、pcmdata.min.js、libamr-nb.js (g上述ithub項目中有另外三個js,我給合成一個amr.js,不要混亂)這三個 JS 是播放聲音的主要依賴,下麵一 一 介紹下:
① amr.js : 可以理解成 “橋” 的概念,連接底層解碼和客戶端調用的橋梁。
② pcmdata.min.js : 封裝 pcm data, PCM是一種編碼格式 , PCM 添加上 RIFF 頭 可以組成 Wave 音頻(Wave 不止這一種編碼方式),細節太深,小弟也不懂啦~~,有空深 挖。
③ libamr-nb.js : amr轉換的核心庫,這個文件比較大,壓縮後大概也要 600kb 左右。
④ 如果下載三個核心庫,建議從上篇 “Web 播放聲音 — 介紹篇” 中的附件中卸載,在 github 現在的核心庫,在 Chrome 下只能播放一次,把這個問題修複了一下,原因是 以同樣的 Audio.src 創建 Audio 在 Chrome 下就不能二次播放,網上說法各異,最終自己動手解決了,詳細可以在 amr.js 中的 global.util.play 方法中看。
3、具體用法
說了有一陣子白話了,該上點代碼了,下麵是我封裝的播放 AMR 格式 Base64 碼 的插件,直接在頁面引用可以 ,下載地址可以在 “Web 播放聲音 — 介紹篇” 下載。
頁面一點樣式都沒有加,只有兩個按鈕,小伙伴湊合湊合,哈哈~~
代碼中只引入了 voice.js 一個 js,其他 js 動態引用了。(公司有 cdn 所以直接放在上面了)。
步驟:
第一步:執行初始化。
第二步:調用相應方法。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Example</title> <script src="js/voice.js"></script> </head> <body> <input type="button" value="play" id="playId"/> <input type="button" value="stop" id="stopId"/> </body> <script type="text/javascript">
//初始化 RongIMLib.RongIMVoice.init();
//播放
document.getElementById("playId").onclick = function(){
// base64Str : amr 格式的 base64 碼,可以用 HTML5 FileReader 或者canvas 進行轉換 。
var base64Str = "IyFBTVIKLNEafAAeef/hgmeAH8AD/+ggggAALMWpzAAf+f/hgmYAH8AD/+ggggAALEV+mgIf+f/hlGYCH8RGH+ggg+IiLE9FsgAeLP/gmyfQHOAz5qRSggiYLJCYjhETDa/giiYg33Oz99Su+hiILNHNfAAf+f/hgmYAH8AD/+ggggAALMWFzAAf+f/hgmYAH8AD/+ggggAALNGpzAAf+f/hgmYAH8AD/+ggggAALE4csoB5sf9lkkxqW51ZZegtsmYqLIsXQA0htL0vj55kHodV5Ik/SKIoLE8XnBwpvO5Xn42Xdtyqk5KTkRyeLJdmnB4BBMI1biyoWiAi3i9xLseGLGUGUpBphDE++psEzkywnUH68MxcLJbqQB4CBcQ7pzqFTycUUtGuMk0ULJMXRhwgDonzLl/njVsRFafpt8yiLJcNQB4AF+S6LiT1KANiUqkLyIiOLGkNtB5Ea6QcnnWtUY6+pGq5AVuwLHoWdh5BYX5agJQYmJJix5GicDgQLHQCjhxghnG7GQ9yqaLWUzr2hrlMLHcNnB8AqHn/xmlQYxtv3Uu8c7S+LHQqQB/EQHyj7zDTOWlGKd9SksFULGQAnB/gWytgX4Ojr40Mj5DxCk6yLJICnB7gCth1XzDxFgXpuVpje9lWLG4CiB8kYNGk6mLcVIaeKzJvaEguLJYWjh/FDBcDxySILxqc3WuPh2LWLHUXiB+igeMi0rlXL9qLRPJ9yAgKLHqb4B9ghgzpfklRcUrl6fgErpH4LKHtjodAAMuy07Nz9t2lzEaVTzQ+LHoM5h9ADvHpHDlHqXHQn0mlO+PqLHdTjh6gSxyzpoS8jxpdTfLMassWLLAMnB5AAYXClg6eYIQV3Uz3GMFQLGIMiB6Ihh6FOobh08zeqq7GGnNWLGXtiB8AFX3GO+uJSPIOzsij673ALHgZjh4AL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RongIMLib.RongIMVoice.play(base64Str ); };
//暫停
document.getElementById("stopId").onclick = function(){ RongIMLib.RongIMVoice.stop(); };
</script>
</html>
在這裡也把 "voice.js" 的代碼貼出來,使用 typescript 寫完後生成出來的,方便小伙伴們查看:
var RongIMLib; (function (RongIMLib) { var RongIMVoice = (function () { function RongIMVoice() { } /** * 初始化聲音庫 */ RongIMVoice.init = function () { if (this.isIE) { var div = document.createElement("div"); div.setAttribute("id", "flashContent"); document.body.appendChild(div); var script = document.createElement("script"); script.src = "http://cdn.ronghub.com/swfobject-2.0.0.min.js"; var header = document.getElementsByTagName("head")[0]; header.appendChild(script); setTimeout(function () { var swfVersionStr = "11.4.0"; var flashvars = {}; var params = {}; params.quality = "high"; params.bgcolor = "#ffffff"; params.allowscriptaccess = "always"; params.allowfullscreen = "true"; var attributes = {}; attributes.id = "player"; attributes.name = "player"; attributes.align = "middle"; swfobject.embedSWF("http://cdn.ronghub.com/player-2.0.2.swf", "flashContent", "1", "1", swfVersionStr, null, flashvars, params, attributes); }, 200); } else { var list = ["http://cdn.ronghub.com/pcmdata-2.0.0.min.js", "http://cdn.ronghub.com/libamr-2.0.1.min.js"]; for (var i = 0, len = list.length; i < len; i++) { var script = document.createElement("script"); script.src = list[i]; document.head.appendChild(script); } } this.isInit = true; }; /** * 開始播放聲音 * @param data {string} amr 格式的 base64 碼 * @param duration {number} 播放大概時長 用 data.length / 1024 */ RongIMVoice.play = function (data, duration) { this.checkInit("play"); var me = this; if (me.isIE) { me.thisMovie().doAction("init", data); } else { me.palyVoice(data); me.onCompleted(duration); } }; /** * 停止播放聲音 */ RongIMVoice.stop = function () { this.checkInit("stop"); var me = this; if (me.isIE) { me.thisMovie().doAction("stop"); } else { if (me.element) { me.element.stop(); } } }; /** * 播放聲音時調用的方法 */ RongIMVoice.onprogress = function () { this.checkInit("onprogress"); };
/**
* 校驗是否初始化
*/ RongIMVoice.checkInit = function (postion) { if (!this.isInit) { throw new Error("RongIMVoice not initialized,postion:" + postion); } }; RongIMVoice.thisMovie = function () { return eval("window['player']"); }; RongIMVoice.onCompleted = function (duration) { var me = this; var count = 0; var timer = setInterval(function () { count++; me.onprogress(); if (count >= duration) { clearInterval(timer); } }, 1000); if (me.isIE) { me.thisMovie().doAction("play"); } }; RongIMVoice.base64ToBlob = function (base64Data, type) { var mimeType; if (type) { mimeType = { type: type }; } base64Data = base64Data.replace(/^(.*)[,]/, ''); var sliceSize = 1024; var byteCharacters = atob(base64Data); var bytesLength = byteCharacters.length; var slicesCount = Math.ceil(bytesLength / sliceSize); var byteArrays = new Array(slicesCount); for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) { var begin = sliceIndex * sliceSize; var end = Math.min(begin + sliceSize, bytesLength); var bytes = new Array(end - begin); for (var offset = begin, i = 0; offset < end; ++i, ++offset) { bytes[i] = byteCharacters[offset].charCodeAt(0); } byteArrays[sliceIndex] = new Uint8Array(bytes); } return new Blob(byteArrays, mimeType); }; RongIMVoice.palyVoice = function (base64Data) { var reader = new FileReader(), blob = this.base64ToBlob(base64Data, "audio/amr"), me = this; reader.onload = function () { var samples = new AMR({ benchmark: true }).decode(reader.result); me.element = AMR.util.play(samples); }; reader.readAsBinaryString(blob); }; RongIMVoice.isIE = /Trident/.test(navigator.userAgent); RongIMVoice.isInit = false; return RongIMVoice; })(); RongIMLib.RongIMVoice = RongIMVoice; //相容AMD CMD if ("function" === typeof require && "object" === typeof module && module && module.id && "object" === typeof exports && exports) { module.exports = RongIMVoice; } else if ("function" === typeof define && define.amd) { define("RongIMVoice", [], function () { return RongIMVoice; }); } })(RongIMLib || (RongIMLib = {}));
好啦,到此本文結束,會儘快更新 Flash 下播放 AMR 格式 Base64 碼,代碼中哪裡有錯誤或者有異議麻煩小伙伴們聯繫我,一起討論,有則改之無法加冕,一起進步,thx~~。