Skip to content

Instantly share code, notes, and snippets.

@mayroncachina
Created August 7, 2015 03:02
Show Gist options
  • Save mayroncachina/20cd4a6df37caf94a7bf to your computer and use it in GitHub Desktop.
Save mayroncachina/20cd4a6df37caf94a7bf to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", init, false);
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "http://appncpc.com.br/public/audio/00015.ogg";
//File name of our important data file we didn't ship with the app
var fileName = "00015.ogg";
function init() {
$status = document.querySelector("#status");
$status.innerHTML = "Checking for data file.";
store = cordova.file.dataDirectory;
alert(store);
console.log(store);
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
alert("About to start transfer");
console.log("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
alert("Success!");
appStart();
},
function(err) {
alert("Error");
console.dir(err);
});
}
//I'm only called when the file exists or has been downloaded.
function appStart() {
console.log("file ja existe")
$status.innerHTML = "App ready!";
play(store+fileName)
console.log(store+fileName);
}
function play(locale){
//var audioElement = document.createElement('player');
//audioElement.setAttribute('src', locale);
console.log("play")
var my_media = new Media(locale,
// success callback
function () { console.log("playAudio():Audio Success"); },
// error callback
function (err) { console.log("playAudio():Audio Error: " + err); }
);
// Play audio
my_media.play();
}
function player(){
var my_media = new Media(store+fileName,
// success callback
function () { console.log("playAudio():Audio Success"); },
// error callback
function (err) { console.log("playAudio():Audio Error: " + err); }
);
// Play audio
my_media.play();
}
</script>
</head>
<body >
<h2>My App</h2>
<div id="status"></div>
<button type="button" onclick="init()">DOWNLOAD</button>
<button type="button" onclick="player()">Play</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment