From cc84b81b30e56f1d6d49238d431c6037a050dafa Mon Sep 17 00:00:00 2001 From: example Date: Sun, 14 Apr 2019 14:27:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=9F=B3=E9=A2=91=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E6=96=B9=E6=B3=95=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=80=A7?= =?UTF-8?q?=E6=9B=B4=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- translate/translate-dictionary.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index dcdb1d3..a44771c 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 划词翻译:多词典查询 // @namespace http://tampermonkey.net/ -// @version 4.3 +// @version 4.4 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” // @author https://github.com/barrer // @match http://*/* @@ -756,23 +756,30 @@ } /**发音*/ function play(obj) { - var audio = new iframeWin.Audio(); if (obj.url in audioCache) { // 查找缓存 log('audio in cache', obj, audioCache); - audio.src = createObjectURLWithTry(audioCache[obj.url]); - audio.play(); // 播放 + playArrayBuffer(audioCache[obj.url]); // 播放 } else { ajax(obj.url, function (rst, res) { - audio.src = createObjectURLWithTry(res.response); audioCache[obj.url] = res.response; // 放入缓存 - audio.play(); // 播放 + playArrayBuffer(audioCache[obj.url]); // 播放 }, function (rst) { log(rst); }, { - responseType: 'blob' + responseType: 'arraybuffer' }); } } + /**播放 ArrayBuffer 音频*/ + function playArrayBuffer(arrayBuffer) { + var context = new iframeWin.AudioContext() || new iframeWin.webkitAudioContext(); + context.decodeAudioData(arrayBuffer.slice(0), function (audioBuffer) { // `slice(0)`克隆一份(`decodeAudioData`后原数组清空) + var bufferSource = context.createBufferSource(); + bufferSource.buffer = audioBuffer; + bufferSource.connect(context.destination); + bufferSource.start(); + }); + } /**得到发音按钮*/ function getPlayButton(obj) { var type = document.createElement('a');