Video Tag Local File Download Cordova Android

走远了吗. 提交于 2019-12-06 12:23:09

问题


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>

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 FOUND A SOLUTION !!!!

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


回答1:


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>';
    });
});

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



来源:https://stackoverflow.com/questions/22083023/video-tag-local-file-download-cordova-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!