views:

442

answers:

2

Hi

Can anybody tell me why or what I have to do to fix the following issue?

I load a song and when I get the length the song never reach this value.

Here is issue document by another guy AS3 – SoundChannel.position never reaches Sound.length

And here is my code


import flash.display.Sprite;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.events.IOErrorEvent;


var snd:Sound = new Sound();
var channel:SoundChannel;
var statusTextField:TextField  = new TextField();


    statusTextField.autoSize=TextFieldAutoSize.LEFT;

    var req:URLRequest=new URLRequest("http://localhost/chance_teaser.mp3");

    try {
        snd.load(req);

        channel=snd.play();
    } catch (err:Error) {
        trace(err.message);
    }

    snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
    addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);

    this.addChild(statusTextField);


function enterFrameHandler(event:Event):void {
    var loadTime:Number=snd.bytesLoaded/snd.bytesTotal;
    var loadPercent:uint=Math.round(100*loadTime);
    var estimatedLength:int = Math.ceil(snd.length / (loadTime));
    var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength));

    statusTextField.text = "Sound file's size is " + snd.bytesTotal + " bytes.\n" 
                                       + "Bytes being loaded: " + snd.bytesLoaded + "\n" 
                                       + "Percentage of sound file that is loaded " + loadPercent + "%.\n"
                                       + "Sound playback is " + playbackPercent + "% complete. \n"
                                       + "Lengh of sound is " + snd.length + "\n"
                                       + "Pos of sound is " + channel.position + "\n";
}

function errorHandler(errorEvent:IOErrorEvent):void {
    statusTextField.text="The sound could not be loaded: "+errorEvent.text;
}

function soundCompleteHandler(event:Event):void {
    statusTextField.text = statusTextField.text + "\n The sound has finished playing.";
    removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
A: 

This is a known issue in Flash, if you google around you will find several forum threads on this. As far as I know you will either have to roll your own or find a library that does this for you.

There are several projects that do handle audio (properly). Here's a list of them.

The first one you might to look into is the popforge open source audio library.

Matti
Thanks friend! I´m taking a look right now.
Deryck
A: 

did u check if the bit-rate of the song is same as your publish bit-rate in flash file settings?

AdityaGameProgrammer
Well, not I don´t check this. Thanks for the tip.
Deryck