Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bf474b5fac
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 200 lines (166 sloc) 5.662 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
import flash.media.SoundTransform;

class Mp3Player extends flash.events.EventDispatcher, implements IPlayer {
    var File : flash.net.URLStream;
var sound : flash.media.Sound;
var channel : flash.media.SoundChannel;
    var pitch : Array<Float>;
    var buffer : Array<Array<Float>>;
    var padding : Array<Float>;
    var in_off : Array<Float>;
    var fname : String;
    var first : Bool;
    var trigger : Null<Float>;
    var pos : Null<Float>;
var playTimer : flash.utils.Timer;

    var schtr: SoundTransform;
    public var volume(getVolume, setVolume): Float;
    public var pan(getPan, setPan): Float;
    public var soundTransform(getST, setST): SoundTransform;

    public function new(?path : String) {
        super();
        schtr = new SoundTransform();
        fname = path;
        File = null;
    }

    public function play(?path : String, ?trigger_buffer : Float) {
// Now we don't use trigger_buffer variable anyhow. May be we don't need it
        if (path != null) fname = path;
        if (fname == null) throw "No sound URL given";

        trace("Mp3Player for " + fname);
        
try {
File = new flash.net.URLStream();
            var Req = new flash.net.URLRequest(fname);
            
sound = new flash.media.Sound();
sound.addEventListener(flash.events.IOErrorEvent.IO_ERROR, ioErrorHandler);

sound.load(Req);
dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, 0));
}
        catch (error : Dynamic) {
            trace("Unable to load: " + error);
            throw error;
        }

if (sound != null)
{
// Add the event listeners for load progress and load
// complete
sound.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler);
sound.addEventListener(flash.events.Event.COMPLETE, completeHandler);

// If there's a channel
if (channel != null)
{
channel.stop();

// Play the music
channel = sound.play(0);
}
else {

// Play the music
channel = sound.play(0);
if (this.schtr != null) {
this.channel.soundTransform = this.schtr;
}

// Add the event listener for sound complete
channel.addEventListener(flash.events.Event.SOUND_COMPLETE, stoppedEvent);

// Start a timer to show play progress, there's no
// play progress event
startPlayTimer();
}
}
}

    public function setVolume(volume: Float): Float {
        this.schtr.volume = volume;
        trace("mp3 setVolume(" + volume + ")");
        this.soundTransform = this.soundTransform; // Apply changes
        return volume;
    }

    public function getVolume(): Float {
        return this.schtr.volume;
    }

    public function setPan(pan: Float): Float {
        this.schtr.pan = pan;
        this.soundTransform = this.soundTransform; // Apply changes
        return this.schtr.pan;
    }

    public function getPan(): Float {
        return this.schtr.pan;
    }

    public function setST(st: SoundTransform): SoundTransform {
        this.schtr = st;
        if (this.channel != null) {
            this.channel.soundTransform = this.schtr;
        }
        return this.schtr;
    }

    public function getST(): SoundTransform {
        return this.schtr;
    }

function startPlayTimer() {
if (playTimer != null)
playTimer.stop();

// Timer for emulating Playing event
playTimer = new flash.utils.Timer(100, Math.round(sound.length / 100));
playTimer.addEventListener(flash.events.TimerEvent.TIMER, playingEvent);

playTimer.start();
    }
    function playingEvent(event: flash.events.Event) {
if (channel != null) {
pos = channel.position;
}
        dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, pos));
    }
    function stoppedEvent(event: flash.events.Event) {
        dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, pos));
    }

    public function pause() {
if (channel != null)
{
pos = channel.position;
channel.stop();

trace("mp3 Paused pos = " + pos);
dispatchEvent(new PlayerEvent(PlayerEvent.PAUSED, pos));

if (playTimer != null)
playTimer.stop();
}
    }
    public function resume() {
        trace("mp3 Try to resume from " + pos);
channel.stop();

        if (pos != null) {
channel = sound.play(pos);
        }
        else play();
    }
    public function seek(pos: Float) {
channel.stop();
channel = sound.play(pos);

dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, pos));
    }
    
    public function stop() {
trace("mp3 Stopped position = " + channel.position);
pos = channel.position;
channel.stop();

        if (File != null) {
            File.close();
            File = null;
            dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, 0.0));
        }
        if (playTimer != null) {
            playTimer = null;
        }
    }

    function completeHandler(event: flash.events.Event) {
        trace("mp3 completeHandler: " + event);
        dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, sound.length, sound.length) );
        dispatchEvent(event);
    }

    function progressHandler(event: flash.events.ProgressEvent) {
        trace("mp3 progressHandler: " + event);
        dispatchEvent(event); // here we fire byte progress

// dirty hack but could work correct I suppose :)
var percent = event.bytesLoaded / event.bytesTotal;
dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, sound.length * percent, sound.length) );
    }

    function ioErrorHandler(event: flash.events.IOErrorEvent) {
        trace("mp3 ERROR ERROR");
                dispatchEvent(event);
    }
}
Something went wrong with that request. Please try again.