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

Posted

技术标签:

【中文标题】增加和减少点唱机的音量(Actionscript 3)【英文标题】:Increasing and decreasing the volume of a jukebox(Actionscript 3) 【发布时间】:2014-04-12 13:10:38 【问题描述】:

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

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) ); 

现在,随着音量 sn-p 添加,点唱机的音量保持不变。即使我将 st.volume = 0.3 更改为 0 到 1 之间的任何其他数字。

【问题讨论】:

【参考方案1】:

您可以极大地优化您的代码,并且对于这项任务您不需要 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];
    

【讨论】:

以上是关于增加和减少点唱机的音量(Actionscript 3)的主要内容,如果未能解决你的问题,请参考以下文章

iPhone 音量间歇性/突然增加或减少

如何将设备音量值获取到我的应用程序

使用 SOX 按百分比值减少音频文件的音量

ActionScript 3 调整FLV视频的音量

ActionScript 3 音量控制AS3

Chromecast-音量进度条在电视上可见,但在我的 android 移动设备上不可见