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.
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.
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