clarkk
clarkk

Reputation: 1

sound stream stops loading

I have a little sound stream script here, but sometimes if you press play to the next track before the current has done loading the next track doesn't comeplete loading

package player {
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.external.ExternalInterface;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    import player.Loader_bar;

    public class Stream extends Sprite {
        private var _Sound = null;
        private var _Channel = null;

        private var isLoading = false;

        private var _Loader_bar = null;
        public var loader_color = null;
        public var loader_width = 0;
        public var loader_height = 0;

        private var i = 0;

        public function Stream(){
            this._Loader_bar = new Loader_bar();
            addChild(this._Loader_bar);
        }

        public function cnstr(){
            this._Loader_bar.color = this.loader_color;
            this._Loader_bar.w = this.loader_width;
            this._Loader_bar.h = this.loader_height;
            this._Loader_bar.cnstr();
        }

        public function play(url){
            this.stop();
            this.close();

            this._Sound = new Sound();
            this.isLoading = true;

            this.addEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
            this._Sound.addEventListener(Event.COMPLETE, listener_loadedComplete);

            this._Sound.load(new URLRequest(url));
            this._Channel = this._Sound.play();
        }

        public function stop(){
            if(this._Channel){
                this._Channel.stop();
            }
        }

        private function close(){
            if(this.isLoading){
                this._Sound.close();
            }
        }

        private function listener_loadedComplete(event){
            this.close();

            this.isLoading = false;

            this.removeEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
        }

        private function listener_bytesLoaded(event){
            var float = this._Sound.bytesLoaded / this._Sound.bytesTotal;
            this._Loader_bar.progress(float);

            var data = {
                i : this.i,
                float : float,
                loaded : this._Sound.bytesLoaded,
                total : this._Sound.bytesTotal
                };
            ExternalInterface.call('swf2js', 'tst_progress', data);
            this.i++;
        }
    }
}

Upvotes: 1

Views: 674

Answers (1)

Ribs
Ribs

Reputation: 1505

close():void Closes the stream, causing any download of data to cease.

try this:

create a boolean like hasLoaded, set it to false. When the sound successfully loads set it to true.

Then when you play a sound you can test for hasLoaded in your play() function. If you have called play() before your previous sound has loaded, hasLoaded will be false, in which case you call this._Sound.close() before creating and loading the new sound. The reason for testing instead of just calling close() is so if you have paused the stream you don't have to reload it to play it again.

ADDITION:

Concerning the load not reporting properly, you have your progress logic set up incorrectly. Try this:

1) import flash.events.ProgressEvent

2) For the listener, replace this.addEventListener(Event.ENTER_FRAME, listener_bytesLoaded); in your play() method with this._Sound.addEventListener(ProgressEvent.PROGRESS, listener_bytesLoaded);

3) Change your listener_bytesLoaded() method to the following:

private function listener_bytesLoaded(event:ProgressEvent)
{
    var percent = event.bytesLoaded / event.bytesTotal;
    this._Loader_bar.progress(percent);

    var data = {
            i : this.i,
            float : percent,
            loaded : event.bytesLoaded,
            total : event.bytesTotal
            };

        ExternalInterface.call('swf2js', 'tst_progress', data);
        this.i++;
    }

4) Change this.removeEventListener(Event.ENTER_FRAME, listener_bytesLoaded); in your listener_loadedComplete() method to this._Sound.removeEventListener(ProgressEvent.PROGRESS, listener_bytesLoaded); and then move it out of that method and place it inside the conditional in the close() method.

NOTE - I didn't actually compile this but I think its good to go. Hope that helps. :)

Upvotes: 2

Related Questions