Cache audio samples

This commit is contained in:
Alex Yatskov 2016-06-14 20:33:19 -07:00
parent ad61722130
commit b91c79e321

View File

@ -20,7 +20,7 @@
class Client { class Client {
constructor() { constructor() {
this.popup = new Popup(); this.popup = new Popup();
this.audio = null; this.audio = {};
this.lastMousePos = null; this.lastMousePos = null;
this.lastRange = null; this.lastRange = null;
this.activateKey = 16; this.activateKey = 16;
@ -172,12 +172,15 @@ class Client {
const dfn = this.definitions[index]; const dfn = this.definitions[index];
const url = `http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana=${dfn.reading}&kanji=${dfn.expression}`; const url = `http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana=${dfn.reading}&kanji=${dfn.expression}`;
if (this.audio !== null) { for (let key in this.audio) {
this.audio.pause(); this.audio[key].pause();
} }
this.audio = new Audio(url); const audio = this.audio[url] || new Audio(url);
this.audio.play(); audio.currentTime = 0;
audio.play();
this.audio[url] = audio;
} }
api_displayKanji(kanji) { api_displayKanji(kanji) {