Building a Native Extension | Part 1 – The Problem and the Plan

I’ve been writing a lot of native extensions lately.  The project I’m working on now requires all kinds of custom behavior.  It’s been an interesting ride.  I’ve learned a lot, for sure, particularly since I haven’t coded extensively with iOS or Android.  I’ve learned some things and I’d like to share them.  I won’t only be sharing the knowledge though, I also have a fully functional (and useful) native extension for everyone to use.

The Problem:

Changing the volume of a video while it’s playing in Flash is pretty easy, particularly if you’re using OSMF.  Just change MediaPlayer.volume, and if you’re running in a browser that’s it.  Sadly, when running on mobile that doesn’t produce the behavior that users are going to expect.

Both Android and iOS have a system wide media volume.  Pressing the hardware volume buttons while media is playing causes the media volume to be adjusted.  The volume is applied to the entire AIR application and overrides the volume of the AIR application.  This leads to a problem where the AIR application thinks that it is at full volume, but the system media volume is muted.  If you want to use a slider control in your application to control the audio (or have any sort of UI/logic that is based off of the volume) you’re in trouble as you will be representing the wrong volume.

The Solution:

Create a native extension that interacts with the native layer to modify the system media volume, used instead of the usual methods in AIR to change the volume.

The native extension must have two parts.  The first is a method that can be called in AIR that will adjust the system volume.  The second is the ability to notify AIR whenever the user changes the system volume, either by using the hardware volume buttons or by using a different application.

The Finished Product:

Building the native extension has five parts.  These parts are the ActionScript library, the iOS native code, the Android native code, and a default implementation written in ActionScript.  The next several blog posts will walk through each part, discussing how to build the code and any pitfalls to consider.

If you’d like to find out more about how the native extension was built, grab the entire source code here https://github.com/nweber/SystemVolumeNativeExtension and use it as a reference as you read through the next several blog posts.

If you’re only interested in grabbing the finished extension you can grab that here https://github.com/nweber/SystemVolumeNativeExtension/blob/master/Volume.ane.  Something to note, if you plan on using the extension on iOS you must compile the IPA (both debug and release) from a Mac.  You must also specify the location of the iOS SDK in the native extension settings for iOS in Flash Builder.  If you’d like more information on why that is or how to do it, see the part 3 blog post.

Continue to part 2

9 Comments

    Hey, thanks for this tutorial. I have been unable build this or any ANE directly in flash builder 4.6 , have you had any success doing it without the air command line?

    • by Antonio
    • 2:40 pm, April 23, 2012
    • Reply

      As far as I know you can’t build the ANE through Flash Builder.

      • by Nathan Weber
      • 3:32 pm, April 30, 2012
      • Reply

    Thank you for the ANE, very useful. Might it be possible to add panning to the ANE?

      Sorry, I don’t have any plans to add panning. You could always download the code from github and give it a go yourself though! I took a quick peek at the iOS documentation and it looks like the strategy that’s being used for volume won’t work for panning, so you’d have to find another way to do it.

      • by Nathan Weber
      • 6:56 am, July 13, 2012
      • Reply

        Thank you for your answer! Will see if I can find another strategy to enable panning.

    Awesome! Lov’n air and native extensions. Thanks so much for publishing this info.

Leave a Reply