Skip to content

Instantly share code, notes, and snippets.

@Barneybook
Created October 11, 2012 13:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Barneybook/3872133 to your computer and use it in GitHub Desktop.
Save Barneybook/3872133 to your computer and use it in GitHub Desktop.
record Audio using Titanium Framework
var win = Titanium.UI.createWindow({
title: 'Video Recording from Appcelerator Titanium',
backgroundColor: '#fff'
});
// const value grabbed from
// http://developer.android.com/reference/android/provider/MediaStore.Audio.Media.html#RECORD_SOUND_ACTION
var RECORD_SOUND_ACTION = "android.provider.MediaStore.RECORD_SOUND";
var soundUri = null; // Will be set as a result of recording action.
var recordButton = Titanium.UI.createButton({
top: 10, left: 10, right: 10, height: 35, title: "Record Audio"
});
var labelResultCaption = Titanium.UI.createLabel({
top: 50, left: 10, right: 10, height: 35, visible: false, color: 'yellow'
});
var labelResult = Titanium.UI.createLabel({
top: 90, left: 10, right: 10, height: 100, visible: false,
backgroundColor: 'white', color: 'black',
verticalAlign: 'top'
});
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);
recordButton.addEventListener('click', function()
{
var intent = Titanium.Android.createIntent({ action: RECORD_SOUND_ACTION });
Titanium.Android.currentActivity.startActivityForResult(intent, function(e)
{
if (e.error)
{
Titanium.UI.createNotification({
duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
message: 'Error: ' + e.error
}).show();
}
else
{
if (e.resultCode === Titanium.Android.RESULT_OK)
{
soundUri = e.intent.data;
Titanium.UI.createNotification({
duration: Titanium.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
{
Titanium.UI.createNotification({
duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
message: 'Canceled/Error? Result code: ' + e.resultCode
}).show();
}
}
});
});
saveButton.addEventListener('click', function()
{
var source = Titanium.Filesystem.getFile(soundUri);
var target = Titanium.Filesystem.getFile('appdata://harsh.amr');
// 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: Titanium.UI.NOTIFICATION_DURATION_LONG,
message: 'Saved to: ' + target.nativePath
}).show();
});
shareButton.addEventListener('click', function()
{
var intent = Titanium.Android.createIntent({
action: Titanium.Android.ACTION_SEND,
type: 'audio/amr'
});
intent.putExtraUri(Titanium.Android.EXTRA_STREAM, soundUri);
Titanium.Android.currentActivity.startActivity(
Titanium.Android.createIntentChooser(intent, 'Send Video via'));
});
win.add(recordButton);
win.add(labelResultCaption);
win.add(labelResult);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment