Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Last active December 16, 2015 21:20
Show Gist options
  • Save cfjedimaster/5498530 to your computer and use it in GitHub Desktop.
Save cfjedimaster/5498530 to your computer and use it in GitHub Desktop.
document.addEventListener('deviceready', deviceready, false);
var buttomDom;
var statusDom;
var fileSystem;
function deviceready() {
console.log('dv ready');
//step one is to request a file system
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0,
function(fs) {
fileSystem = fs;
buttonDom = document.querySelector('#startDl');
buttonDom.addEventListener('touchend', startDl, false);
buttonDom.removeAttribute("disabled");
statusDom = document.querySelector('#status');
}, function(e) {
alert('failed to get fs');
alert(JSON.stringify(e));
});
}
function startDl() {
buttonDom.setAttribute("disabled","disabled");
var ft = new FileTransfer();
var uri = encodeURI("http://archive.org/download/Kansas_Joe_Memphis_Minnie-When_Levee_Breaks/Kansas_Joe_and_Memphis_Minnie-When_the_Levee_Breaks.mp3");
var downloadPath = fileSystem.root.fullPath + "/download.mp3";
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
statusDom.innerHTML = perc + "% loaded...";
} else {
if(statusDom.innerHTML == "") {
statusDom.innerHTML = "Loading";
} else {
statusDom.innerHTML += ".";
}
}
};
ft.download(uri, downloadPath,
function(entry) {
statusDom.innerHTML = "";
var media = new Media(entry.fullPath, null, function(e) { alert(JSON.stringify(e));});
media.play();
},
function(error) {
alert('Crap something went wrong...');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment