开发者

Loading and playing more than one .mp3 file

I'm trying to set up a .flv file that will have buttons for two different pieces of sound. I've been able to get the first piece to load and to play and stop, but when I try to load the second piece, I trip up. I'm still a novice, but do think I understand the basic problem -- Actionscript can't distinguish between one sound request and another. The question is, how to fix this and get actionscript to understand that I'm trying to load and play two different sounds associated with two different buttons? I get error codes such as "1151: A conflict exists with definition s in namespace internal." , "duplicate function" problems and "duplicate variable" warnings. I've seen some discussion on this site of a recyclable sound object. I think I'm asking a similar question, but although the person who posed that question also posted a solution, I haven't been able to make it work. Here's the code:

var s:Sound = new Sound();
s.load(new URLRequest("http://www.website.com/Water.mp3"));

wellsplay_开发者_如何学Cbtn.addEventListener(MouseEvent.CLICK,playSound);
wellsstop_btn.addEventListener(MouseEvent.CLICK,stopSound);

function playSound(e:MouseEvent):void
{
sc = s.play();
}

function stopSound(e:MouseEvent):void
{
sc.stop();
}

var s:Sound = new Sound();
 s.load(new URLRequest("http://www.website.com/Wells.mp3"));

wellsplay_btn.addEventListener(MouseEvent.CLICK,playSound);
wellsstop_btn.addEventListener(MouseEvent.CLICK,stopSound);

function playSound(e:MouseEvent):void
{
sc = s.play();
}

function stopSound(e:MouseEvent):void
{
sc.stop();
}

The rest of the commands work fine. It's only this section that I'm having trouble with. Thanks in advance for any suggestions.


Following on the comment you left for Tyler Egeto, giving a new name to a Sound instance means doing the following

 //The variables names are s1 & s2
 var s1:Sound = new Sound();
 var s2:Sound = new Sound();

The first "Sound" , after your variable name is your class name. AS3 is a strongly typed language meaning that you need to state the type of the variable you are declaring. In this case , it's an instance of the Sound class.

In order to play ( or stop ) a specific Sound instance, you will need to identify which button was clicked and react accordingly

 function playSound(e:MouseEvent):void
 {
     switch( e.currentTarget )
     {
          case wellsplay_btn:
           sc = s1.play();
           break;

          case waterplay_btn:
           sc = s2.play();
           break;
     }

 }


This is because you are declaring the variable var s:Sound twice, use different names for them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜