Copy link to clipboard
Copied
I was looking at sample code from David Hassoun about how to play AAC sound files in Flash. His code in Flash works great. But the same code more or less, moved to an AIR Desktop project, fails completely, without any error message. A baffling situation. I am just trying to play a simple sound file, a trivial operation. I know the sound file exists, and can be found but nothing comes out of the speaker, and no messages from the stream system occur either.
Is this possibly due to an obsolete runtime of my AIR system? how does one locate the AIR version you are running? There are so many AIR runtimes sprinkled through my hard drive...
<pre>
public class SysSound extends Object
{
public static const TRACE_SOUND : Boolean = true;
public static var g_connection :NetConnection;
public static var g_stream :NetStream;
//================================
// on_metadata
//================================
// streaming video needs this kind of function
// callback function for meta data handling
private static function on_metadata( info:Object ):void
{
if (TRACE_SOUND) trace ("onMetaData was called!");
}
//================================
// play_external_aac
//================================
// play an external file (must specify the folder!)
// should be in m4a format
public static function play_external_aac (path : String) : void {
var fd :File;
fd = File.applicationDirectory;
fd = fd.resolvePath(path); // audio/xxxx.m4a");
if (TRACE_SOUND) trace ("about to play "+path+", exists="+fd.exists);
if (!fd.exists)
throw new ArgumentError ("can't find external sound "+path);
var customClient : Object = new Object();
customClient.onMetaData = on_metadata;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var my_stream:NetStream = new NetStream(nc);
my_stream.client = customClient;
my_stream.play(fd.url)
// to stop the sound call channel.stop()
} // end func
</pre>
Copy link to clipboard
Copied
the problem with this code is a classic Actionscript programming error; the object my_stream is allocated on the stack, and when the function returns it disappears. The reason Flash works, is that the code on Frame 1 is never deallocated, but in AIR, you gotta make the my_stream variable static otherwise it is deallocated before it even gets a chance to start playing. Sure wish adobe would catch this kind of error in the runtime or at the compiler level. What is very difficult to judge is which variables have to be declared as static? the customClient object is added as a reference to my_stream.client, so as long as my_stream is around, that object is marked as "used" by the garbage collector.... this whole issue of when to mark something as static vs. can be assumed dynamic is awfully frustrating.