MusicPlayer.js
4.78 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Ext JS Library 0.30
* Copyright(c) 2006-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.ns('Ext.air');
Ext.air.MusicPlayer = Ext.extend(Ext.util.Observable, {
/**
* The currently active Sound. Read-only.
* @type air.Sound
* @property activeSound
*/
activeSound: null,
/**
* The currently active SoundChannel. Read-only.
* @type air.SoundChannel
* @property activeChannel
*/
activeChannel: null,
/**
* The currently active Transform. Read-only.
* @type air.SoundTransform
* @property activeTransform
*/
activeTransform: new air.SoundTransform(1, 0),
// private
pausePosition: 0,
/**
* @cfg {Number} progressInterval
* How often to fire the progress event when playing music in milliseconds
* Defaults to 500.
*/
progressInterval: 500,
constructor: function(config) {
config = config || {};
Ext.apply(this, config);
this.addEvents(
/**
* @event stop
*/
'stop',
/**
* @event pause
*/
'pause',
/**
* @event play
*/
'play',
/**
* @event load
*/
'load',
/**
* @event id3info
*/
'id3info',
/**
* @event complete
*/
'complete',
/**
* @event progress
*/
'progress',
/**
* @event skip
*/
'skip'
);
Ext.air.MusicPlayer.superclass.constructor.call(this, config);
this.onSoundFinishedDelegate = this.onSoundFinished.createDelegate(this);
this.onSoundLoadDelegate = this.onSoundLoad.createDelegate(this);
this.onSoundID3LoadDelegate = this.onSoundID3Load.createDelegate(this);
Ext.TaskMgr.start({
run: this.notifyProgress,
scope: this,
interval: this.progressInterval
});
},
/**
* Adjust the volume
* @param {Object} percent
* Ranges from 0 to 1 specifying volume of sound.
*/
adjustVolume: function(percent) {
this.activeTransform.volume = percent;
if (this.activeChannel) {
this.activeChannel.soundTransform = this.activeTransform;
}
},
/**
* Stop the player
*/
stop: function() {
this.pausePosition = 0;
if (this.activeChannel) {
this.activeChannel.stop();
this.activeChannel = null;
}
if (this.activeSound) {
this.activeSound.removeEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate);
this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
this.activeSound.removeEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate);
}
},
/**
* Pause the player if there is an activeChannel
*/
pause: function() {
if (this.activeChannel) {
this.pausePosition = this.activeChannel.position;
this.activeChannel.stop();
}
},
/**
* Play a sound, if no url is specified will attempt to resume the activeSound
* @param {String} url (optional)
* Url resource to play
*/
play: function(url) {
if (url) {
this.stop();
var req = new air.URLRequest(url);
this.activeSound = new air.Sound();
this.activeSound.addEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate);
this.activeSound.addEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate);
this.activeSound.addEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
this.activeSound.load(req);
} else {
this.onSoundLoad();
}
},
/**
* Skip to a specific position in the song currently playing.
* @param {Object} pos
*/
skipTo: function(pos) {
if (this.activeChannel) {
this.activeChannel.stop();
this.activeChannel = this.activeSound.play(pos);
this.activeChannel.soundTransform = this.activeTransform;
this.fireEvent('skip', this.activeChannel, this.activeSound, pos);
}
},
/**
* Returns whether or not there is an active SoundChannel.
*/
hasActiveChannel: function() {
return !!this.activeChannel;
},
// private
onSoundLoad: function(event) {
if (this.activeSound) {
if (this.activeChannel) {
this.activeChannel.stop();
}
this.activeChannel = this.activeSound.play(this.pausePosition);
this.activeChannel.soundTransform = this.activeTransform;
this.fireEvent('load', this.activeChannel, this.activeSound);
}
},
// private
onSoundFinished: function(event) {
// relay AIR event
this.fireEvent('complete', event);
},
// private
onSoundID3Load: function(event) {
this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
var id3 = event.target.id3;
this.fireEvent('id3info', id3);
},
// private
notifyProgress: function() {
if (this.activeChannel && this.activeSound) {
var playbackPercent = 100 * (this.activeChannel.position / this.activeSound.length);
// SOUND_COMPLETE does not seem to work consistently.
if (playbackPercent > 99.7) {
this.onSoundFinished();
} else {
this.fireEvent('progress', this.activeChannel, this.activeSound);
}
}
}
});