视频标签本地文件下载Cordova Android [英] Video Tag Local File Download Cordova Android

查看:177
本文介绍了视频标签本地文件下载Cordova Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个视频,然后使用apache Cordova 3.4.0播放,但我不知道是否可以使用视频标签。有一些插件可以使用视频标签,例如

Html5Video ,但它需要视频文件存在于应用程序文件夹中。还有其他插件使用其他播放器,并使用文件路径加载文件,但由于文件下载现在使用Html5 urls,fullPath给我一个真实的路径文件系统,真的不可能知道文件的保存位置,或者给予视频播放器的路径。有没有办法访问应用程序文件夹位置,并把视频以这样的方式,它可以找到由cordova和播放?

I'm trying to download a video and then play it using apache Cordova 3.4.0, but I don't know if I can use the video tag. There are some plugins that work for using the video tag like
Html5Video but it needs the video file to be present in the app folder. There are other plugins that use other players and load the file using the file path, but since the file download now uses Html5 urls, and the fullPath gives me a realitive path to the file system, it's really impossible to know where the file was saved, or the path to give to a video player. Is there a way to access the app folder location and put the video in such a way that it can be found by cordova and played?

这是我到目前为止,但我不知道如何找到视频。

This is what I have so far, but I don't know how to find the video.

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <div id="video_container">
                Loading Video...
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();

            document.addEventListener("deviceready", onDeviceReady, false);

            // device APIs are available
            //
            function onDeviceReady() {
                console.log('Requesting file system');
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
            }

            function gotFS(fileSystem) {
                fileSystem.root.getDirectory("vids", {create: true}, gotDir);
            }

            function gotDir(dirEntry) {
                dirEntry.getFile("some_video.mp4", {create: true, exclusive: false}, gotFile);
            }

            function gotFile(fileEntry) {
                var localPath = fileEntry.fullPath;
                var localUrl = fileEntry.toURL();

                console.log('Loaded local path: ' + localPath);
                console.log('Loaded local url: ' + localUrl);

                var fileTransfer = new FileTransfer();
                var uri = encodeURI('http//site/with/video.mp4');
                console.log('Downloading ' + uri + ' to ' + localPath);

                fileTransfer.download(
                    uri,
                    localUrl,
                    function(entry) {
                        console.log('download complete (path): ' + entry.fullPath); // Returns '/vids/some_video.mp4'
                        console.log('download complete (url): ' + entry.toURL()); // Returns 'cdvfile://localhost/persistent/vids/some_video.mp4'
                        document.getElementById('video_container').innerHTML = 
                        'Downloaded Video path: ' + entry.fullPath + '<br />'
                        + 'Downloaded Video url: ' + entry.toURL() + '<br />'
                        + '<video width="100%" height="300" controls>' 
                        + '<source src="' + entry.toURL() + '" type="video/mp4">'
                        + '</video>';
                        console.log('Wrote video player');
                    },
                    function(error) {
                        console.log('download error source ' + error.source);
                        console.log('download error target ' + error.target);
                    }
                );
            }

            function fail(error) {
                console.log('Error creating file [' + error.name + ']: ' + error.message);
            }
        </script>
    </body>
</html>

文件正在正确下载,但是如何下载完整路径?或将其移至我的cordova文件夹,将网址放入视频标签中?

The file is being correctly downloaded, but how can I have the fullPath once its downloaded? Or move it to my cordova folder to put the url in the video tag?

我得到它与开发分支如解释此处

I got it working with the dev branches as explained here.

推荐答案

正如你所发现的(几乎是在代码被执行后几乎立即),File的dev分支添加了一个方法 .toNativeURL()到FileEntry对象。

As you found (almost immediately after the code was committed :) ) the dev branch of File adds a method, .toNativeURL() to FileEntry objects. You can use this to get a URL suitable for the source of a tag.

最简单的代码是这样的:

The simplest code that works with this is something like this:

requestFileSystem(TEMPORARY, 0, function(fileSystem) {
    var ft = new FileTransfer();
    ft.download(uri, fileSystem.root.toURL() + "/" + filename, function(entry) {
        document.getElementById('video_container').innerHTML = 
              'Downloaded Video path: ' + entry.fullPath + '<br />'
            + 'Downloaded Video url: ' + entry.toURL() + '<br />'
            + '<video width="100%" height="300" controls>' 
            + '<source src="' + entry.toNativeURL() + '" type="video/mp4">'
            + '</video>';
    });
});

更新:截至3月4日, (1.0.1)包括此功能。不再需要开发分支。

Update: As of 4 March, the published version of the File plugin (1.0.1) includes this capability. Dev branch is no longer necessary.

这篇关于视频标签本地文件下载Cordova Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆