Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from osharper/master

added support for mp3 files
  • Loading branch information...
commit bf474b5facfac90a0187137101f3d3402e8ec12e 2 parents 8266463 + 782fe82
@datacompboy authored
View
16 IPlayer.hx
@@ -0,0 +1,16 @@
+interface IPlayer {
+ var volume(getVolume, setVolume): Float;
+ var pan(getPan, setPan): Float;
+ var soundTransform(getST, setST): flash.media.SoundTransform;
+ function play(?path : String, ?trigger_buffer : Float): Void;
+ function setVolume(volume: Float) : Float;
+ function getVolume(): Float;
+ function setPan(pan: Float): Float;
+ function getPan(): Float;
+ function setST(st: flash.media.SoundTransform): flash.media.SoundTransform;
+ function getST(): flash.media.SoundTransform;
+ function pause(): Void;
+ function resume(): Void;
+ function seek(pos: Float): Void;
+ function stop(): Void;
+}
View
200 Mp3Player.hx
@@ -0,0 +1,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);
+ }
+}
View
2  Player.hx
@@ -18,7 +18,7 @@ import flash.media.SoundTransform;
// Main player class: loads stream, process it by appropriate file decoder,
// that will initialize correct sound decoder. Decoded audio samples
// resample to 44100 and play via AudioSink
-class Player extends flash.events.EventDispatcher {
+class Player extends flash.events.EventDispatcher, implements IPlayer {
var File : flash.net.URLStream;
var Sound : fmt.File;
var Resampler : com.sun.media.sound.SoftAbstractResampler;
View
60 WavPlayer.hx
@@ -303,8 +303,10 @@ class WavPlayerGui_Full extends WavPlayerGui {
// Main user interface: play / stop buttons & ExternalInterface
class WavPlayer {
- static var Version = "1.8.0";
- static var player : Player;
+ static var Version = "1.9.0";
+ static var player : IPlayer;
+ static var wavplayer : Player;
+ static var mp3player : Mp3Player;
static var state : String = PlayerEvent.STOPPED;
static var handlers : List<JsEventHandler>;
static var handlerId : Int;
@@ -364,18 +366,11 @@ class WavPlayer {
trace("WavPlayer - gui started " + iface);
iface.drawStopped();
-
- player = new Player(fvs.sound);
- player.addEventListener(PlayerEvent.BUFFERING, handleBuffering);
- player.addEventListener(PlayerEvent.PLAYING, handlePlaying);
- player.addEventListener(PlayerEvent.STOPPED, handleStopped);
- player.addEventListener(PlayerEvent.PAUSED, handlePaused);
- player.addEventListener(flash.events.ProgressEvent.PROGRESS, handleProgress);
- player.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleError);
- player.addEventListener(PlayerLoadEvent.LOAD, handleLoad);
- player.volume = volume;
- player.pan = pan;
-
+
+ initPlayerForUrl(fvs.sound);
+ player.volume = volume;
+ player.pan = pan;
+
if( !flash.external.ExternalInterface.available )
throw "External Interface not available";
try flash.external.ExternalInterface.addCallback("getVersion",doGetVer) catch( e : Dynamic ) {};
@@ -413,6 +408,38 @@ class WavPlayer {
else
flash.external.ExternalInterface.call("onWavPlayerReady", flash.external.ExternalInterface.objectID);
}
+
+ static function initPlayerForUrl(?path: String) {
+ if(path == null && player != null) return;
+
+ if(path != null && (~/[.]mp3$/i).match(path)) {
+ if(mp3player == null) {
+ mp3player = new Mp3Player(path);
+ AddPlayerEventListeners(mp3player);
+ }
+ player = mp3player;
+ }
+ else {
+ if(wavplayer == null) {
+ wavplayer = new Player(path);
+ AddPlayerEventListeners(wavplayer);
+ }
+ player = wavplayer;
+ }
+ }
+
+ static function AddPlayerEventListeners(dispatcher: flash.events.EventDispatcher) {
+ dispatcher.addEventListener(PlayerEvent.BUFFERING, handleBuffering);
+ dispatcher.addEventListener(PlayerEvent.PLAYING, handlePlaying);
+ dispatcher.addEventListener(PlayerEvent.STOPPED, handleStopped);
+ dispatcher.addEventListener(PlayerEvent.PAUSED, handlePaused);
+
+ dispatcher.addEventListener(flash.events.ProgressEvent.PROGRESS, handleProgress);
+ dispatcher.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleError);
+
+ dispatcher.addEventListener(PlayerLoadEvent.LOAD, handleLoad);
+ }
+
static function handleSeeking(event:WavPlayerGuiEvent) {
player.seek(event.position);
}
@@ -487,7 +514,10 @@ class WavPlayer {
player.stop();
lastNotifyProgress = 0;
lastNotifyLoad = 0;
- iface.setPosition(0);
+ iface.setPosition(0);
+
+ initPlayerForUrl(fname);
+
player.play(fname, buffer);
}
static function doStop( ) {
View
BIN  wavplayer-debug.swf
Binary file not shown
View
BIN  wavplayer.swf
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.