actionscript-3 - ActionScript 3 Flash CS6-淡出音频

标签 actionscript-3 flash audio fadeout

我在菜单上播放此背景音乐。每当播放器选择“开始”按钮(startBtn)时,我都希望使其平滑淡出。你是怎样做的?
这是我的代码:

import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.SoundTransform;
import flash.events.MouseEvent;

//Declare MUSIC Variables
var musicSelection:Sound;
var musicChannel:SoundChannel;
var musicTrack:String = "music/dearly_beloved.mp3";
var musicVolume:Number=0.2;
/*var isMuted = false;*/

loadMusic();
function loadMusic():void 
{
    //first stop old sound playing
    SoundMixer.stopAll();
    musicSelection = new Sound();
    musicChannel = new SoundChannel();
    //create and load the required soun
    musicSelection.load(new URLRequest(musicTrack));
    //when loaded - play it
    musicSelection.addEventListener(Event.COMPLETE, musicLoaded);
}

function musicLoaded(evt:Event):void
{
    //finished with this listener
    musicSelection.removeEventListener(Event.COMPLETE, musicLoaded);
    //play music
    playMusic();
}

function playMusic():void
{
    //play the random or selected music
    musicChannel = musicSelection.play();
    //setting the volume control property to the music channel
    musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
    //but add this one to make repeats
    musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}

function playAgain(evt:Event):void
{
    // remove this listener and repeat
    musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
    playMusic();
}

这是第二帧的代码:
import flash.events.Event;
import flash.media.SoundTransform;

stop();

optionBtn.addEventListener(MouseEvent.CLICK, goToOption)
function goToOption(event:MouseEvent):void
{
    nextFrame();
}

startBtn.addEventListener(MouseEvent.MOUSE_DOWN, stopMusic);
function stopMusic(event:MouseEvent):void
{
    musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
    while (musicVolume > 0)
    {
        musicVolume--;
    }
}

quitBtn.addEventListener(MouseEvent.MOUSE_DOWN, closeGame);
function closeGame(event:MouseEvent):void 
{
    fscommand("quit");
}

最佳答案

使用while ...只会在一帧中立即降低音乐。您需要做的是添加ENTER_FRAME事件,然后在其中减少音乐。像这样的东西:

addEventListener(Event.ENTER_FRAME, onLoop);

var _oldTime:Number = 0;
private function onLoop(e:Event):void
{
   var resultTime:Number = getTimer() - oldTime;
   if(resultTime > 200 )
   {
      musicVolume -= 0.1;
      oldTime += resultTime;
      if(musicVolume <= 0) 
      {
        removeEventListener(Event.ENTER_FRAME,onLoop);
      } 
   }
}

根据所需的速度,尝试减小音量或减小音量的其他值(结果时间> 200-> 200ms)

关于actionscript-3 - ActionScript 3 Flash CS6-淡出音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13005075/

相关文章:

flash - 为什么动画有时会根据耗时使用步骤来完成?

c++ - 对应于 AS3 Number (Integral) 的 C++ 数据类型是什么

apache-flex - 在开始下载之前如何获取远程文件的大小?

php - 安装后如何更新 adobe air 应用程序?

java - AudioPlayer无法在使用Java 1.6的Mac上播放声音

android - com.googlecode.mp4parser无法获取mp3音频文件?

actionscript-3 - Flash 打印 - Chrome 在打印时忽略 Sprite

javascript - 使用 AS3 或 JS/Jquery 在 SWF 动画结束时执行 JavaScript 函数

flash - zClip 卡住 Firefox

audio - 想要将音频词保存在文本文件中