views:

489

answers:

2

Hi,

I have a flash player that has a set of songs loaded via an xml file.

The files dont start getting stream until you pick one.

If I quickly cycle through each of the 8 files, then flash starts trying to download each of the 8 files at the same time.

I'm wondering if there is a way to clear the file that is being downloaded. So that bandwidth is not eaten up if someone decides to click on lots of track names.

Something like mySound.clear would be great, or mySound.stopStreaming..

Has anyone had this problem before?

Regards,

Chris

A: 

If you do something like:

MySoundObject = undefined;

That should do it.

Jon
+1  A: 

Check out Sound.Close().

From the docs: "Closes the stream, causing any download of data to cease. No data may be read from the stream after the close() method is called."

This is the source code example from the linked docs:

package {
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;    
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.MouseEvent;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;

    public class Sound_closeExample extends Sprite {
        private var snd:Sound = new Sound();
        private var button:TextField = new TextField();
        private var req:URLRequest = new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");

        public function Sound_closeExample() {
            button.x = 10;
            button.y = 10;
            button.text = "START";
            button.border = true;
            button.background = true;
            button.selectable = false;
            button.autoSize = TextFieldAutoSize.LEFT;

            button.addEventListener(MouseEvent.CLICK, clickHandler);

            this.addChild(button);
        }

        private function clickHandler(e:MouseEvent):void {

            if(button.text == "START") {

                snd.load(req);
                snd.play();        

                snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

                button.text = "STOP";
            }
            else if(button.text == "STOP") {

                try {
                    snd.close();
                    button.text = "Wait for loaded stream to finish.";
                }
                catch (error:IOError) {
                    button.text = "Couldn't close stream " + error.message;    
                }
            }
        }

        private function errorHandler(event:IOErrorEvent):void {
                button.text = "Couldn't load the file " + event.text;
        }
    }
}
HanClinto