Camera Tutorial Part 5: Start the camera from ActionScript

Opt In Image
Early bird offer on the ANE eBooks

 

Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.

 

 

  • step-by-step guide to making your iOS extension in under an hour
  • library for data conversion between ActionScript and native code
  • tutorials
  • infographics
  • code included

At the end of this part you will have

A confirmation of whether all of that code you piled up so far actually works. :)

Seriously, though, here is where you put the plumbing of your ANE together and start the camera from your test app. Dive in.

Time

5-6 minutes

Wait, have you done these first?

Step 1: Create an instance of CameraDelegate

Open CameraLibiOS.m and first include the CameraDelegate.h header file, then (naughtily) declare a global pointer to CameraDelegate at the top of the file:

Then add a function that will create a CameraDelegate instance for g_cameraDelegate to point to, when needed. Put that in your Auxiliary functions section, where you declared sendMessage() in Part 2.

Step 2: Get CameraDelegate to start the camera

In CameraLibiOS.m find the ASStartCameraPreview function you declared in Part 2 of this tutorial. It should look like this:

Replace the two lines in the function with code that will do actual work:

Step 3: Check your ActionScript call

In Flash Builder find your CameraTutorialAIRLib project and open CameraDriver.as. Make sure CameraDriver calls your native code – you added this in Part 3 of the tutorial, so it should already be there:

Step 4: Get notified when the camera starts

Time to do something useful in the onStatusEvent() handler you added in Part 3. Find onStatusEvent() and replace the trace() statement in it with the following:

Then define CAMERA_STARTED_EVENT public, so apps that use your ANE can listen for it:

Step 5: Make the call in your test app

Time to test whether that pile of code actually works. In Flash Builder open the test app project, CameraTutorialApp, you set up in Part 1 of the tutorial.

What? You skipped that part? No worries, you can set it up now or use an app you already have. Go on, do it – I’ll be here waiting.

Ready? The steps below refer to the files and functions in CameraTutorialApp – you can pretend it’s called something else if you are using your own app.

5.1. Open CameraTutorialAppHomeView.mxml and inside the <fx:Script> section first include the CameraDriver class:

Then add a CameraDriver variable:

5.2. Create the CameraDriver instance and hook to its CAMERA_STARTED_EVENT.

Remember the ensureCameraDriver() placeholder function you added in Part 1 of this tutorial? Let’s get it to work:

A good place to call ensureCameraDriver() is the view’s creationComplete handler:

5.3. Implement onCameraStarted():

As you may have noticed, your work here is not yet done. Once the camera has started, your test app can begin requesting video frames: that will be your objective in Part 7: Pass video frames to ActionScript.

5.4. Find the btnStart click handler that you added in Part 1 of the tutorial and get it to start the camera preview:

6. Run!

No, don’t run from this tutorial, run your app and see if it receives CAMERA_STARTED_EVENT. You should see the Start button go disabled and the Stop button become enabled when that happens.

If you get errors, crashes or other dodgy behaviour instead, post a comment below and let’s see if we can sort it out together. Details and excerpts from your code would be very helpful. (OK, my code – we can agree that when it doesn’t work, it’s my code.)

Wait, want more features and Android support?

Check out the DiaDraw Camera Driver ANE.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">