Flash Player can only play 32 sounds at a time

In as3 you play a sound by creating a Sound object, and calling its play() method. play() returns a SoundChannel object which has a soundTransform property that you can use to adjust the volume and panning of the playing sound.
If you like, you can think of the Sound class as being the data, and the SoundChannel class as the actual playing sound.

A potentially important detail you may not be aware of (it is not mentioned in the API reference) is that Flash Player will only allow a maximum of 32 sound channels to play simultaneously. If you already have 32 active SoundChannels, and you try to call the play() method of a sound, it will return null, and the sound will not play. That's not 32 channels per swf btw, that's 32 channels per player. So if whoever is viewing your swf has other swfs open at the same time (in another tab, or another window) those other swfs could very well monopolize all 32 channels, leaving you with no ability to play sound.

Therefore, when attempting to play a sound it is important that you check to make sure you actually got access to a SoundChannel before you attempt any soundTransform operations.
If you do this:

var st:SoundChannel = mySound.play();
st.soundTransform = new SoundTransform(0.5);

there's a chance that mySound.play() will return null, and your movie will generate a runtime error.

Instead, you should do this:

var st:SoundChannel = mySound.play();
    st.soundTransform = new SoundTransform(0.5);
    //take alternate action if necessary

That way if mySound.play() does return null, your movie doesn't break.

Tags: Flash, sound


Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <pre> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options