<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>maclema's blog</title>
	<link>http://maclema.com/b</link>
	<description>random computer stuff</description>
	<pubDate>Fri, 09 May 2008 08:57:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>AS3 - Working with Sound.</title>
		<link>http://maclema.com/b/2008/05/09/as3-working-with-sound/</link>
		<comments>http://maclema.com/b/2008/05/09/as3-working-with-sound/#comments</comments>
		<pubDate>Fri, 09 May 2008 08:45:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[AIR]]></category>

		<category><![CDATA[Actonscript 3]]></category>

		<category><![CDATA[Flash]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://maclema.com/b/2008/05/09/as3-working-with-sound/</guid>
		<description><![CDATA[The other day I was writing a small AIR mp3 player for myself that I could use for simply putting on shuffle and letting play all day. All that I wanted was to create a small AIR app that I could load my mp3 library into, read the ID3 tags, and implement my own custom [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was writing a small AIR mp3 player for myself that I could use for simply putting on shuffle and letting play all day. All that I wanted was to create a small AIR app that I could load my mp3 library into, read the ID3 tags, and implement my own custom search function. Once I had it all running I opened it up and let it scan my Music folder for mp3&#8217;s. Of course, loading the ID3 info for 3000+ mp3 files took a little while so I just left my computer and came back a little while later. When I came back my computer had been rendered almost useless! When I looked at the Activity Monitor it was showing that &#8220;adl&#8221; was using almost 1.5GB of my ram!</p>
<p>So naturally I thought to myself, &#8220;Geesh, stupid me. I must of left my event listeners on the Sound class&#8221;, which would mean that the Flash Player would not garbage collect them properly, thus leaving all the sound data in memory. After fixing this issue I let app scan my library again. Once again, rendering my computer useless.</p>
<p>After a long time searhing the internet for blog posts, documentation etc on this issue I came across nothing that showed me how to &#8220;unload&#8221; a sound from memory. Once the sound was loaded, it seemed that it was in memory until you closed your application. After finding nothing to help me out, I started to change my code around, trying different methods of loading / reading the ID3 info. Then I stumbled upon this, <a href="http://www.gskinner.com/blog/archives/2006/06/understanding_t.html">&#8220;</a><span><a href="http://www.gskinner.com/blog/archives/2006/06/understanding_t.html">the delete keyword deletes the actual variable definition, not just the variable&#8217;s value. This of course frees any reference it was holding, potentially freeing that object for garbage collection&#8221;</a>. </span></p>
<p>So, I <a href="http://maclema.com/b/wp-content/uploads/2008/05/soundmanager.as" title="SoundManager.as" target="_blank">created a class</a> that dynamically created properties to hold loaded Sound classes, and used the delete keyword to remove or &#8220;unload&#8221; the sounds. Bingo! The sounds were now unloading properly and my app then only started to consume 30 - 40 MB of ram while scanning my library.</p>
<p>Here is the key to the solution:</p>
<pre><code>
private var dynamicProps:Array = [null, null];

private function get sound():Sound {
	return dynamicProps[0];
}

private function set sound(value:Sound):void {
	destroySoundChannel();
	destroySound();

	System.gc(); //force garbage collection

	dynamicProps[0] = value;

	addSoundListeners();

	dispatchEvent(new Event("soundChanged"));
}

private function destroySound():void {
	if ( dynamicProps[0] != null ) {
		//cancel any remaining downloading
		try {
			Sound(dynamicProps[0]).close();
		} catch ( e:Error ) {}

		removeSoundListeners();

		delete dynamicProps[0];
		dynamicProps[0] = null;
	}
}

private function destroySoundChannel():void {
	if ( dynamicProps[1] != null ) {
		SoundChannel(dynamicProps[1]).stop();
		removeSoundChannelListeners();
		delete dynamicProps[1];
		dynamicProps[1] = null;
	}
}
</code></pre>
<p>Here is an example, of reading the ID3 tags of 100 mp3 files:</p>
<p align="center"><img src="http://maclema.com/b/wp-content/uploads/2008/05/bad.jpg" alt="Not Using Sound Manager" border="1" height="400" width="475" /><br />
Not using &#8220;SoundManager&#8221; to load mp3&#8217;s</p>
<p style="text-align: center"><img src="http://maclema.com/b/wp-content/uploads/2008/05/good.jpg" alt="Using Sound Manager" border="1" height="400" width="475" /><br />
Using the &#8220;SoundManager&#8221; class for loading mp3&#8217;s</p>
<p>Here is an example usage of SoundManager.as</p>
<pre><code>
var manager:SoundManager = new SoundManager();

private function loadSound(url:String):void {
	manager.addEventListener(Event.COMPLETE, soundLoaded);

	//we can call loadSound as many times as we like
	//everytime we call it the old sound is replaced by the
	//new loading sound.
	manager.loadSound(new URLRequest(url));
}

private function soundLoaded(e:Event):void {
	manager.play();
}

private function soundComplete(e:Event):void {
	loadSound("anotherSound.mp3");
}
</code></pre>
<p><strong>Files:</strong></p>
<p><a href="http://maclema.com/b/wp-content/uploads/2008/05/soundmanager.as" target="_blank">SoundManager.as </a></p>
]]></content:encoded>
			<wfw:commentRss>http://maclema.com/b/2008/05/09/as3-working-with-sound/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AlertBox With No Buttons! (HACK)</title>
		<link>http://maclema.com/b/2008/01/10/alertbox-with-no-buttons-hack/</link>
		<comments>http://maclema.com/b/2008/01/10/alertbox-with-no-buttons-hack/#comments</comments>
		<pubDate>Thu, 10 Jan 2008 11:14:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[AIR]]></category>

		<category><![CDATA[Actonscript 3]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://maclema.com/b/2008/01/10/alertbox-with-no-buttons-hack/</guid>
		<description><![CDATA[Browsing around EE today, I found an interesting question. This person wanted an AlertBox that had no buttons at all. The goal was to have the identical functionality / look as an alert, but to only display a message such as &#8220;Saving Changes&#8230;&#8221;. This is something that I do all over the place, and have [...]]]></description>
			<content:encoded><![CDATA[<p>Browsing around <a href="http://www.experts-exchange.com" target="_blank">EE</a> today, I found an interesting question. This person wanted an AlertBox that had no buttons at all. The goal was to have the identical functionality / look as an alert, but to only display a message such as &#8220;Saving Changes&#8230;&#8221;. This is something that I do all over the place, and have written custom components specifically for locking the UI until an operation is complete.</p>
<p>However, if you don&#8217;t mind using some nasty hacks, this functionality can be accomplished in only a couple lines of code.</p>
<pre><code>private var theAlert:Alert;

public function showAlert():void
{
  theAlert = Alert.show("Saving Changes...", "", Alert.OK);
  theAlert.mx_internal::alertForm.removeChild(
    theAlert.mx_internal::alertForm.mx_internal::buttons[0]);
}

public function hideAlert():void
{
  PopUpManager.removePopUp(theAlert);
}</code></pre>
<p>It&#8217;s a hack, but it works really good!</p>
]]></content:encoded>
			<wfw:commentRss>http://maclema.com/b/2008/01/10/alertbox-with-no-buttons-hack/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Keyboard Text Navigation in OSX.</title>
		<link>http://maclema.com/b/2007/12/16/keyboard-text-navigation-in-osx/</link>
		<comments>http://maclema.com/b/2007/12/16/keyboard-text-navigation-in-osx/#comments</comments>
		<pubDate>Sun, 16 Dec 2007 23:58:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[OS X]]></category>

		<guid isPermaLink="false">http://maclema.com/b/2007/12/16/keyboard-text-navigation-in-osx/</guid>
		<description><![CDATA[Having recently switched to a MacBook from Windows. I was becoming continually frustrated navigating text with the keyboard. I really missed my &#8220;DELETE&#8221; button from my old Acer. Finally I browsed around and found a bunch of useful keyboard shortcuts for OSX that make navigating text much easier.
- Func + Delete(Backspace): Deletes characters in front [...]]]></description>
			<content:encoded><![CDATA[<p>Having recently switched to a MacBook from Windows. I was becoming continually frustrated navigating text with the keyboard. I really missed my &#8220;DELETE&#8221; button from my old Acer. Finally I browsed around and found a bunch of useful keyboard shortcuts for OSX that make navigating text much easier.</p>
<p>- Func + Delete(Backspace): Deletes characters in front of the cursor. (Like the Windows Delete Key)</p>
<p>- Cmd + (Left | Right): Move to the beginning or end of the current line.</p>
<p>- Cmd + (Up | Down): Move to the beginning or end of the document.</p>
<p>- Opt + (Left | Right): Move the cursor to the beginning or end of the current word</p>
<p>- Opt + (Up | Down): Move the cursor to the beginning or end of the current paragraph.</p>
<p>- Opt + Delete: Delete to the beginning of the word.</p>
<p>Use shift with any of these combinations to select text.</p>
<p>Any others?</p>
]]></content:encoded>
			<wfw:commentRss>http://maclema.com/b/2007/12/16/keyboard-text-navigation-in-osx/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
