• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

What should I do when Sound.play() return null?

Explorer ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

I have a small webbase project that load and play some mp3 sound. My problem is sometimes sound.play() return null, not a SoundChannel object. I found Adobe documents say:

A SoundChannel object, which you use to control the sound. This method returns null if you have no sound card or if you run out of available sound channels. The maximum number of sound channels available at once is 32.

My question is: The maximum number of sound channels available at once is 32 in my application or my computer? In both case, I don't think are many sound channels run at one time. Just about 4-5 sound channels.

So, what should I do when sound.play() return null? Talk to user something like "Sound channels not available now. Please refresh your browser and try again."?

Or an other way to catch event when sound play complete, change volume or stop sound at any time?

Thanks for advance!

TOPICS
ActionScript

Views

650

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

Since about Flash 5 the maximum sound channels has been 8. I don't remember exactly when that happened, but it was a while ago. You can set new sound files to one of the existing sound channels as you like. But you have been restricted to a maximum of 8 channels that you can play at any one time.

Another reason that you will get a null result is if the sound file is not available. I don't think that is your problem in this case. The sound may not be available because it hasn't downloaded yet or there is a spelling or case error.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

Thanks for reply, Rob!

Since about Flash 9 the maximum sound channels has been 32, see Sound - Adobe ActionScript® 3 (AS3 ) API Reference. Don't controversy about that. The sound channels available more than what I need.

I'm sure use existing sound channels (global variables) to get sound channel return from sound.play() method. But, what I talking about is sound.play() methed return null in first time.

Other reason is file path? I'm sure it good, because I just test in my PPD or my localhost. And if something wrong in file path, error will appear in sound.load() method not in sound.play().

My code:

public class A

{

     private soundChannel:SoundChannel;

     private soundPlay:Sound;

     public function A()

     {

          soundPlay = new Sound();

          soundPlay.load...

          ...

     }

     ...    

     public function Play():void

     {

          // Sometimes soundPlay.play() return null

          soundChannel = soundPlay.play();

          // So line below error

          soundChannel.addEventListener(Event.SOUND_COMPLETE, this.onSoundComplete);

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Hi Chocolate,

Yes the reference says that there are a maximum of 32 channels. But the actual number of playable channels has been 8 for many years. The real maximum is dependent on the sound card in the user's computer. This means that the actual number of available channels can be lower than 8.

If the sound.play() returns null the first time but will play the sound later, then, I'm guessing, the problem is that the sound is not available yet because a sufficient amount of the file has not downloaded yet. Do you get different results when you test locally than you get when you test from a remote server? Are some sounds always available while others are not?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Thanks Rob!

I just tested on my old laptop (HP 6520s distribute in 2008). Sometimes load from local file, sometimes from real link on server by use SoundLoaderContext(5000, false). After sound open event (mean buffer sound data already loaded), I push interface to user. So, I think sound available when Play Button are clicked.

Maybe I will check SoundChannel before use. If SoundChannel are null then stop process and dispatch a message to user. E.g "Refresh your browser and try again!"

Thanks for help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

LATEST

One thing that I do is to not make the play mechanism available until the sound tests as ready. You could also track the sound's loading state and show that to the user.

Good luck in any case.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines