Skip to content
An AIR native extension to interact with the system volume for iOS and Android devices.
Find file
Pull request Compare This branch is even with nweber:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
AndroidVolumeLib
IOSVolumeLib
VolumeDefault
VolumeLib
VolumeTest
build
README
Volume.ane

README

DESCRIPTION
An AIR native extension to interact with the system volume on iOS and Android devices.

EXPECTED BEHAVIOR

iOS
When changing the volume using the hardware volume keys the system volume popup will be displayed and the AIR application is notified of the new volume.  AIR is notified of the new volume each time a hardware button is pressed.
When changing the volume from AIR the system volume popup is displayed with the new volume.

Android
When changing the volume using the hardware volume keys the system volume popup will be displayed.  The AIR application will be notified of the new volume after the system volume popup disappears.  This means that the user can press the hardware volume buttons several times before AIR is notified of a volume change.
When changing the volume from AIR the system volume is changed, but the system volume popup is not displayed.

BUILDING THE ANE
Navigate to the "build" directory.
Be sure all files are up to date:

VolumeLib.swc
extension.xml
platformoptions.xml
android/libAndroidVolumeLib.jar
android/library.swf (extract from VolumeLib.swc)
ios/libIOSVolumeLiba.
ios/library.swf (extract from VolumeLib.swc)
default/library.swf (extract from VolumeDefault.swc)

Run this command:
"adt -package -storetype pkcs12 -keystore CERT.p12 -storepass XXXX -target ANE Volume.ane extension.xml -swc VolumeLib.swc -platform iPhone-ARM -C ios . -platformoptions platformoptions.xml -platform Android-ARM -C android . -platform default -C default ."
Note: The certificate is optional.  Flash Builder might complain about an unsigned ANE if the certificate is absent, but it will work just fine.

EXAMPLE APPLICATION
The VolumeTest project shows a very simple implementation of the native extension.  There is a video that plays back (to be sure there's sound), and a slider which responds to changes in system volume and initiates a change to the system volume when the thumb is moved.

INTEGRATING THE ANE
Note:
If you plan on supporting iOS you *must* build the IPA from a machine running OSX with the iPhone SDK installed.  The native extension uses libraries that AIR does not package by default, so you must be able to link them from the iPhone SDK.  Building the IPA from a Windows machine will not work.
You can build the Android APK from either an OSX machine or a Windows machine.

1. Add the Native Extension.
Right click on the project and go to "properties".
Navigate to "Flex Build Path" -> "Native Extensions".
Click "Add ANE", navigate to Volume.ane and select it.
Press "OK".

2. Add support for desired platforms.
Right click on the project and go to "properties".
Expand the "Flex Build Packaging" option.
If you want to support iOS, navigate to "Apple iOS" option.
	Be sure "enabled this target platform" is checked.
	Navigate to the "Native Extensions" tab.
	Be sure the check next to the native extension in the "Package" column is checked.
	Put the location of the iPhone SDK into the "Apple iOS SDK" input.  (The default location should be "/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk".)
	Press "Apply".
If you want to support Android, navigate to the "Google Android" option.
	Be sure "enabled this target platform" is checked.
	Navigate to the "Native Extensions" tab.
	Be sure the check next to the native extension in the "Package" column is checked.
	Press "Apply".
Press "OK".

3. Setup the controller.
The VolumeController is a singleton.  You can access the controller through VolumeController.instance.
Once you access the VolumeController.instance the systemVolume property should be checked to find out what the current systemVolume is when the native extension was created.
	volumeSlider.value = VolumeController.instance.systemVolume;
The systemVolume property will always reflect the current systemVolume, so you can check it at any time.

4. Responding to VolumeEvent.VOLUME_CHANGED.
Create the event listener function.  The volume property of the VolumeEvent holds the current system volume.
	VolumeController.instance.addEventListener( VolumeEvent.VOLUME_CHANGED, onVolumeChanged );
	private function onVolumeChanged( event:VolumeEvent ):void {
		trace( "System volume changed:", event.volume );
		volumeSlider.value = event.volume;
	}
When you receive this event the system volume has been changed by the user outside of the AIR application.
The VolumeEvent.volume property is a value between 0 and 1.
Update the AIR volume UI to reflect the new system volume.
In the above example code an HSlider is updated to reflect the value.

5. Change the system volume.
When the AIR application want to change the system volume call the VolumeController.setVolume() method.
	private function onSliderChange( event:Event ):void {
		var value:Number = volumeSlider.value;
		trace( "Set volume to:", value );
		VolumeController.instance.setVolume( value );
	}
The value passed to setVolume() should be between 0 and 1.
In the above example code the setVolume() method is called in response to the CHANGE event on an HSlider component.
Something went wrong with that request. Please try again.