Capturing video via the video recorder

You can also use the inbuilt camera of your iPhone (3GS and above) or Android device to record video. The quality and length of the video that you can record is dependant on both your device's memory capabilities and the type of camera that's included in the hardware. However, you should at least be able to capture short video clips in VGA resolution as a minimum.

In this recipe we will create a basic interface for our Video tab consisting of a record button, which will launch the camera and record video on your device. We'll also perform this in two separate ways: using standard Titanium code for the iPhone and using intents for Android.

Note

Note that this recipe will require a physical device for testing. In addition, the iPhone 3G models are not be capable of recording video, but all models from the 3GS and upwards should be fine.

Getting ready

Create a new JavaScript file called video.js and save it into your resources folder. Then, back in your app.js file, add the URL property of window2 and give it a value of video.js. This will load up our video JavaScript file for the second tab window of our application.

Note

Complete source code for this recipe can be found in the /Chapter 4/Recipe 7 folder.

How to do it...

First of all, let's set up the basic interface to have a record button (in the navigation bar section for the iPhone and as a normal button for Android), along with the videoFile variable. This will hold the path to our recorded video as a string.

var win = Titanium.UI.currentWindow;

var videoFile = 'video/video-test.mp4';

var btnGetVideo = Titanium.UI.createButton({
  title: 'Record Video'
});


//set the right nav butto to our get button
if(Ti.Platform.osname == 'iphone') {
  win.rightNavButton = btnGetVideo;
}
else {
  //add it to the main window because android does
  //not have 'right nav button'
  btnGetVideo.right = 20;
  btnGetVideo.top = 20;
  win.add(btnGetVideo);
}

Now let's create the event listener and handler code for the record button. This will check our current platform (either iPhone or Android) and execute the record video code for the correct platform:

//get video from the device
btnGetVideo.addEventListener('click', function()
  {
    if(Titanium.Platform.osname == 'iphone') {
      //record for iphone
      Titanium.Media.showCamera({
        success:function(event)
        {
          var video = event.media;
          movieFile = Titanium.Filesystem.getFile(
            Titanium.Filesystem.applicationDataDirectory, 
            mymovie.mov'),

          movieFile.write(video);
          videoFile = movieFile.nativePath;
          btnGetVideo.title = 'Play Video';
        },
        cancel:function()
        {
        },
        error:function(error)
        {
          // create alert
          var a = 
          Titanium.UI.createAlertDialog({title:'Video'});

          // set message
          if (error.code == Titanium.Media.NO_VIDEO)
          {
            a.setMessage('Device does not have video recording 
              capabilities'),
          }
          else
          {
            a.setMessage('Unexpected error: ' + error.code);
          }

          // show alert
          a.show();
        },
        mediaTypes: Titanium.Media.MEDIA_TYPE_VIDEO,
        videoMaximumDuration:10000,
        videoQuality:Titanium.Media.QUALITY_HIGH
      });
    } 
    else 
    {
      //record for android using intents
      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) {
              videoFile = e.intent.data;
              var source = Ti.Filesystem.getFile(videoFile);
              movieFile = 
              Titanium.Filesystem.getFile(
                Titanium.Filesystem.applicationDataDirectory, 
              'mymovie.3gp'),

              source.copy(movieFile.nativePath);
              videoFile = movieFile.nativePath;
              btnGetVideo.title = 'Play Video';
            } 
            else {
              Ti.UI.createNotification({
                duration: Ti.UI.NOTIFICATION_DURATION_LONG,
                message: 'Canceled/Error? Result code: ' + 
                  e.resultCode
              }).show();
          }
        }
      });

    }
});

How it works…

Let's work through the code for recording on iPhone devices first, which is encapsulated within the if(Titanium.Plaform.osname == 'iphone') statement as mentioned in the previous code. Here, we are executing the camera in the same way you would for capturing plain photos, however, we're passing additional parameters. The first of these is called mediaType, and it tells the device we want to capture a mediaType of MEDIA_TYPE_VIDEO.

The other two parameters define how long and what quality to capture the video in. The parameter videoMaximumDuration float defines the duration (how long in milliseconds to allow capture before completing,) while the videoQuality constant indicates the video quality during capture. We have set these to 10 seconds (10,000 milliseconds) and a video quality of "high" respectively.

On successful video capture, we save the event.media (our video in its raw format) to the filesystem, using pretty much the same method as we did when saving a photograph. The final step is to set the videoFile path to the location of our newly saved video file on the filesystem.

For Android, we are capturing the video in a different way, using an intent. In this case, we're using the video capture intent called android.media.action.VIDEO_CAPTURE. Objects of type android.content.Intent are used to send asynchronous messages within your application or between applications. Intents allow the application to send or receive data to and from other activities or services. They also allow it to broadcast that a certain event has occurred. In our recipe's code, we are executing our Intent and then capturing the result—if the resultCode equals Titanium.Android.RESULT_OK then we know that we've managed to record a video clip. We can then move this file from its temporary storage location to a new destination of our choosing.

Note

Note that we are capturing video in 3GP format for Android while it was in MP4/MOV format on the iPhone.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190