Skip to content

Instantly share code, notes, and snippets.

@urish
Created October 9, 2011 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urish/1274020 to your computer and use it in GitHub Desktop.
Save urish/1274020 to your computer and use it in GitHub Desktop.
Titanium URL download
var ZLSound = require('com.salsarhythmsoftware.zlsound');
function createUrlSound(url, localFilename) {
var sound = nil;
var autoPlay = false;
var request = Titanium.Network.createHTTPClient();
var soundFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, localFilename);
if (soundFile.exists()) {
return ZLSound.createSample({media: soundFile.nativePath});
}
request.onload = function() {
if (request.status >= 200 && request.status < 300) {
soundFile.write(request.responseData);
sound = ZLSound.createSample({
media: soundFile.nativePath
});
if (autoPlay) {
sound.play();
}
}
}
request.open('GET', url);
request.send();
return {
play: function() {
if (sound) {
sound.play();
} else {
autoPlay = true;
}
},
stop: function() {
if (sound) {
sound.stop();
} else {
autoPlay = false;
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment