media.selectVideo()

Type Function
Library media.*
Return value None
Revision 2014.2374
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 photo library. 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.

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.

iOS

The iPad requires additional parameters, origin and permittedArrowDirections, to specify the location and direction of the popover used to select the photo.

Syntax

media.selectVideo ({ listener = myListener [, mediaSource] [, origin] [, permittedArrowDirections] })
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 camera selection.
  • event.duration will be the number of seconds of the video if the user selected a video; nil if the user cancelled the video selection or the value could not be obtained.
  • event.fileSize will be the number of bytes of the user selected a video; nil if the user cancelled the video selection or the value could not be obtained.
mediaSource (optional)

Constant. Can be one of the following:

  • media.PhotoLibrary
  • media.SavedPhotosAlbum
origin (optional)

Table. The rectangle of the button which the iPad's popover emerges from. A convenience programming pattern is to pass the contentBounds of your button. (Only available on iPad.)

media.selectVideo( { listener = sessionComplete, origin = myButton.contentBounds } )
permittedArrowDirections (optional)

Table. An optional field that is an array of allowed directions the iPad's popover arrow may point. Valid values are "up", "down", "left", "right", "any". The default is "any". (Only available on iPad.)

media.selectVideo( { listener = sessionComplete, origin = myButton.contentBounds, permittedArrowDirections = { "up", "down" } } )

Example

local function onComplete(event)
    if event.completed then
        media.playVideo(event.url)
    end
end

if media.hasSource( media.PhotoLibrary ) then
   media.selectVideo( { listener = onComplete } )
else
   native.showAlert( "Corona", "This device does not have a photo library.", { "OK" } )
end
-- Examples using Photo Library for iPad

-- Completion listener
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

local button = display.newRect(120,240,80,70)

-- Button tap listener
local function pickVideo( event )

    -- Note: Only use one of the media.show routines listed below

    -- Show photo on screen (no file save)  
    media.selectVideo(
        {
            mediaSource = media.SavedPhotosAlbum,
            listener = onComplete, 
            origin = button.contentBounds, 
            permittedArrowDirections = { "right" } 
        } )
end

button:addEventListener("tap", pickVideo )