在flex中需要均衡器的解决方案吗?

时间:2013-10-21 21:50:14

标签: flex equalizer

我开发了一个小应用程序,用于测试flex中的5波段均衡器。我找到了此链接中的代码Flex Equalizer 均衡器工作正常。但是我在播放mp3文件时无法找到所需的位置。以下是我的代码....如何解决这个问题?

        import __AS3__.vec.Vector;

        import mx.events.FlexEvent;


        import spark.components.VSlider;
        public static const BUFFER_SIZE:int = 8192;
        public static const SAMPLE_RATE:int = 44100;
        private var _Position:Number=0;
        private var timer:Timer = new Timer(1000);

        private const _soundURI:String = "testfile.mp3";

        private var _out_snd:Sound = new Sound();
        private const _eqL:EQ = new EQ();
        private const _eqR:EQ = new EQ();
        private var _loop_snd:Sound = new Sound();
        // For storing dynamically created VSliders
        private const _sliders_vect:Vector.<VSlider> = new Vector.<VSlider>();
        private var _channel:SoundChannel = new SoundChannel();
        private var _samples:ByteArray;
        protected function view1_initializeHandler(event:FlexEvent):void
        {
            // TODO Auto-generated method stub
            timer.addEventListener(TimerEvent.TIMER,timerFunction);

            pgHolder.addEventListener(MouseEvent.CLICK,seekf);
            setupEQ();
            loadSound();
        }
        private function timerFunction(event:TimerEvent):void
        {       
            _Position = _channel.position;      
            pgHolder.value=_Position;
        }

        private function seekf(event:MouseEvent):void
        {

            _channel.stop();                
            _Position = (pgHolder.contentMouseX/pgHolder.width)*_loop_snd.length;   
            startPlay();
        }
        private function loadSound():void 
        {
            _Position=0;                
            _loop_snd.addEventListener(Event.COMPLETE, loadSoundComplete);
            _loop_snd.addEventListener(IOErrorEvent.IO_ERROR, loadSoundError);  
            _out_snd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
            _loop_snd.load(new URLRequest(_soundURI));
        }

        private function loadSoundComplete(event:Event):void 
        {
            pgHolder.minimum=0;
            pgHolder.maximum=_loop_snd.length;
            timer.start();                              
            startPlay();                
        }           

        private function startPlay():void 
        {
            _channel=_out_snd.play(_Position);  
        }

        private function loadSoundError(event:Event):void 
        {
            trace("loadError");
        }

        // Create Sliders for changing EQ gain
        private function setupEQ():void 
        {                
            var slider:VSlider;
            for (var i:int = 0; i < _eqL.gains.length; i++) {
                slider = new VSlider();
                slider.x = (i * 35);
                slider.y = 20;
                slider.value = .9;
                slider.maximum = 1;
                slider.snapInterval=0.1;
                slider.minimum = 0;
                slider.addEventListener("change", changeEQHandler);
                addElement(slider);
                _sliders_vect[i] = slider;
            }

            var event:Event = new Event("change");
            changeEQHandler(event);
        }    

        private function processSound(event:SampleDataEvent):void 
        {
            //trace("loading");
            if(_Position>=_loop_snd.length)
            {
                _channel.stop();
            }
            _samples = new ByteArray();             
            var len:Number = _loop_snd.extract(_samples,BUFFER_SIZE);
            var i:int=0;
            var l:Number;
            var r:Number;

            if ( len < BUFFER_SIZE ) { // If end of MP3, start over
                len += _loop_snd.extract(_samples,BUFFER_SIZE-len,0);
            }
            _samples.position = 0;
            trace("len" + len + "//" + _channel.position + "//" +_samples.length); 
            while (i < BUFFER_SIZE) {
                event.data.writeFloat(_eqL.compute(_samples.readFloat()));
                event.data.writeFloat(_eqR.compute(_samples.readFloat()));                  
                i++;
            }                   
        }           

        // Update EQ gains when sliders are changed
        private function changeEQHandler(event:Event):void 
        {    
            var i:int = 0;
            for(i = 0; i < _eqL.gains.length; i++){
                _eqL.gains[i] = _sliders_vect[i].value * 2;
            }

            for(i = 0; i < _eqR.gains.length; i++){
                _eqR.gains[i] = _sliders_vect[i].value * 2;
            }
        }



    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:HSlider width="100%" height="100%" id="pgHolder">

</s:HSlider>

1 个答案:

答案 0 :(得分:0)

当您处理滑块更改时,我看到您计算新的_Position,但_Position不可绑定且其从未使用过的set设置了玩家位置。

这只是伪代码,但你不应该这样做:

private function seekf(event:MouseEvent):void
{
    _channel.stop();                
    _channel.position = (pgHolder.contentMouseX/pgHolder.width)*_loop_snd.length;   
    _channel.start();
 }

只需查看代码,当您设置_Position = _channel.position时,您只需获取值,而不是引用。我的猜测是你需要设置_channel.position