Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to record video, then share or save it. Using Appcelerator Titanium!
/**
* This sample lets you record and share video with Appcelerator Titanium on Android.
* REQUIRES THE 1.6.0 RC OF TITANIUM MOBILE SDK
* http://developer.appcelerator.com/blog/2011/02/release-candidate-for-titanium-mobile-1-6-0.html
*/
/**
* First, create our UI. We'll have two buttons: record, and share.
*/
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();
});
@Mabux

This comment has been minimized.

Copy link

@Mabux Mabux commented May 30, 2011

Hi,

great example, this is just what I needed!

As there is no answer in the Appcelerator Q & A to Android Video Recording, would U mind posting this there as an answer (for example here https://developer.appcelerator.com/question/86151/recording-video-in-android ) ?
I tested it on my device, and it's running fine.

Thx for sharing !

@KarthiPnsmy

This comment has been minimized.

Copy link

@KarthiPnsmy KarthiPnsmy commented Dec 16, 2011

Awesome!!!!!!! Great work

@blankenshipb

This comment has been minimized.

Copy link

@blankenshipb blankenshipb commented Dec 20, 2011

Dawson, thanks for sharing. Do you know if there is anything here that would inhibit video record quality? Have been using this but getting reports of quality issues, although I assume this intent defaults to 'high' video quality

@blankenshipb

This comment has been minimized.

Copy link

@blankenshipb blankenshipb commented Dec 20, 2011

...the capture overlay presents options for video quality and shows 'high' as default - again, just wondering if you might have an explanation for any reports of video quality issues (comparing new Android output to iPhone 4 output, for example). Thanks.

@Jomen

This comment has been minimized.

Copy link

@Jomen Jomen commented Nov 2, 2012

Hello. This is great! Do you also know a way to make the recorded video to be displayed in a: var activeMovie = Titanium.Media.createVideoPlayer on a Android devise? It seems to be impossible to make it show up in a Android view.

@Jomen

This comment has been minimized.

Copy link

@Jomen Jomen commented Nov 2, 2012

Hello. This is great! Do you also know a way to make the recorded video to be displayed in a: var activeMovie = Titanium.Media.createVideoPlayer on a Android devise? It seems to be impossible to make it show up in a Android view.

@dragosstancu

This comment has been minimized.

Copy link

@dragosstancu dragosstancu commented Feb 11, 2013

Thank you!

@martintietz

This comment has been minimized.

Copy link

@martintietz martintietz commented Nov 18, 2013

Unfortunately, this is not working on some Android devices (e.g. my Samsung Galaxy Nexus). On those devices e.intent.data is always null. I tried several solutions suggested on stackoverflow (e.g. My Android camera Uri is returning a null value, but the Samsung fix is in place, help?), but not a single one lead to success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.