media.captureVideo()

Type Function
Library media.*
Return value None
Revision 2014.2450
Keywords media, camera, photo library
Sample code /CoronaSDK/SampleCode/Media/Camera
/CoronaSDK/SampleCode/Media/PhotoPicker
See also media.hasSource()

Overview

Opens a platform-specific interface to the device's camera. This function is asynchronous, i.e. it returns immediately so the calling code will continue to execute until the end of its scope; after that, the application will be suspended until the session is complete. A listener is required to handle the returned display object (URL). The returned URL can be used in native.newVideo() and media.playVideo().

Gotchas

Windows and Mac

The Corona Simulator for Windows and Mac does not support this API and it will do nothing when calling this function.

Android

In order to use the camera, you must set the following permissions in your "build.settings" file. The external storage permission is required even if your app is not writing the camera shot to file, because Android's camera app needs permission to save its camera shot to public storage to make it available to your app.

settings =
{
    android =
    {
        usesPermissions =
        {
            "android.permission.CAMERA",
            "android.permission.WRITE_EXTERNAL_STORAGE",
        },
    },
}

The preferredMaxDuration setting might not work on all devices. In that case, the user can take a video for as long as they want. You can check the value of the video length to get the actual length and size of the recorded video.

This function does not work on the internal Corona camera activty and will have to launch an external camera activity instead.

Syntax

media.captureVideo ({ listener = myListener, preferredQuality = myQuality, preferredMaxDuration = myMaxDuration, })
listener (required)

Listener. Can be either a function listener or a table listener. If a table, it must have a completion method. The event dispatched to the listener will be a completion event with the following additional properties:

  • event.url is a String which is the URI of the video. If the operation was cancelled, this value will be nil.
  • event.completed will be true if the user captured a video; false if the user cancelled the video capture.
  • event.duration will be the number of seconds of the video if the user captured a video; nil if the user cancelled the video capture or the value could not be obtained.
  • event.fileSize will be the number of bytes of the user captured a video; nil if the user cancelled the video capture or the value could not be obtained.
preferredQuality (optional)

Listener. A string indicating the quality of the video you want to take. The following are acceptable values:

  • high
  • medium
  • low

The default value is low. On Android, there is no medium quality level and if thats the value passed, low will be used instead.

preferredMaxDuration (optional)

Listener. The maximum duration of the taken video in seconds. This setting might not work on all devices and it is advised to check against the event to verify.

Example

local onVideoComplete = function(event)
    print("video session ended")
end

local function onComplete(event)
    if event.completed then
        media.playVideo(event.url, media.RemoteSource, true, onVideoComplete)
        print(event.duration)
        print(event.fileSize)
    end
end

if media.hasSource( media.Camera ) then
    media.captureVideo( { listener = onComplete } )
else
    native.showAlert( "Corona", "This device does not have a camera.", { "OK" } )
end