mike chambers | about

Monitoring System Volume changes with Adobe AIR

Tuesday, February 24, 2009

I am at FITC Amsterdam this week, where I had a talk on Desktop Development with Adobe AIR. One of the things I showed was how to get notifications when new volumes / drives are added / removed to a machine. This could be useful if for example, you application needs to know when a new CD Rom or USB drive has been added or removed to the user’s system.

Anyways, I create a reusable class called VolumeMonitor that makes it very easy to monitor volume changes. I have checked in the code into the as3corelib library under the AIR package. Its not in the build yet, but once I write some docs and unit tests, and check for some bugs, Ill add it to the next build.

Here is an example of the code in use:

import com.adobe.air.filesystem.events.FileMonitorEvent;
import com.adobe.air.filesystem.VolumeMonitor;

private var monitor:com.adobe.air.filesystem.VolumeMonitor;

private function onApplicationComplete():void
{
	monitor = new com.adobe.air.filesystem.VolumeMonitor();
	monitor.addEventListener(FileMonitorEvent.ADD_VOLUME, onAddVolume);
	monitor.addEventListener(FileMonitorEvent.REMOVE_VOLUME, onRemoveVolume);
	monitor.watch();
	}

private function onAddVolume(e:FileMonitorEvent):void
{
	trace("Volume added : " + e.file.url);
}

private function onRemoveVolume(e:FileMonitorEvent):void
{
	trace("Volume removed : " + e.file.url);
}

You can check out the class by grabbing the as3corelib source. If you poke around you will notice some other classes that I have added to the air package, but Ill post about those later.

twitter github flickr behance