当我试图检索和播放存储在indexedDB中的视频文件(blob)时,我收到了错误消息。我基本上打开了数据库的事务,获取文件,然后将源对象分配给HTML视频元素。
基本上,我设法将视频存储在indexedDB中,现在我想做的就是在浏览器中检索和播放视频文件。我收到错误信息。我读了一些书,发现这可能是由于"createObjectURL"的弃用,但我不确定如何将新方法融入我的代码中。
<script type="text/javascript">
(function () {
if (!('indexedDB' in window)) {
console.log('This browser doesn't support IndexedDB');
return;
}
// IndexedDB
window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB || window.OIndexedDB ||
window.msIndexedDB,
IDBTransaction = window.IDBTransaction ||
window.webkitIDBTransaction ||
window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1.0;
var indexedDB = window.indexedDB;
// Create/open database
var request = indexedDB.open("Syafunda_Videos");
request.onerror = function (event) {
// Failed to Open the indexedDB database
};
request.onsuccess = function (event) {
db = request.result;
// Open a transaction to the database
var transaction = db.transaction(["Videos"], "readwrite");
//Retrieve the video file
transaction.objectStore("Videos").get("1").onsuccess = function (event) {
var videoFile = event.target.result;
var URL = window.URL || window.webkitURL;
var videoURL = URL.createObjectURL(videoFile) ;
// Set video src to ObjectURL
var videoElement = document.getElementById("video");
videoElement.setAttribute("src", videoURL);
var mimeDisplayElement = document.getElementById("vidMimeDisplay");
mimeDisplayElement.innerHTML = videoFile.type;
};
}
})();
</script>
问题很可能与videoFile
是什么有关。
URL.createObjectURL
需要一个Blob对象来为您生成URL,但videoFile
很可能是一个常规的JavaScript对象,其中有问题的视频是该对象的属性。
也许你有类似的东西
videoFile = {
[primaryKey]: "1",
yourVideo: Blob(...)
};
您的文档是如何存储的?您可能需要进一步从event.target.result
对象中提取视频文件。