Tutorial: Capture/Select Photo and Video

Share on Facebook0Share on Google+1Tweet about this on TwitterShare on LinkedIn2

video-featCorona SDK has long offered the ability to load images, save images, and work with photos in the camera roll or photo album. Now, since Daily Build #1239, we’ve added the ability to load and record video as well. For this purpose, we’ve added two new API calls to the media library, as well as two new APIs for working with photos.

New Video APIs

The two new video-related APIs are:

Video Capture

Let’s look at a basic code sample for capturing video:

In this case, we simply call media.captureVideo() to bring up the operating system’s video dialog. When the user dismisses the screen, the listener function is called and the app can optionally “react” to the video, for example, play it back. You can also set a preferredQuality preference for the video which includes “low” and “high” for both iOS and Android, plus a “medium” option for iOS. The default is “low” and you should be aware of the potential size of recorded videos if you increase the quality. Finally, you can request a max duration (in seconds) via the preferredMaxDuration parameter — just note that it may not be respected on all Android devices.

Video Selection

To select a video from the album/camera roll, use the media.selectVideo() call, for example:

In this call, the optional mediaSource parameter lets you choose between the “PhotoLibrary” or the “SavedPhotosAlbum.”

Event Properties

Both of these APIs reveal basic info to the listener function (onVideoComplete) when the system’s dialog is dismissed:

  • event.url — path to video.
  • event.completed — true if the user decided to keep/use the video; false if the user cancelled the dialog.
  • event.duration — length of the video in seconds.
  • event.fileSize — size of the video file in bytes.

Once selected, you can play the video using either the media.playVideo() call or native.newVideo() call.

IMPORTANT: Videos are captured/selected in an area outside of your app’s sandbox and technically are accessed via a URL. Because of this, you need to specify media.RemoteSource in the playback API when playing the video.

New Photo APIs

As part of introducing these new API calls, we are deprecating the older media.show() API as well. Moving forward, we suggest that you convert to the newer photo-related APIs, including:

Photo Capture

The code to capture a photo is similar to video capture:

An additional optional parameter for media.capturePhoto() is the destination parameter. This determines what the API should do with the photo. If this parameter is not defined, the photo will be returned as a standard Corona display object which can be shown on the screen, inserted into a display group/container, etc. However, if you want to save the photo to the app’s sandbox, specify the destination parameter, for example:

Photo Selection

To read an image from the camera roll, use the media.selectPhoto() API call.

Event Properties

Both of these APIs reveal basic info to the listener function (onPhotoComplete) when the system’s dialog is dismissed:

  • event.target — this will be a display object if destination is not specified. If destination is specified, this property will be nil.
  • event.completed — true if the user decided to keep/use the photo; false if the user cancelled the dialog.

iOS Notes: Landscape Orientation, iPad

Landscape Orientation

Video is typically recorded in landscape orientation, but the iOS dialogs are primarily in portrait orientation. If you lock your app to landscape orientation, it will cause problems. To solve this, add the following four lines to your build.settings file in the iphone.plist table:


The Apple iPad uses a popover menu that usually originates from a UI button that is used to open the dialog. For the two select functions — media.selectPhoto() and media.selectVideo() — there are two optional parameters which specify where on the screen the button is and which direction the popover should happen.

  • origin — a typical approach is to pass in the content bounds of the button used to select the dialog.
  • permittedArrowDirections — the popover menu has an arrow that points from the popover to the button. This lets you specify the direction in which that arrow should point. Valid values are “up”, “down”, “left”, “right”, or “any”. Default is “any”.

Android Build Settings

To use photos and videos, Android requires the following permissions in the build.settings table:

In Summary

Remember that the photo and video dialogs behave like other native system objects. Thus, you can not overlay images or other Corona display objects on top of these dialogs, and they cannot be added to display groups or containers.

Ready to experiment? If you’re a Pro or Enterprise subscriber with access to daily builds, please get Daily Build #1239 or later and download the sample project from Dropbox. NOTE: The sample project is based on Graphics 2.0. You will need to center the image yourself with Graphics 1.0 and change how the text colors are set.

Share on Facebook0Share on Google+1Tweet about this on TwitterShare on LinkedIn2
Rob Miracle

Rob Miracle creates mobile apps for his own enjoyment and the amusement of others. He serves the Corona Community in the forums, on the blog, and at local events.

This entry has 22 replies

  1. Tim says:

    Pretty Cool…..What is the ETA on being able to render (display) a video file that we include in our build on an object during gameplay? I’m pretty sure I read somewhere that there would be a render video as texture type feature in Graphics 2.0?

    • Rob Miracle says:

      No ETA yet.

      • Max says:

        One more newbie question: is it possible to record screen video in Corona? Or will be possible – some sunny day? 🙂

        • Ruvim Kuzmik says:

          What do you mean screen video?


  2. Mo says:

    Cool stuff as usual! This is probably a stupid question but I was wondering if it will be possible to video screen capture the screen? Say you have a game running on the device and you want to capture the screen (or a section of the screen) in a video format. I know we can take a screenshot of the screen and save it to a file. But I am think about taking a video capture of the screen instead. I am not interested in video screen capture using a PC software..

    My guess if it is not possible, I could maybe take bunch of screenshots at “rapid” intervals and save each shots into the device. Then I can playback the files as a movie. Not great of course!

    Anyway I was wondering about that for a future app…

    Thanks for these great G2 tutorials. They will come VERY handy when I start working on apps based on the new graphic engine:)


    • Ruvim Kuzmik says:

      This will never be possible in Corona unless they remake the whole engine ( Robs words ).


  3. Mr Shaint says:

    Can you resize the media.playVideo in any way? On the iPhone 5, it leaves the top and bottom black bars.

    • Rob Miracle says:

      Not that I’m aware of.

  4. Teddy Engel says:

    Is it possible to record a video without sound ?

    • Rob Miracle says:

      Not that I’m aware of

  5. babzzz says:

    Hi Rob
    i was playing with this and realised that there is 1~2 pixels gap lines on each other sides.(left/right) on Ipad Retina ONLY
    It works perfectly on Iphone4
    Otherwise on iphone5 the picture taken is cropped Horizontaly.

    I’m using the http://coronalabs.com/blog/2013/09/10/modernizing-the-config-lua/


    PS : I’m centring the photo with :
    photo.x = centerX
    photo.y = centerY

  6. babzzz says:

    Nevermind i fixed the problem 😉

  7. Billy says:

    Is there documentation on how to work with the video once taken (specifically how to upload it)? The upload method doesn’t seem to play nice with media.RemoteSource and the URL.

    • Mike Kelly says:

      did you ever get a response? I want to upload to iCloud and be able call back from there. this only seems to work for video that’s already in the video library, and capturing doesn’t seem to send any video there. thanks!

  8. Don says:

    Hi! Is it possible to have a button in the camera view that opens up the photo library to pick a picture?

  9. Farjad says:

    Is there a way to know whether the selected photo is portrait or landscape orientation ? Both use same width x height, atleast on iOS, one is just rotated 90 degrees.

    • Rob Miracle says:

      It should load in with the right orientation and obj.contentWidth, obj.contentHeight should give you the proper image dimensions.

  10. anitha says:

    Is there any way to find the size of the photo that is being selected from the internal storage of a device?

    • Rob Miracle says:

      Not through the tools that Corona SDK currently offers. You might be able to use Corona Enterprise and get access to the meta data before you load the image using native calls.

  11. anitha says:


  12. Jorge says:

    I buld the sample code and run on android. I get the error message:

    media.selectPhoto() needs storage access to handle all possible file paths a 3rd party Gallery app might provide!

    This permission are in build.settings. This is a bug?

    • Rob Miracle says:

      Can you ask this in the forums please? We will probably need to see your code and code does not post well here in the comments.