Procházet zdrojové kódy

Changed caching to include the story id as well. Probably

tags/v0.1.0
Fen Dweller před 6 roky
rodič
revize
58f630924e
1 změnil soubory, kde provedl 15 přidání a 13 odebrání
  1. +15
    -13
      audio.js

+ 15
- 13
audio.js Zobrazit soubor

@@ -7,6 +7,7 @@ let audioContext;
let gainControl;

let audioBaseUrl;
let storyName;

let audioDict = {};

@@ -17,7 +18,6 @@ function setVolume(vol) {
// play some sound

function playSfx(name) {

if (audioDict[name] == undefined) {

if (waiting[name]) {
@@ -46,7 +46,6 @@ function playSfx(name) {
}

function playLoop(name) {

if (audioDict[name] == undefined) {

if (waiting[name]) {
@@ -159,10 +158,7 @@ function parseAudioData(name, data) {
audioContext.decodeAudioData(data, function(buffer) {
audioDict[name] = buffer;

console.log(waiting);

waiting[name].forEach(queued => {
console.log(queued);
if (queued.type == "sfx") {
playSfx(name);
}
@@ -175,7 +171,7 @@ function parseAudioData(name, data) {

delete waiting[name];
}, function(e){
console.log("Error with decoding audio data" + e.err);
console.error("Error with decoding audio data" + e.err);
delete waiting[name];
});

@@ -190,12 +186,12 @@ function loadRemoteAudio(name) {
if (xhr.status == 200)
cacheAndParse(name, xhr.response);
else {
console.log("Couldn't load " + name);
console.error("Couldn't load " + name);
delete waiting[name];
}
}
xhr.onerror = (xhr) => {
console.log("Couldn't load " + name);
console.error("Couldn't load " + name);
}

xhr.send();
@@ -203,14 +199,14 @@ function loadRemoteAudio(name) {

// check if the content is cached
function checkCache(type, name, hit, miss) {
const req = window.indexedDB.open("cache", 1);
const req = window.indexedDB.open("cache", 2);
req.onsuccess = () => {
const db = req.result;
const tx = db.transaction([type], "readonly");

const audio = tx.objectStore(type);

const read = audio.get(name);
const read = audio.get([storyName, name]);

read.onsuccess = (event) => {
const res = event.target.result;
@@ -243,6 +239,7 @@ function initAudio(story, state) {
createCache();

audioBaseUrl = "./media/" + story.id + "/audio/";
storyName = story.id;

story.sounds.forEach(sound => {
loadAudio(sound);
@@ -252,7 +249,7 @@ function initAudio(story, state) {
// caching stuff here

function storeCache(type, name, blob) {
const req = window.indexedDB.open("cache", 1);
const req = window.indexedDB.open("cache", 2);
req.onsuccess = () => {
const db = req.result;
const tx = db.transaction([type], "readwrite");
@@ -260,6 +257,7 @@ function storeCache(type, name, blob) {
const audio = tx.objectStore(type);

const update = audio.put({
story: storyName,
name: name,
content: blob
});
@@ -274,12 +272,16 @@ function storeCache(type, name, blob) {
function createCache() {
let idb = window.indexedDB;

let req = idb.open("cache", 1);
let req = idb.open("cache", 2);

req.onupgradeneeded = event => {
const db = event.target.result;

const audio = db.createObjectStore("audio", { keyPath: "name" });
if (event.oldVersion == 1) {
db.deleteObjectStore("audio");
}

const audio = db.createObjectStore("audio", { keyPath: ["story", "name"] });
}

req.onerror = event => {


Načítá se…
Zrušit
Uložit