简体   繁体   English

下载文件以供离线使用Cordova / PhoneGap构建

[英]Download file for offline use Cordova/PhoneGap build

I'm a novice when it comes to the development of apps. 关于应用程序开发,我是新手。 I made a simple audio player app for android and iOS and build it with phonegap build. 我为Android和iOS开发了一个简单的音频播放器应用,并使用phonegap构建。

The app uses the <audio> html tag to load mp3 files that are located on a remote server like so: 该应用程序使用<audio> html标记加载位于远程服务器上的mp3文件,如下所示:

            <div class="mobileui-music-cover-controls">
    <audio ontimeupdate="udpateProgress()" id="player">
        <source src="http://www.myserver.com/audiofile.mp3" type="audio/mpeg">          
    </audio>
            <a id="playKnop" href="JavaScript:playMusic()" class="play"><i class="ion-ios-play"></i></a>
            <a id="pauseKnop" href="JavaScript:pauseMusic()" class="play"><i class="ion-ios-pause"></i></a>
        </div>

obviously, I changed the scr for this example. 显然,我更改了此示例的scr

It works fine as long as you have a stable internet connection but a few people are experiencing connection drops etc. I would like to make the files offline available on user request. 只要您具有稳定的Internet连接,但有些人遇到连接断开等情况,它就可以正常工作。我想根据用户要求使文件脱机。 So it has to be optional (by clicking a button or something like that). 因此,它必须是可选的(通过单击按钮或类似的按钮)。 The app should detect if the file is present on the device and if so choose the local file over the remote file. 应用程序应检测设备上是否存在该文件,如果存在,则选择本地文件,而不是远程文件。

In short, I have 2 questions. 简而言之,我有2个问题。

  1. How can I download a specific file on user request? 如何根据用户要求下载特定文件?
  2. How can I check whether the file is there or not and play the right file? 如何检查文件是否存在并播放正确的文件?

I can't seem to figure out how to do this I'm working on it for the last couple of days but I honestly have no clue on how to achieve this. 我似乎无法弄清楚该如何做,最近几天我一直在努力,但老实说,我对如何实现这一目标一无所知。 Any help would be greatly appreciated. 任何帮助将不胜感激。

As you said you need to download the file to your device. 如您所说,您需要将文件下载到设备上。 Since your using cordova you could use fileTransfer to download the audio. 由于您使用的是cordova,因此可以使用fileTransfer下载音频。 This is very straight forward (this is the offical example code): 这非常简单(这是官方示例代码):

// !! Assumes variable fileURL contains a valid URL to a path on the device,
//    for example, cdvfile://localhost/persistent/path/to/downloads/

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    fileURL,
    function(entry) {
        console.log("download complete: " + entry.toURL());
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("download error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

The next time you want to play the audio you need to check if there is a local version. 下次您要播放音频时,需要检查是否有本地版本。 Either you check your device if that file exists (there are many ways, eg Cordova check if file in url exists ) or you make use of something like localStorage which basically is a simple lil database. 您可以检查设备是否存在该文件(有很多方法,例如Cordova检查url中的文件是否存在 ),或者使用诸如localStorage之类的东西,它基本上是一个简单的lil数据库。

// more or less pseudo code!!!

// callback from fileTransfer when your file was downloaded
function downloadSuccess(entry) {
    // save it to localStorage 
    // key: the remote URL, value: the local URL
    localStorage.setItem(remoteURL, entry.toURL());
}

...
...

// the check if there is a cached file
var remoteSrc = "http://www.myserver.com/audiofile.mp3";
var localSrc = localStorage.getItem(remoteSrc);

if(localSrc === null) {
    // when there is NO cached version, use remote
    audioElement.src = remoteSrc;
} else {
    // when there IS a cached version, use local
    audioElement.src = localSrc;
}

I hope this helps and gives you an idea how to accomplish simple caching yourself :) 希望对您有所帮助,并为您提供一个如何完成简单缓存的想法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM