1 | /* |
---|
2 | * Ext JS Library 0.30 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | Ext.ns('Ext.air'); |
---|
10 | |
---|
11 | Ext.air.MusicPlayer = Ext.extend(Ext.util.Observable, { |
---|
12 | /** |
---|
13 | * The currently active Sound. Read-only. |
---|
14 | * @type air.Sound |
---|
15 | * @property activeSound |
---|
16 | */ |
---|
17 | activeSound: null, |
---|
18 | /** |
---|
19 | * The currently active SoundChannel. Read-only. |
---|
20 | * @type air.SoundChannel |
---|
21 | * @property activeChannel |
---|
22 | */ |
---|
23 | activeChannel: null, |
---|
24 | /** |
---|
25 | * The currently active Transform. Read-only. |
---|
26 | * @type air.SoundTransform |
---|
27 | * @property activeTransform |
---|
28 | */ |
---|
29 | activeTransform: new air.SoundTransform(1, 0), |
---|
30 | // private |
---|
31 | pausePosition: 0, |
---|
32 | /** |
---|
33 | * @cfg {Number} progressInterval |
---|
34 | * How often to fire the progress event when playing music in milliseconds |
---|
35 | * Defaults to 500. |
---|
36 | */ |
---|
37 | progressInterval: 500, |
---|
38 | |
---|
39 | constructor: function(config) { |
---|
40 | config = config || {}; |
---|
41 | Ext.apply(this, config); |
---|
42 | |
---|
43 | this.addEvents( |
---|
44 | /** |
---|
45 | * @event stop |
---|
46 | */ |
---|
47 | 'stop', |
---|
48 | /** |
---|
49 | * @event pause |
---|
50 | */ |
---|
51 | 'pause', |
---|
52 | /** |
---|
53 | * @event play |
---|
54 | */ |
---|
55 | 'play', |
---|
56 | /** |
---|
57 | * @event load |
---|
58 | */ |
---|
59 | 'load', |
---|
60 | /** |
---|
61 | * @event id3info |
---|
62 | */ |
---|
63 | 'id3info', |
---|
64 | /** |
---|
65 | * @event complete |
---|
66 | */ |
---|
67 | 'complete', |
---|
68 | /** |
---|
69 | * @event progress |
---|
70 | */ |
---|
71 | 'progress', |
---|
72 | /** |
---|
73 | * @event skip |
---|
74 | */ |
---|
75 | 'skip' |
---|
76 | ); |
---|
77 | |
---|
78 | Ext.air.MusicPlayer.superclass.constructor.call(this, config); |
---|
79 | this.onSoundFinishedDelegate = this.onSoundFinished.createDelegate(this); |
---|
80 | this.onSoundLoadDelegate = this.onSoundLoad.createDelegate(this); |
---|
81 | this.onSoundID3LoadDelegate = this.onSoundID3Load.createDelegate(this); |
---|
82 | |
---|
83 | Ext.TaskMgr.start({ |
---|
84 | run: this.notifyProgress, |
---|
85 | scope: this, |
---|
86 | interval: this.progressInterval |
---|
87 | }); |
---|
88 | }, |
---|
89 | |
---|
90 | /** |
---|
91 | * Adjust the volume |
---|
92 | * @param {Object} percent |
---|
93 | * Ranges from 0 to 1 specifying volume of sound. |
---|
94 | */ |
---|
95 | adjustVolume: function(percent) { |
---|
96 | this.activeTransform.volume = percent; |
---|
97 | if (this.activeChannel) { |
---|
98 | this.activeChannel.soundTransform = this.activeTransform; |
---|
99 | } |
---|
100 | }, |
---|
101 | /** |
---|
102 | * Stop the player |
---|
103 | */ |
---|
104 | stop: function() { |
---|
105 | this.pausePosition = 0; |
---|
106 | if (this.activeChannel) { |
---|
107 | this.activeChannel.stop(); |
---|
108 | this.activeChannel = null; |
---|
109 | } |
---|
110 | if (this.activeSound) { |
---|
111 | this.activeSound.removeEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate); |
---|
112 | this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate); |
---|
113 | this.activeSound.removeEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate); |
---|
114 | } |
---|
115 | }, |
---|
116 | /** |
---|
117 | * Pause the player if there is an activeChannel |
---|
118 | */ |
---|
119 | pause: function() { |
---|
120 | if (this.activeChannel) { |
---|
121 | this.pausePosition = this.activeChannel.position; |
---|
122 | this.activeChannel.stop(); |
---|
123 | } |
---|
124 | }, |
---|
125 | /** |
---|
126 | * Play a sound, if no url is specified will attempt to resume the activeSound |
---|
127 | * @param {String} url (optional) |
---|
128 | * Url resource to play |
---|
129 | */ |
---|
130 | play: function(url) { |
---|
131 | if (url) { |
---|
132 | this.stop(); |
---|
133 | var req = new air.URLRequest(url); |
---|
134 | this.activeSound = new air.Sound(); |
---|
135 | this.activeSound.addEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate); |
---|
136 | this.activeSound.addEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate); |
---|
137 | this.activeSound.addEventListener(air.Event.ID3, this.onSoundID3LoadDelegate); |
---|
138 | this.activeSound.load(req); |
---|
139 | } else { |
---|
140 | this.onSoundLoad(); |
---|
141 | } |
---|
142 | }, |
---|
143 | |
---|
144 | /** |
---|
145 | * Skip to a specific position in the song currently playing. |
---|
146 | * @param {Object} pos |
---|
147 | */ |
---|
148 | skipTo: function(pos) { |
---|
149 | if (this.activeChannel) { |
---|
150 | this.activeChannel.stop(); |
---|
151 | this.activeChannel = this.activeSound.play(pos); |
---|
152 | this.activeChannel.soundTransform = this.activeTransform; |
---|
153 | this.fireEvent('skip', this.activeChannel, this.activeSound, pos); |
---|
154 | } |
---|
155 | }, |
---|
156 | |
---|
157 | /** |
---|
158 | * Returns whether or not there is an active SoundChannel. |
---|
159 | */ |
---|
160 | hasActiveChannel: function() { |
---|
161 | return !!this.activeChannel; |
---|
162 | }, |
---|
163 | |
---|
164 | // private |
---|
165 | onSoundLoad: function(event) { |
---|
166 | if (this.activeSound) { |
---|
167 | if (this.activeChannel) { |
---|
168 | this.activeChannel.stop(); |
---|
169 | } |
---|
170 | this.activeChannel = this.activeSound.play(this.pausePosition); |
---|
171 | this.activeChannel.soundTransform = this.activeTransform; |
---|
172 | this.fireEvent('load', this.activeChannel, this.activeSound); |
---|
173 | } |
---|
174 | }, |
---|
175 | // private |
---|
176 | onSoundFinished: function(event) { |
---|
177 | // relay AIR event |
---|
178 | this.fireEvent('complete', event); |
---|
179 | }, |
---|
180 | // private |
---|
181 | onSoundID3Load: function(event) { |
---|
182 | this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate); |
---|
183 | var id3 = event.target.id3; |
---|
184 | this.fireEvent('id3info', id3); |
---|
185 | }, |
---|
186 | // private |
---|
187 | notifyProgress: function() { |
---|
188 | if (this.activeChannel && this.activeSound) { |
---|
189 | var playbackPercent = 100 * (this.activeChannel.position / this.activeSound.length); |
---|
190 | // SOUND_COMPLETE does not seem to work consistently. |
---|
191 | if (playbackPercent > 99.7) { |
---|
192 | this.onSoundFinished(); |
---|
193 | } else { |
---|
194 | this.fireEvent('progress', this.activeChannel, this.activeSound); |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | }); |
---|