[英]mute and unmute button as3 flash
我正在尝试创建一个静音和取消静音按钮。 我不希望任何播放/暂停按钮只是静音和取消静音。 到目前为止,我只创建了静音按钮,问题是我不知道要创建一个静音按钮。 我还希望静音图标将其外观更改为单击后取消静音。 非常感谢任何帮助,谢谢大家。(一些侧面的评论或解释也很好)
希望我不要要求太多。 到目前为止,这是我的代码
var soundReq:URLRequest = new URLRequest("assets/for_flash.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete (e:Event):void
{
sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteSound);
function muteSound(e:MouseEvent):void
{
SoundMixer.stopAll();
}
假设在您的mute_btn
实例中,您有一个子图标,其实例名称为muteIcon
并且该图标的存在可以直观地指示您的声音是否当前未静音(例如,带有斜线的圆圈键入图标),下方是扬声器图标。
然后将是代码:
//First, you need to assign the result of the sound.play function to the sound channel
function onComplete (e:Event):void{
channel = sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteBtnClick);
function muteBtnClick(e:MouseEvent):void
{
//check to see if the sound channel exists yet (in case you click the button before the sound loads, otherwise you'll get an error
if(!channel) return;
//toggle the icon's visibility
mute_btn.muteIcon.visible = !mute_btn.muteIcon.visible;
//now check and see if the icon is visible or not
if(mute_btn.muteIcon.visible){
//it is visible, so we should unmute the sound
channel.soundTransform = new SoundTransform(1); //1 is the volume level from 0 - 1
}else{
//it is not visible, so we should mute the sound
channel.soundTransform = new SoundTransform(0); //0 is no volume, 1 is full volumn
}
}
另外,为了提高效率,您可以更改此设置:
var channel:SoundChannel = new SoundChannel();
对此:
var channel:SoundChannel;
因为在该var中创建新对象是没有意义的,因为在执行channel = sound.play();
时会分配一个新对象channel = sound.play();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.