Created
January 26, 2019 09:09
-
-
Save karantir/1e0a3a289e15195065d5ad4cec8db7f3 to your computer and use it in GitHub Desktop.
Play sound with Howler from blob stored in IndexedDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// IndexedDB | |
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB, | |
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction, | |
dbVersion = 2.0; | |
// Create/open database | |
var request = indexedDB.open("soundScapeFiles", dbVersion), | |
db, | |
createObjectStore = function (dataBase) { | |
// Create an objectStore | |
console.log("Creating objectStore") | |
dataBase.createObjectStore("sounds"); | |
}, | |
getImageFile = function () { | |
// Create XHR | |
var xhr = new XMLHttpRequest(), | |
blob; | |
xhr.open("GET", "/media/big.mp3", true); | |
// Set the responseType to blob | |
xhr.responseType = "blob"; | |
xhr.addEventListener("load", function () { | |
if (xhr.status === 200) { | |
console.log("Image retrieved"); | |
// Blob as response | |
blob = xhr.response; | |
console.log("Blob:" + blob); | |
// Put the received blob into IndexedDB | |
putElephantInDb(blob); | |
} | |
}, false); | |
// Send XHR | |
xhr.send(); | |
}, | |
putElephantInDb = function (blob) { | |
console.log("Putting elephants in IndexedDB"); | |
// Open a transaction to the database | |
var transaction = db.transaction(["sounds"], 'readwrite'); | |
// Put the blob into the dabase | |
var put = transaction.objectStore("sounds").put(blob, "big"); | |
// Retrieve the file that was just stored | |
transaction.objectStore("sounds").get("big").onsuccess = function (event) { | |
var soundFile = event.target.result; | |
console.log(soundFile); | |
// Get window.URL object | |
var URL = window.URL || window.webkitURL; | |
// Create and revoke ObjectURL | |
var soundURL = URL.createObjectURL(soundFile); | |
// Set img src to ObjectURL | |
setTimeout(() => { | |
var sound = new Howl({ | |
src: [soundURL], | |
format: 'mp3', | |
sprite: { __default: [0, 20000, true] }, | |
volume: .9, | |
onload() { | |
setTimeout(() => { sound.play(); }, 500) | |
URL.revokeObjectURL(soundURL); | |
} | |
}) | |
}, 3000) | |
}; | |
}; | |
request.onerror = function (event) { | |
console.log("Error creating/accessing IndexedDB database"); | |
}; | |
request.onsuccess = function (event) { | |
console.log("Success creating/accessing IndexedDB database"); | |
db = request.result; | |
db.onerror = function (event) { | |
console.log("Error creating/accessing IndexedDB database"); | |
}; | |
getImageFile(); | |
} | |
request.onupgradeneeded = function (event) { | |
console.log('Creating store') | |
createObjectStore(event.target.result); | |
}; | |
})() |
i love you!!! thank you haha
Good stuff, saved me a decent chunk of time. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helped me a lot in my project. Thanks for sharing 👍