我以为我可以更改布尔值true / false值,但它不起作用。在按下按钮之前,如何让它静音?
import flash.media.Sound;
import flash.media.SoundChannel;
var soundOn:Boolean = true; //music is ON when we start
var myToons:TitleMusic = new TitleMusic();
var myChannel:SoundChannel = myToons.play(0,1000); // endless loop, in effect
var myTransform:SoundTransform;
mySoundButton.addEventListener(MouseEvent.CLICK,toggleSound);
mySoundButton.buttonMode = true;
mySoundButton.mouseChildren = false;
function toggleSound(e:MouseEvent)
{
if(soundOn)
{
// turn sound off
myTransform = new SoundTransform();
myTransform.volume = 0; // silent
myChannel.soundTransform = myTransform;
soundOn = false;
mySoundButton.myButtonText.text = "click to turn sound ON";
}
else // sound is off
{
// turn sound on
myTransform = new SoundTransform();
myTransform.volume = 1; // full volume
myChannel.soundTransform = myTransform;
soundOn = true;
mySoundButton.myButtonText.text = "click to turn sound OFF";
}
}
答案 0 :(得分:1)
不需要布尔语句。这就是我所做的。
变量'mySound'实例化声音。我将声音控制权交给'myChannel'。组件面板中的按钮有名称。确保正确设置mp3属性,命名为'Sound,Class'Sound'。一切都应该有效!
播放并停止嵌入式mp3
/*
place mp3 in library.
Give it a Name and Class of 'Sound'
*/
var mySound:Sound = new Sound();
//control the channel that your sound is on
var myChannel:SoundChannel = new SoundChannel();
playButton.addEventListener (MouseEvent.CLICK, myPlayButtonHandler);
/*
Grab a Play and Stop button from the components menu.
Go to Properties Panel and give each an instance name.
Play is 'playButton', Stop is 'stopButton'
*/
function myPlayButtonHandler (e:MouseEvent):void {
//mySound.play();//use channel instead
myChannel = mySound.play();
}
//Stopping the sound channel
stopButton.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.stop();
}
加载外部mp3选项
//URL load and auto play of external file "myMp3.mp3"
var mySound:Sound = new Sound();
snd.load(new URLRequest("myMp3.mp3"));
snd.play();