如何使用AS3中的相同按钮来循环和停止声音?
问题描述:
我有4个按钮在舞台上,我需要一个代码,使我可以点击其中的一个,循环播放特定的声音“无限”,当它再次点击时,声音停止。声音不是最初播放的。另外,当其中一个声音循环播放时,如果我按另一个按钮,我希望先前的声音停止播放,并播放新的声音。如何使用AS3中的相同按钮来循环和停止声音?
为了更好地展现这一点,我将解释我的项目。我必须创建一个像在线吉他调音器一样的“应用程序”。此功能是那种我想重现什么:
http://www.gieson.com/Library/projects/utilities/tuner/
我甚至不知道从哪里开始与编码...任何帮助是极大的赞赏。谢谢!
答
实际代码很大程度上取决于您在声音中的加载方式,因此我将为代码编写“框架”。
var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;
var sound1:Sound = /* Load me */
var sound2:Sound = /* Load me */
button1.addEventListener(MouseEvent.CLICK, playSound1);
function playSound1(event:MouseEvent)
{
playSound(sound1);
}
button2.addEventListener(MouseEvent.CLICK, playSound2);
function playSound2(event:MouseEvent)
{
playSound(sound2);
}
function playSound(sound:Sound):void
{
if (currentSound != null)
{
// Stop the current music
currentSoundChannel.stop();
}
if (currentSound == sound)
{
// Stop playing ANY sound
currentSound = null;
currentSoundChannel = null;
}
else
{
// Play a different sound
currentSound = sound;
currentSoundChannel = sound.play();
}
}
+0
你可以避免按钮和他们的听众重复的代码,但如果你是初学者,可能会更容易写出像我在例子中的东西。 – IQAndreas
自己做一个尝试......当你卡在某个地方时发布具体问题。对于初学者...尝试参考:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html – loxxy