geekrelief (owner)

Revisions

gist: 106059 Download_button fork
public
Public Clone URL: git://gist.github.com/106059.git
Soundfx.hx
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
/* 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);
        }
    }
}