Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (downloading)
Viewing all articles
Browse latest Browse all 3

Example / Productivity Tool - Saving video to iOS Simulator

$
0
0

Wrote a 5 min. app to save video from a URL into the Photo Gallery. This is a convenient way to load your iOS simulator with video content, if you need it for testing. Hopefully this will be useful to someone beside me. ;)

To use this yourself, create a new mobile app and replace app.js with this code. Run in your simulator and you're done. Then, enter in a valid URL and press the button. I find it easiest to turn on web sharing on your computer and just pull from your local device. But, this should work with any valid URL.

// GallerySaver by David Geller. No rights reserved whatsoever.
// Do whatever you like with this code!
 
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
 
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
var media_file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'captured_media.mp4');
 
/**
 * Copy the file to the gallery
 */
function copyToGallery ()
{
    var video_blob = media_file.read();
 
    Ti.API.info ("[message_download.js:copyToGallery] video_file: " + media_file.getName);
    Ti.API.info ("[message_download.js:copyToGallery] length (bytes): " + media_file.size);
 
    Ti.Media.saveToPhotoGallery (video_blob, {
        success: function(e) {
            Ti.API.info ("[message_download.js:copyToGallery] media data saved gallery");
            progress_indicator.message = 'Media saved to gallery!';
            return;
        },
        error: function(e) {
            var emsg = 'Media could not be saved to your Photo Gallery. Error: ' + e.error;
            Titanium.UI.createAlertDialog({
                title:'Error saving',
                message:emsg
            }).show();
        }
    });
}
 
function downloadMedia (path)
{
    // this is where we will save the file
    var f = media_file;
 
    if (media_file.exists()) {
        media_file.deleteFile();
    }
 
    download_xhr = Ti.Network.createHTTPClient();
    download_xhr.onload = function(e) {
        f.write(this.responseData);
        Ti.API.info ('[downloadMedia] file downloaded!');
        progress_indicator.message = 'Downloaded! Saving...';
        copyToGallery ();
        return;
    };
 
    download_xhr.onerror = function(e) {
        Ti.API.info ('[downloadMedia] onerror');
        return;
    };
 
    download_xhr.ondatastream = function (e) {
        progress_indicator.value = e.progress;
        return;
    }
 
    Ti.API.info ('[downloadMedia] getting ' + path)
    download_xhr.open ('GET', path);
 
    download_xhr.file = f;
    download_xhr.send();
}
 
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Gallery Saver',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Gallery Saver',
    window:win1
});
 
 
var url = Ti.UI.createTextField ({
    top: 40,
    left: 20,
    width: 280,
    height: 30,
    borderWidth: 1,
    borderRadius: 8,
    hintText: 'Enter URL here'
});
 
win1.add (url);
 
var progress_indicator = Titanium.UI.createProgressBar({
    width:200,
    height:50,
    min:0,
    max:1,
    value:0,
    style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
    top:100,
    message:'Downloading video',
    font:{fontSize:14, fontWeight:'normal'},
    color:'#888',
    visible:false
});
 
win1.add (progress_indicator);
 
 
var button = Ti.UI.createButton ({
    top: 150,
    width: 100,
    height: 32,
    title: 'Grab Media' 
});
 
button.addEventListener ('click', function(e) {
 
    progress_indicator.visible = true;  
    progress_indicator.value = 0;
    downloadMedia (url.value);
 
});
 
win1.add (button);
 
//
//  add tabs
//
tabGroup.addTab(tab1);  
 
 
// open tab group
tabGroup.open();

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>