Nulls.net: Invalid use of Null Error : use, null, invalid, error :)

Flash ActionScript 3 – several sound tricks


Flash ActionScript 3 – several sound tricks  

Article by Predrag Koncar

1. Recently, I had a problem with sound card detection in AS3. Yes, there are still some sound-crippled devices that are able to play flash files, but they are causing problems when it comes to coding. This article is mainly targeted to those developers who would like to see their creations working on any flash-capable device.

System.capabilities.hasAudio is used for flash player capabilities, not for actual computer hardware, so it will return ‘true’ even on devices without sound card, or as Adobe states: “Specifies whether the system has audio capabilities. This property is always true.” , so it is basically unusable.

Fortunately, there is a workaround and it is pretty straightforward.

Let’s say that you want to play soundtrack at the beginning of your game and also perform the check that we are talking about here.The code would look like this:

// define a channelvar my_channel:SoundChannel = new SoundChannel();

// define a soundvar my_sound:soundtrack = new soundtrack();

// perform a checkif (my_sound.play(0, int.MAX_VALUE) != null){ my_channel = my_sound.play(0, int.MAX_VALUE);}

As you can see, the if condition at the end is checking if the sound you want to play is not null and only if it is not, it plays sound.The int.MAX_VALUE tells player to loop sound forever, you can set that to any other value that you might need.

In fact, you should use this if condition anywhere in the code where you want to play some sound. For example, if you want to play explosion sound that is named explode_snd, you would perform the following check:

if (explode_snd.play() != null){ explode_snd = my_sound.play();}

Explanation:The sound.play() method returns a SoundChannel object and on sound incapable devices it is set to null. We can access this even before the sound is actually played.

The full description of the flash.media.sound class can be found here.

2. If your sound file is too large and you don’t want it to be preloaded in the frame 1 (this can cause your preloader to wait for a while and then loading percent suddenly jumps to a high value), you should try this method. I know that more experienced developers are already using this or some other method, so this is not for them :) Let’s say that your sound is named ‘soundtrack’.

1. You should add new frame at the end of your timeline.

2. In library, right-click your sound file and select properties. Under linkage tick the ‘Export for ActionScript’ and un-tick the ‘Export in frame 1′ box. Also, under the ‘class’ write the name of your sound, in this case it is soundtrack. Click OK.3. Drag and drop your sound from the library to the last frame on the timeline (the one that you have created in the step 1)4. Write this code on that frame:

import flash.media.SoundMixer; SoundMixer.stopAll();var my_sound:soundtrack = new soundtrack();var my_channel:SoundChannel = new SoundChannel();

if (my_sound.play() != null){ my_channel = my_sound.play();}gotoAndPlay(x); // Erase x and type the number of the frame you want to jump after this

I hope you’ve found this article to be helpful and that it saved some development time for you.

About the Author

Predrag is a veteran in online games development and the owner of ActionShock.com and TikiPlay.com


Most Related Posts


htcell