0

我有几个正弦的音频轨道。我想从一开始就打开一个“TitleMusic”,并允许用户打开和关闭其他轨道。我的代码就目前而言,“TitleMusic”从一开始就播放,所有其他曲目也在播放。我需要切换“track8”和所有其他轨道(未显示),以便它们在开始时关闭。这花了我很长时间才达到这一点,我只需要一些帮助来扭转它。谢谢

import flash.media.Sound;
import flash.media.SoundChannel;

var soundOn:Boolean = true;//This music is ON when we start
var myMusic:TitleMusic = new TitleMusic();
var myChannel1:SoundChannel = myMusic.play(0,1000);//endless loop, in effect

var soundOn3:Boolean = true; //music is ON when we start
var myMusic3:track8 = new track8();
var myChannel3:SoundChannel = myMusic3.play(0,1000); // endless loop, in effect
var myTransform3:SoundTransform;

mySoundButton3.addEventListener(MouseEvent.CLICK,toggleSound3);
mySoundButton3.buttonMode = true;
mySoundButton3.mouseChildren = false;

function toggleSound3(e:MouseEvent)
{
if(soundOn3)
{
// turn sound off

myTransform3 = new SoundTransform();
myTransform3.volume = 0; // silent
myChannel3.soundTransform = myTransform3;
soundOn3 = false;
mySoundButton3.myButtonText.text = "click to turn sound ON";
}
else // sound is off
{
// turn sound on

myTransform3 = new SoundTransform();
myTransform3.volume = 1; // full volume
myChannel3.soundTransform = myTransform3;
soundOn3 = true;
mySoundButton3.myButtonText.text = "click to turn sound OFF";
}

}
4

1 回答 1

0

你不能把这一行放在后面mySoundButton3.mouseChildren = false;

toggleSound3(null);

或者,为了提高内存效率,您可以这样做:

采取这一行:

var myChannel3:SoundChannel = myMusic3.play(0,1000); 

并将其更改为:

var myChannel3:SoundChannel; 

这使得您实际上并没有立即开始声音,而只是为它创建指针(var)

然后,在您打开块之后// turn sound on

if(!myChannel13){
    myChannel3 = myMusic3.play(0,1000); 
}

这会检查您是否已经启动了声音,如果没有,它会创建/启动声音

您还需要在声音关闭块中更改此行:

myChannel3.soundTransform = myTransform3;

对此

if(myChannel13){
    myChannel3.soundTransform = myTransform3;
}

这样,如果在 on 按钮之前单击 off 按钮,则不会引发错误。

于 2013-04-26T18:09:06.933 回答