增加和减少点唱机的音量(Actionscript 3)

时间:2014-03-09 14:00:09

标签: actionscript-3 flash actionscript flash-cs5 volume

所以我现在已经在这个自动点唱机任务上工作了一段时间。我偶然发现了一个与播放动画时改变自动点唱机音量有关的问题。 这是我试图使用的代码:

var st:SoundTransform = new SoundTransform();
st.volume = 0.3;
SoundMixer.soundTransform = st;

虽然这段代码本身运行得很好,但当我尝试将它添加到我的脚本时,它对音量级别没有影响。 (上面的代码没有添加到下面的代码中,因为它不起作用)

这是我的剧本:

import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.SoundTransform;

//Equalizer
var _sound1         :Sound          = null;
var _sound2         :Sound          = null;
var _sound3         :Sound          = null;
var _sound4         :Sound          = null;
var _sound5         :Sound          = null;
var _sound6         :Sound          = null;
var _sound7         :Sound          = null;
var _sound8         :Sound          = null;

//Volume

var _soundChannel   :SoundChannel   = null;
_soundChannel = new SoundChannel();

//Sanger
_sound1 = new Sound(new URLRequest("lyd1.mp3"));
_sound2 = new Sound(new URLRequest("lyd2.mp3"));
_sound3 = new Sound(new URLRequest("lyd3.mp3"));
_sound4 = new Sound(new URLRequest("lyd4.mp3"));
_sound5 = new Sound(new URLRequest("lyd5.mp3"));
_sound6 = new Sound(new URLRequest("lyd6.mp3"));
_sound7 = new Sound(new URLRequest("lyd7.mp3"));
_sound8 = new Sound(new URLRequest("lyd8.mp3"));


//Spille sanger
knapp1.addEventListener(MouseEvent.CLICK, spill1);
knapp2.addEventListener(MouseEvent.CLICK, spill2);
knapp3.addEventListener(MouseEvent.CLICK, spill3);
knapp4.addEventListener(MouseEvent.CLICK, spill4);
knapp5.addEventListener(MouseEvent.CLICK, spill5);
knapp6.addEventListener(MouseEvent.CLICK, spill6);
knapp7.addEventListener(MouseEvent.CLICK, spill7);
knapp8.addEventListener(MouseEvent.CLICK, spill8);

function spill1(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound1.play();
    skjermTekst.text = "Miami Nights - Ocean Drive";
}

function spill2(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound2.play();
    skjermTekst.text = "Likelike - So Electric";
}

function spill3(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound3.play();
    skjermTekst.text = "LazerHawk - Disco Planet";
}

function spill4(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound4.play();
    skjermTekst.text = "Garth Knight - Silent Strike";
}

function spill5(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound5.play();
    skjermTekst.text = "Mitch Murder - Terminator Theme";
}

function spill6(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound6.play();
    skjermTekst.text = "Dynatron - Stars of the Night";
}

function spill7(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound7.play();
    skjermTekst.text = "Electric Youth - The Best Thing";
}

function spill8(event:MouseEvent)
{
    SoundMixer.stopAll();
    _soundChannel = _sound8.play();
    skjermTekst.text = "Borgeoisie - Neon Black";
}

//Equalizer

addEventListener(Event.ENTER_FRAME, Update, false, 0, true)

function Update($e:Event):void
{
    _equalizer._eq1.gotoAndStop (Math.round(_soundChannel.leftPeak  * 3) ); 
    _equalizer._eq2.gotoAndStop (Math.round(_soundChannel.rightPeak * 5) );
    _equalizer._eq3.gotoAndStop (Math.round(_soundChannel.leftPeak * 7) ); 
    _equalizer._eq4.gotoAndStop (Math.round(_soundChannel.rightPeak * 9) );
    _equalizer._eq5.gotoAndStop (Math.round(_soundChannel.leftPeak * 11) ); 
    _equalizer._eq6.gotoAndStop (Math.round(_soundChannel.rightPeak * 13) );
    _equalizer._eq7.gotoAndStop (Math.round(_soundChannel.leftPeak * 15) );
    _equalizer._eq8.gotoAndStop (Math.round(_soundChannel.rightPeak * 17) );
    _equalizer._eq9.gotoAndStop (Math.round(_soundChannel.leftPeak  * 17) ); 
    _equalizer._eq10.gotoAndStop (Math.round(_soundChannel.rightPeak * 15) );
    _equalizer._eq11.gotoAndStop (Math.round(_soundChannel.leftPeak * 13) ); 
    _equalizer._eq12.gotoAndStop (Math.round(_soundChannel.rightPeak * 11) );
    _equalizer._eq13.gotoAndStop (Math.round(_soundChannel.leftPeak * 9) ); 
    _equalizer._eq14.gotoAndStop (Math.round(_soundChannel.rightPeak * 7) );
    _equalizer._eq15.gotoAndStop (Math.round(_soundChannel.leftPeak * 5) );
    _equalizer._eq16.gotoAndStop (Math.round(_soundChannel.rightPeak * 3) ); 
}

现在,添加了卷片段后,自动点唱机的音量保持不变。即使我将st.volume = 0.3更改为0到1之间的任何其他数字。

1 个答案:

答案 0 :(得分:0)

您可以大大优化您的代码,对于此任务,您不需要SoundMixer,您的声音和SoundChannels可以轻松访问。主要想法是及时管理一个SoundChannel

const soundKey:String = "sound";
const soundNameKey:String = "soundName";
var sounds:Dictionary = new Dictionary();
var mainChannel:SoundChannel;
var volume:SoundTransform = new SoundTransform(1);

//"Bind" your buttons to the corresponding Sound description
sounds[knapp1] = {"sound": new Sound(new URLRequest("path")), "soundName": "Miami Nights - Ocean Drive"};
//Add all your sounds
//sounds[knapp8] = ...;

//Add all knapp buttons to the one container
//myKnappContainer.addChild(knapp1); ...

myKnappContainer.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void {
    var soundInfo:Object = sounds[e.target];
    if (soundInfo != null) {
        if (mainChannel != null) {
            //Stop previous sound
            mainChannel.stop();
        }

        //Start a new
        mainChannel = Sound(soundInfo[soundKey]).play();
        //Apply current volume
        mainChannel.soundTransform = volume;

        //Display sound name
        skjermTekst.text = soundInfo[soundNameKey];
    }
}