Skip navigation
VikramRaman
Currently Being Moderated

How to change play position in sound class

Nov 11, 2013 8:38 PM

Tags: #change #play #in #position #sound_equalizer #equalizer

I have developed a small application for testing 5 band equalizer in flex. I found the code in this link Flex Equalizer The equalizer works fine.But i am not able to seek the desired position  while playing the mp3 file. If i try to change position. Its plays from the begining position only.How to solve this problem?

Following is my code....

 

 

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;
 

           
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>

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points