Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Recording video in Android

Hi,
I've read on these forums that recording video is not currently supported in Android. The final version of my app does need to incorporate this feature; but I could hold off if video recording is in the pipeline. Is there some knowledge of a time line when video recording will be supported in the built in apis for Android?
Also, it seems that with the coming 1.5.0 release there will be "proxys." From what I understand from this page http://developer.appcelerator.com/doc/mobile/android/module_sdk I could write a Java-based Activity in Android and use it to control video recording and communicate with the titanium app.

I'm new to appcelerator; so I'd appreciate any information on the subject.

Thanks,
Vance

— asked December 5th 2010 by Vance Turnewitsch
  • android
  • record
  • recording
  • video
  • videos
0 Comments

3 Answers

  • You can try this code

    var win = Titanium.UI.createWindow({
        title: 'Video Recording from Appcelerator Titanium',
        backgroundColor: '#fff'
    });
    var recordButton = Titanium.UI.createButton({
        top: 10, left: 10, right: 10, height: 35, title: 'Record Video'
    });
    win.add(recordButton);
    var shareButton = Titanium.UI.createButton({
        top: 50, left: 10, right: 10, height: 35,
        title: 'Share Recorded Video', visible: false
    });
    win.add(shareButton);
    var saveButton = Titanium.UI.createButton({
        top: 100, left: 10, right: 10, height: 35,
        title: 'Save Recorded Video', visible: false
    });
    win.add(saveButton);
    win.open();
    
    /**
    * We'll use the following variable to keep track of the result of our recording action.
    */
    var videoUri = null;
    
    /**
    * When they click this, we'll start the video capture activity and wait to hear back from it.
    */
    recordButton.addEventListener('click', function() {
        // http://developer.android.com/reference/android/provider/MediaStore.html
        var intent = Titanium.Android.createIntent({ action: 'android.media.action.VIDEO_CAPTURE' });
        Titanium.Android.currentActivity.startActivityForResult(intent, function(e) {
            if (e.error) {
                Ti.UI.createNotification({
                    duration: Ti.UI.NOTIFICATION_DURATION_LONG,
                    message: 'Error: ' + e.error
                }).show();
            } else {
                if (e.resultCode === Titanium.Android.RESULT_OK) {
                    videoUri = e.intent.data;
                    Ti.UI.createNotification({
                        duration: Ti.UI.NOTIFICATION_DURATION_LONG,
                        message: 'Video captured; now share or save it!'
                    }).show();
                    // note that this isn't a physical file! it's a URI in to the MediaStore.
                    shareButton.visible = true;
                    saveButton.visible = true;
                } else {
                    Ti.UI.createNotification({
                        duration: Ti.UI.NOTIFICATION_DURATION_LONG,
                        message: 'Canceled/Error? Result code: ' + e.resultCode
                    }).show();
                }
            }
        });
    });
    
    /**
    * When they click this, we'll start an activity with an intent chooser to let the user
    * choose how they want to share their video.
    */
    shareButton.addEventListener('click', function() {
        var intent = Titanium.Android.createIntent({
            action: Titanium.Android.ACTION_SEND,
            type: 'application/octet-stream'
        });
        intent.putExtraUri(Titanium.Android.EXTRA_STREAM, videoUri);
        Titanium.Android.currentActivity.startActivity(
                Titanium.Android.createIntentChooser(intent, 'Send Video via'));
    });
    
    /**
    * When they click this, we'll save the video to the SDCard and tell the user where to find it.
    */
    saveButton.addEventListener('click', function() {
        var source = Ti.Filesystem.getFile(videoUri);
        var target = Ti.Filesystem.getFile('appdata://sample.3gp');
        // note: source.exists() will return false, because this is a URI into the MediaStore.
        // BUT we can still call "copy" to save the data to an actual file
        source.copy(target.nativePath);
    
        Ti.UI.createNotification({
            duration: Ti.UI.NOTIFICATION_DURATION_LONG,
            message: 'Saved to: ' + target.nativePath
        }).show();
    });
    
    — answered May 9th 2012 by Harsh Punnoose
    permalink
    1 Comment
    • This is great! Do anyone also know a how to make the recorded video to be displayed in a: var activeMovie = Titanium.Media.createVideoPlayer on a Android devise? I only get a black screen.

      — commented November 2nd 2012 by Åge Engjom
  • Hi Vance,

    Here is a gist that demonstrates how to record video on Android:

    https://gist.github.com/832488

    It starts the video capture activity and handles the result when it's available.

    Regards,
    Dawson

    — answered May 31st 2011 by Dawson Toth
    permalink
    2 Comments
    • Just tried this with Ti SDK 3.1.0 and it's not working for me. The video recording screen is shown, I can record video, but the callback function is never invoked!! So I can't do anything with the recorded video…

      — commented April 26th 2013 by Kevin Southworth
    • agree with Kevin callback function is never invoked, working with Ti SDK 3.2

      — commented February 25th 2014 by Amit Joshi
  • This tweak of the above solution does work under 3.x TI SDK. You need to get a reference to currentActivity a different way. Tested and working on SDK 3.2.1 and Galaxy Nexus (Android 4.0.4).

    var curActivity = win.getActivity();

    https://gist.github.com/tgreiser/9360124

    — answered March 5th 2014 by Tim Greiser
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.