-
Notifications
You must be signed in to change notification settings - Fork 312
/
sound.as
94 lines (81 loc) · 3.11 KB
/
sound.as
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
package {
import flash.display.*;
import flash.events.*;
import flash.external.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
public class sound extends Sprite {
private var _sound:Sound;
private var channel:SoundChannel = new SoundChannel();
private var position:Number = 0;
private var volume:Number = 1;
private var playing:Boolean = false;
public function sound() {
Security.allowDomain('*');
var id:String = root.loaderInfo.parameters.id;
var src:String = root.loaderInfo.parameters.src;
_sound = new Sound(new URLRequest(src));
_sound.addEventListener(Event.COMPLETE, function(e:Event):void {
ExternalInterface.call([
'function() {',
'enchant.Sound["', id, '"].dispatchEvent(new Event("load"));',
'}'
].join(''));
ExternalInterface.addCallback('play', play);
ExternalInterface.addCallback('pause', pause);
ExternalInterface.addCallback('stop', stop);
ExternalInterface.addCallback('getCurrentTime', getCurrentTime);
ExternalInterface.addCallback('setCurrentTime', setCurrentTime);
ExternalInterface.addCallback('getDuration', getDuration);
ExternalInterface.addCallback('getVolume', getVolume);
ExternalInterface.addCallback('setVolume', setVolume);
});
_sound.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void {
ExternalInterface.call([
'function() {',
'throw new Error("Cannot load an asset: ', src, '");',
'}'
].join(''));
});
}
public function play():void {
channel = _sound.play(position);
setVolume(volume);
playing = true;
}
public function pause():void {
position = channel.position;
channel.stop();
playing = false;
}
public function stop():void {
position = 0;
channel.stop();
playing = false;
}
public function getCurrentTime():Number {
return (playing ? channel.position : position) / 1000;
}
public function setCurrentTime(time:Number):void {
position = time * 1000;
if (position > _sound.length) position = _sound.length;
if (position < 0) position = 0;
if (playing) play();
}
public function getDuration():Number {
return _sound.length / 1000;
}
public function getVolume():Number {
return volume;
}
public function setVolume(vol:Number):void {
volume = vol;
var transform:SoundTransform = channel.soundTransform;
if (vol < 0) vol = 0;
if (vol > 1) vol = 1;
transform.volume = vol;
channel.soundTransform = transform;
}
}
}