Created
October 9, 2011 18:56
-
-
Save urish/1274020 to your computer and use it in GitHub Desktop.
Titanium URL download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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