Skip to content

Instantly share code, notes, and snippets.

@geekrelief
Created May 3, 2009 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekrelief/106059 to your computer and use it in GitHub Desktop.
Save geekrelief/106059 to your computer and use it in GitHub Desktop.
/* compile.hxml
-swf-version 10
-swf sfx.swf
-main Soundfx
-swf-header 600:400:30:ff6600
*/
// Soundfx.hx - example for playing back mp3s in haxe
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.KeyboardEvent;
import flash.events.Event;
class Soundfx {
public var s:Sound;
public var m:Sound;
public var c:SoundChannel;
public var loop:Bool;
public static function main() {
new Soundfx();
}
public function new() {
m = new Sound(new URLRequest("music/Tech_Step_Break.mp3"));
c = null;
flash.Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, on_key_down);
loop = false;
}
public function on_key_down(e:KeyboardEvent) {
trace(e.keyCode);
switch (e.keyCode) {
case 37:
if(c != null) {
var pos = c.position;
if(pos > 0) {
pos = (((pos - 250) < 0) ? 0 : (pos -250));
}
play(pos);
trace("<< "+pos);
}
case 39:
if(c != null) {
var pos = c.position;
if(pos < m.length) {
pos = (((pos + 250) < m.length) ? (pos +250) : m.length -1);
}
play(pos);
trace(">> "+pos);
}
case 38:
if(c != null) {
c.stop();
}
c = null;
case 40:
trace("play");
play();
case 76:
loop = !loop;
trace("looping "+loop);
play();
default:
}
}
public function soundComplete(e:Event) {
trace("finished");
c = null;
}
public function play(?pos:Float = 0) {
if(loop && c != null) {
var _pos = c.position;
var t = m.play(pos, 10000);
c.stop();
c = t;
c.addEventListener("soundComplete", soundComplete);
} else if (c != null) {
var _pos = c.position;
var t = m.play(pos);
c.stop();
c = t;
c.addEventListener("soundComplete", soundComplete);
} else {
c = m.play(pos);
c.addEventListener("soundComplete", soundComplete);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment