3

I've created a few buttons in Flash. I'm trying to make it so that if you click one button, the audio starts playing for that button. If you click another button, the active audio stops and the new audio of the button you clicked last start playing.

Any help please?

fyrm
  • 192
  • 1
  • 8
Anil_ramnauth
  • 55
  • 2
  • 7

1 Answers1

2

What you're describing is actually quite easy to do.

First things first, I recommend importing the audio into your Flash project. Alternatively, there is a way to play it directly from an external file. This is beyond the scope of my answer, so if you need help on that, you should post a question specifically covering it.

Assuming you have imported the audio file into your Flash project's library, make an as3 instance of it. (Right click the file in the library, click Properties --> ActionScript [tab] --> [Check] Export for ActionScript & [Enter name in] Class)

Now, create a definition of the sound in your code. (Assuming your two sounds were named "mySound1" and "mySound2" in the Class field of the previous step.)

var mySound1:Sound = new mySound1();
var mySound2:Sound = new mySound2();

Now, define your sound channel.

var mySoundChannel:SoundChannel = new SoundChannel();

There are two alternate ways of stopping one sound and playing another. The first is to create one function that does both every time. The second method is to create two formulas, one for "play" and one for "stop". You will need to decide which method works best for you. I'll use the two-function method below:

function stopSound():void
{
   //This stops all sound in the sound channel. 
   //If there is nothing playing, nothing happens.
   mySoundChannel.stop();
}

//In this function, we create an argument that allows us to tell the function 
//what sound to we want it to play.
function playSound(soundname:String):void
{
   mySoundChannel = this[soundname].play(0, 0);
}

[Note, you can tweak the play() properties to meet your needs, doing things like starting in the middle of the song, or looping it forever. 0,0 starts at the beginning, and doesn't loop. See the documentation for this.]

Now you hook up the event listeners for the buttons. (If you need help with event listeners, read the documentation.)

myButton1.addEventListener(Mouse.CLICK, btn1Click);
myButton2.addEventListener(Mouse.CLICK, btn2Click);

function btn1Click(evt:Event):void
{
   stopSound();
   playSound(mySound1);
}

function btn2Click(evt:Event):void
{
   stopSound();
   playSound(mySound2);
}

This should be enough information to get you started. In my game core, I actually have a custom class for dealing with sound playback that gives me the ability to repeat sounds, change volume, and keep sounds from conflicting with each other. I say that to emphasize that you can do quite a bit with the sound class. Do some digging in that documentation for ideas and help.

You may also consider putting a try-catch statement in the playSound function, since it will throw an reference error if you pass a name for a sound that doesn't exist.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
  • thank you so much for your help but this might sound crazy as im new to using flash, im a bit confuse as how to do the steps above as much as it sounds straight forward to you, is it possible that i can call you or teamviewer into my computer and show me how to do one button? please advise or just give me step by step as what to click after what – Anil_ramnauth Oct 31 '12 at 16:22
  • Unfortunately, I won't be back until later today. I would be more than willing to help in this case, however (and privately so, since StackOverflow is not the best place for teaching flash basics). Drop me a message through my blog's contact form (www.indeliblebluepen.com) and I'll see if I can give you a more detailed walkthrough, and some resources. I was in your shoes about a year ago, so I can relate. – CodeMouse92 Oct 31 '12 at 16:26
  • is there a way i can have a pause button or press a key to pause any of the audio that's playing on the above code you sent that im currently using? – Anil_ramnauth Nov 08 '12 at 19:50
  • You will want to create a new question to ask that. – CodeMouse92 Nov 08 '12 at 19:54
  • (Reference link to his new question: http://stackoverflow.com/questions/13296834/action-script-to-add-a-pause-button-on-a-few-audio-buttons/13297385#13297385) – CodeMouse92 Nov 08 '12 at 20:38