【问题标题】:Cordova File Plugin to load video source from ios cacheCordova File Plugin 从 ios 缓存加载视频源
【发布时间】:2016-09-16 14:46:07
【问题描述】:

我真的想从我的应用程序缓存中加载视频源。在我的应用程序的本机部分,我将视频保存到缓存中文件夹内的文件夹中。

/var/mobile/Containers/Data/Application/639797B4-1726-4350-91D7-2E212ACB974D/Library/Caches/.../.../clip.mov

所以我正在研究使用 cordova 文件插件:

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html#display-an-image-file-

老实说,我很困惑我应该如何实现它。我在应用程序的 Web 端几乎什么都没做。只有几个基本功能,我有点不确定如何执行此操作以及我应该在哪里执行此操作。我知道它应该在设备准备好之后来。

我想做的只是读取文件,但它说我需要一个 fileEntry 对象,我认为我需要为其创建一个临时或持久文件。 (不确定哪个合适,因为我只想暂时使用该文件,但我将它保存到文件缓存文件系统中,所以我猜它是持久的?)我只是对我需要包含的内容感到困惑。

下面是我的准系统 JS:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        ...
        //some button events
        ...
    },
    // deviceready Event Handler
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        // <---
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};
app.initialize();

如果有人能指出我正确的方向,将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript ios cordova caching plugins


    【解决方案1】:

    在这里,您可以使用此代码从您所说的路径中获取文件,并将其放在 Base64 中的变量中。在此基础上,您可以随心所欲地使用它。

    window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, function(dir) {
              console.log("got main dir",dir);
    
              dir.getFile("clip.mov", {create:false}, function(fileEntry) {
                console.log("got the file", fileEntry);
                fileEntry.file(function(file) {
                  var reader = new FileReader();
                  reader.onloadend = function(e) {
                    //In this e you have your file
                    console.log(e);
                  };
                  reader.readAsDataURL(file);
                });
              });
            }, function(err) {
                console.log(err);
            });
    

    【讨论】:

    • 您好,感谢您的回复,这似乎很有帮助。但是 window.resolveLocalFileSystemURL 似乎并没有为我开火。我已经尝试将它放在几个位置,包括我的问题中指示的 onDeviceReady 事件内部和 app.initialize() 之后,但是我什至没有得到来自 console.log 的响应或警报。有什么想法吗?
    • 你安装了cordova文件插件吗?
    • 所以目前我在触发的函数之前有一个日志,一个在不触发的函数中,一个在不触发的错误捕获中,一个在之后不触发.除此之外,我已经注释掉了所有内容,所以它似乎由于某种原因卡在了函数本身上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多