PANC Interactive
  • Work
    • hardware >
      • GMK Taro Presskit
      • Campine
      • Minorca and Sebright >
        • Minorca and Sebright gallery
      • Bireme Info
    • Software >
      • Eagle Screech!
      • Crosstown Smash
      • Infiltration
      • Segreta
  • Code
    • Platformer tutorial
  • Blog
  • Store

How NOT to save video with Corona SDK

1/16/2014

0 Comments

 
So if you’ve spent any time with Corona, you know it’s pretty evolved as far as what you can do and the device options you have access to. Recording audio, video and random data, playing internet video and calling native webViews.

One really powerful function is the recording of video and saving it locally on a device. Corona has a tutorial for recording video, but it doesn’t explicitly discuss saving what you’ve recorded. A Corona SDK developer Eja has posted a really useful forum thread detailing how to save a video, with Corona, using json logic. I wanted to repost it to just make sure that it exists outside of the forums and people can find it later on.

http://forums.coronalabs.com/topic/43203-solved-saving-captured-video/#entry224933


And here's the code, for posterity:

local function copyVideoFile(videoPath,dstName,dstPath)
  local rfilePath=videoPath
  local wfilePath=system.pathForFile(dstName,dstPath)
  local rfh=io.open(rfilePath,"rb")
  local wfh=io.open(wfilePath,"wb")
  if not(wfh) then
    return false
  else
    local data=rfh:read("*a")
    if not(data) then
      return false
    else
      if not(wfh:write(data)) then
        return false
      end
    end
  end
  rfh:close()
  wfh:close()
  return true
end
local function onVideoComplete(event)
  if (event.completed) then
    local videoFileExtension=".mov"
    if (system.getInfo("platformName")=="Android") then
      videoFileExtension=".mp4"
    end
    local videoFilePath=string.sub(event.url,8,-1)
    local savedVideoFileName="video"..videoFileExtension
    local savedVideoDirectory=system.TemporaryDirectory

    if (copyVideoFile(videoFilePath,savedVideoFileName,savedVideoDirectory)) then

      --your video is now saved under (savedVideoDirectory) directory
      --and the filename of the video is (savedVideoFileName)
      --do whatever you need to do :)

      local function mediaPlayListener(event)
        print("video ended")
      end
      media.playVideo(savedVideoFileName,savedVideoDirectory,true,mediaPlayListener)
    end
  end
end
media.captureVideo({listener=onVideoComplete,preferredQuality="high",preferredMaxDuration=20})
0 Comments



Leave a Reply.

    Alex

    Lead dev at Panc Software and #strongtake generator 
    extraordinare

    Archives

    June 2018
    December 2017
    October 2017
    September 2017
    August 2017
    July 2016
    May 2016
    April 2016
    November 2015
    October 2015
    June 2015
    January 2015
    October 2014
    September 2014
    August 2014
    July 2014
    June 2014
    May 2014
    April 2014
    March 2014
    February 2014
    January 2014

    RSS Feed