Jump to content
Search Community

Prevent audio from playing over and over on click

obi_wan test
Moderator Tag

Recommended Posts

Hey greensock folks, I have a rather simple problem that I just cant seem to get right.

 

I have several buttons that play different sounds. When you click on the buttons they all play the appropriate sound. But when you click on the buttons really fast you can hear the sounds overlapping each other briefly. Basically if the sound is not playing, play the sound else nothing.

 

Below please view the code I have attached. Any help would be greatly appreciated.

 

/**

* @param audioURL

* @param whatToLoad

*/

public function playAudio(audioURL:String, whatToLoad:Object):void

{

if(!_audioPlaying)

{

_audioPlaying = true;

whatToLoad = new MP3Loader(audioURL, {name:"sound_fx", autoPlay:false, onComplete:startAudio});

whatToLoad.load();

}

}

 

private function startAudio(event:LoaderEvent):void

{

event.currentTarget.gotoSoundTime(0,true);

_audioTimer = new Timer(550);

_audioTimer.addEventListener(TimerEvent.TIMER, onTimer, false,0,false);

_audioTimer.start();

}

Link to comment
Share on other sites

I don't see where you are setting _isPlaying to true or where you are declaring its initial value or creating it so I have to assume it always has to be false and thus you will always load and play a new sound.

 

Also it is not clear what the Timer is doing.

 

I think you have to find a way to tell your button what it's sound is when you create the button then when you click to play the sound you should check to see if the sound has loaded and whether or not it is playing.

 

you can use the LoaderStatus and playProgress of the MP3Loader to determine both.

 

consider the following example:

 

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.events.MouseEvent;

var mySound:MP3Loader = new MP3Loader("sampleSound.mp3");

play_btn.addEventListener(MouseEvent.CLICK, playSound);

function playSound(e:MouseEvent):void{
trace("\nmySound.status = " + mySound.status); 
       //0 - READY
       //1 - LOADING
       //2 - COMPLETED


//if the sound hasn't fully loaded then try to load it

if(mySound.status != LoaderStatus.COMPLETED){

	mySound.load();
	trace("    ***    the sound has not yet loaded. it will autoPlay as soon as it can    ****" )

	} else {

		trace("sound is fully loaded");


		//only play the sound if the soud is done playing

		if(mySound.playProgress == 1){

			trace("I will allow the sound to play");

			mySound.gotoSoundTime(0, true);

			}else{

				trace("\n     ****     Sorry the sound is currently playing,    ****");
				trace("\n     ****     Please wait " + (mySound.duration - mySound.soundTime) + "  seconds");

				}

		}	



}

 

attached is a sample file that illustrates the basic concepts of a sound

- only loading if it hasn't yet loaded.

- only playing if it is not currently playing.

 

feel free to implement this into your project's design as you see fit.

 

more on LoaderStatus: http://www.greensock.com/as/docs/tween/ ... tatus.html

Link to comment
Share on other sites

  • 3 weeks later...

I appreciate the help but when I try implementing your example I still get the audio playing over each other. Here is what I used based on your help.

 

This is the event listener that fires the method in my sound manager
_item1.addEventListener(MouseEvent.CLICK,itemHandler,false,0,false);

private function itemHandler(event:MouseEvent):void
{
   _soundManager.playAudio(_soundManager.item1AudioURL,_soundManager.mp3Loader);
}

This is the code inside of my sound manager class that I updated to reflect your changes

	public var mp3Loader		:MP3Loader;
	public var gameSound		:Object;
               public var item1AudioURL	:String;

               item1AudioURL 	=  _xmlsource.levelItem1.@url;

	public function playAudio(audioURL:String, whatToLoad:Object):void
	{
		whatToLoad 		= gameSound;
		_audioPlaying		= true;
		gameSound		= new MP3Loader(audioURL, {name:"game_sound", autoPlay:true});

		if (gameSound.status != LoaderStatus.COMPLETED)
		{
			gameSound.load();
		}
		else
		{
			if (gameSound.playProgress == 1)
			{
				gameSound.gotoSoundTime(0, true);
			}
			else
			{
				trace("\n     ****     Sorry the sound is currently playing,    ****");
				trace("\n     ****     Please wait " + (gameSound.duration - gameSound.soundTime) + "  seconds");
			}
		}
	}

Link to comment
Share on other sites

I still get the audio playing over each other

 

that is because you are creating a new MP3Loader as soon as you call playAudio();

 

gameSound      = new MP3Loader(audioURL, {name:"game_sound", autoPlay:true});

 

as soon as you do that, gameSound will not have a status of COMPLETE, nor will its playProgress == 1.

 

perhaps i misunderstood your original question, as my example checks the status and playProgress of a single sound.

 

you will have to first check the existence of the loader with LoaderMax.getLoader(audioUrl) and see if that loader already exists... if so, then check to see if it has loaded and is playing. if the loader does not yet exist... create it and tell it to play.

 

in short your audioManager needs to create and manage multiple loaders for multiple sounds. right now your code is re-using the same loader over and over and rewriting its properties.

Link to comment
Share on other sites

Hi Carl thanks for all your help. Below is what I used that finally worked based on your feed back. I don't neccessarily think I did it the best way, seems slightly repetitive on my part due to lack of as3 chops but it works and when my schedule frees up a bit I can spend a little more time making this better. Again thanks for your help.

 

 

public function playAudio(audioURL:String, whatToLoad:Object):void
{
whatToLoad = gameSound;
if(LoaderMax.getLoader(audioURL)== null)
{
	gameSound			= new MP3Loader(audioURL, {name:"game_sound", autoPlay:true});
	gameSound.load();
	trace(audioURL)
}
else
{
	if (gameSound.playProgress == 1)
	{
		gameSound = new MP3Loader(audioURL, {name:"game_sound", autoPlay:true});
		gameSound.load();
	}
}
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...