This repository has been archived by the owner on Apr 2, 2022. It is now read-only.
forked from datacompboy/WavPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Audiojs.hx
132 lines (104 loc) · 4.14 KB
/
Audiojs.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
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
class Audiojs {
static var player : Player;
static var playerInstance : String;
static var timer : flash.utils.Timer;
static var playing : Bool;
static var duration : Float;
static var pausePoint : Float;
static var playProgress: Float;
static var lastTimestamp : Float;
static function l(?str:String) {
flash.external.ExternalInterface.call('console.log', str);
}
static function main() {
var fvs : Dynamic<String> = flash.Lib.current.loaderInfo.parameters;
playerInstance = fvs.playerInstance+'.';
if( !flash.external.ExternalInterface.available )
throw "External Interface not available";
try flash.external.ExternalInterface.addCallback("init",init) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("load",load) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("pplay",play) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("ppause",pause) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("skipTo",skipTo) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("setVolume",setVolume) catch( e : Dynamic ) {};
flash.external.ExternalInterface.call(playerInstance+'loadStarted');
}
static function init(mp3:String) {
load(mp3);
}
static function load(mp3:String) {
var volume: Float = 1.0;
var pan: Float = 0.0;
player = new Player(mp3);
player.addEventListener(PlayerEvent.PLAYING, handlePlaying);
player.addEventListener(PlayerEvent.STOPPED, handleStopped);
player.addEventListener(PlayerEvent.PAUSED, handlePaused);
player.addEventListener(PlayerLoadEvent.LOAD, loadProgress);
player.addEventListener(flash.events.IOErrorEvent.IO_ERROR, loadError);
player.volume = volume;
player.pan = pan;
playing = false;
timer = new flash.utils.Timer(250, 0);
timer.addEventListener(flash.events.TimerEvent.TIMER, updatePlayhead);
}
static function play() {
playing = true;
if(pausePoint > 0) {
player.resume();
} else {
pausePoint = 0;
player.play();
}
}
static function pause() {
playing = false;
pausePoint = playProgress;
player.pause();
}
static function skipTo(percent:Float) {
pausePoint = percent;
player.seek(percent * duration);
if(playing == false) {
player.pause();
}
}
static function setVolume(volume:Float) {
player.volume = volume;
}
static function updatePlayhead(event:flash.events.TimerEvent) {
var ts = haxe.Timer.stamp();
playProgress = pausePoint + ((ts - lastTimestamp) / duration);
if (playProgress > 1) {
playProgress = 1;
}
if (playProgress > 0) {
flash.external.ExternalInterface.call(playerInstance+'updatePlayhead', playProgress);
}
}
static function loadProgress(event:PlayerLoadEvent) {
var loadPercent:Float = event.SecondsLoaded / event.SecondsTotal;
duration = event.SecondsTotal;
if (loadPercent > 1) {
loadPercent = 1;
}
if (loadPercent > 0) {
flash.external.ExternalInterface.call(playerInstance+'loadProgress', loadPercent, event.SecondsTotal);
}
}
static function loadError(event:flash.events.IOErrorEvent) {
flash.external.ExternalInterface.call(playerInstance+'loadError');
}
static function handlePlaying(event:PlayerEvent) {
timer.start();
lastTimestamp = haxe.Timer.stamp();
}
static function handleStopped(event:PlayerEvent) {
flash.external.ExternalInterface.call(playerInstance+'trackEnded');
timer.stop();
playing = false;
pausePoint = 0.0;
}
static function handlePaused(event:PlayerEvent) {
timer.stop();
}
}