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 complete ANE that starts the native camera, captures frames from it and finally stops it. Also a test app that takes the captured frames and displays them as live camera feed.
Time
6-7 minutes
Wait, have you done these first?
- Part 1: Create a test app – 15-20 minutes
- Part 2: Set up the Xcode project – 8-10 minutes
- Part 3: Set up the AIR Library – 8-10 minutes
- Part 4: Connect to the camera in Objective-C – 15-20 minutes
- Part 5: Start the camera from ActionScript – 5-6 minutes
- Part 6: Grab frames from iOS camera – 15-20 minutes
- Part 6A: Fake triple buffering – 5-7 minutes, optional
- Part 7: Pass video frames to ActionScript – 15-20 minutes
Step 1: Stop the native camera
What a surprise, we start by doing actual work!
In your Xcode project open CameraDelegate.m and add this method to the CameraDelegate class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
- ( void ) stopCamera { if ( NULL == m_captureSession ) { // The camera was never started, don't bother stpping it return; } // Make sure we don't pull the rug out of the camera thread's feet. // Get hold of a mutex with @synchronized and then stop // and tidy up the capture session. @synchronized( self ) { if ( [ m_captureSession isRunning ] ) { [ m_captureSession stopRunning ]; assert( NULL != m_videoOutput ); [ m_captureSession removeOutput: m_videoOutput ]; assert( NULL != m_cameraInput ); [ m_captureSession removeInput: m_cameraInput ]; // Allow the garbage collector to tidy up: m_cameraWriteBuffer = NULL; m_middleManBuffer = NULL; m_consumerReadBuffer = NULL; m_captureSession = NULL; m_camera = NULL; m_cameraInput = NULL; m_videoOutput = NULL; } } } |
You’ll want to call stopCamera() from outside the CameraDelegate class, so make it public by adding its signature to CameraDelegate.h. This header should now look like this:
1 2 3 4 5 6 7 8 9 10 11 |
#import <Foundation/Foundation.h> #import <AVFoundation/AVCaptureOutput.h> // Allows us to use AVCaptureVideoDataOutputSampleBufferDelegate @interface CameraDelegate : NSObject <AVCaptureVideoDataOutputSampleBufferDelegate> - ( BOOL ) startCamera; - ( void ) stopCamera; - ( const NSData * const ) getLastFrame: ( int * ) frameWidth height: ( int * ) frameHeight; @end |
Step 2: Expose the call to ActionScript
You have technically done that, when you implemented the ASStopCameraPreview() function in Part 4, where you connected your native library to the camera. All that’s left to do there now is to get it to call CameraDelegate‘s stopCamera() method. In your Xcode project open CameraLibiOS.m, find ASStopCameraPreview() and add the call to it:
1 2 3 4 5 6 7 8 |
FREObject ASStopCameraPreview( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] ) { if ( NULL != g_cameraDelegate ) { [ g_cameraDelegate stopCamera ]; } return NULL; } |
Step 3: Make sure your AIR Library makes the call
Hey, you implemented that in Part 3 of the tutorial, remember? In your AIR Library project, you should have this method in the CameraDriver class. Just make sure it’s there:
1 2 3 4 5 |
public function stopCameraPreview() : void { // This will call ASStartCameraPreview() in CameraLib_iOS.m m_extContext.call( "as_stopCameraPreview" ); } |
Step 4: Stop the camera from your app
In the app you made in the first part of the tutorial you should have a Stop button with a click handler function. Open your test app project and in CameraTutorialAppHomeView.mxml find that handler and make it call stopCameraPreview():
1 2 3 4 5 6 7 8 9 10 11 12 |
protected function btnStop_clickHandler( _event : MouseEvent ) : void { // 1. Get the ANE to stop the camera m_cameraDriver.stopCameraPreview(); // 2. Stop the refresh timer - we aren't expecting video frames any more m_refreshTimer.stop(); // 3. Update the UI btnStart.enabled = true; btnStop.enabled = false; } |
Step 5: What are you waiting for?
Go on, test it.
What’s next?
Next, give yourself a tap on the shoulder for sticking with this tutorial – that was a long drag, wasn’t it? No?
- When you tested your ANE you probably noticed that the camera preview doesn’t orient itself when you turn your phone or iPad around. In the next post we’ll make a list of improvements and upgrades you can do to the code to customize it for your own app.
- Here is the table of contents for the tutorial, in case you want to jump back.
Wait, want more features and Android support?
Check out the DiaDraw Camera Driver ANE.
Trabicolo
Hi, great tutorial, thanks!
Any chance to see the next chapter?
Hristo
Hi,
Thanks for the kind words, we’re happy to hear that the tutorial series have been helpful.
The last chapter has been on our TODO list for quite some time now, thanks for the reminder! We have been snowed under with work lately, but there will be an update to our tutorials soon, so stay tuned.
Greetings,
Hristo