开发者

Differences between Sound.length and SoundChannel.position in ActionScript 3

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.m开发者_运维技巧edia.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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜