2 * Ext JS Library 0.30
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
11 Ext.air.MusicPlayer = Ext.extend(Ext.util.Observable, {
\r
13 * The currently active Sound. Read-only.
\r
15 * @property activeSound
\r
19 * The currently active SoundChannel. Read-only.
\r
20 * @type air.SoundChannel
\r
21 * @property activeChannel
\r
23 activeChannel: null,
\r
25 * The currently active Transform. Read-only.
\r
26 * @type air.SoundTransform
\r
27 * @property activeTransform
\r
29 activeTransform: new air.SoundTransform(1, 0),
\r
33 * @cfg {Number} progressInterval
\r
34 * How often to fire the progress event when playing music in milliseconds
\r
37 progressInterval: 500,
\r
39 constructor: function(config) {
\r
40 config = config || {};
\r
41 Ext.apply(this, config);
\r
78 Ext.air.MusicPlayer.superclass.constructor.call(this, config);
\r
79 this.onSoundFinishedDelegate = this.onSoundFinished.createDelegate(this);
\r
80 this.onSoundLoadDelegate = this.onSoundLoad.createDelegate(this);
\r
81 this.onSoundID3LoadDelegate = this.onSoundID3Load.createDelegate(this);
\r
84 run: this.notifyProgress,
\r
86 interval: this.progressInterval
\r
92 * @param {Object} percent
\r
93 * Ranges from 0 to 1 specifying volume of sound.
\r
95 adjustVolume: function(percent) {
\r
96 this.activeTransform.volume = percent;
\r
97 if (this.activeChannel) {
\r
98 this.activeChannel.soundTransform = this.activeTransform;
\r
105 this.pausePosition = 0;
\r
106 if (this.activeChannel) {
\r
107 this.activeChannel.stop();
\r
108 this.activeChannel = null;
\r
110 if (this.activeSound) {
\r
111 this.activeSound.removeEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate);
\r
112 this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
\r
113 this.activeSound.removeEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate);
\r
117 * Pause the player if there is an activeChannel
\r
119 pause: function() {
\r
120 if (this.activeChannel) {
\r
121 this.pausePosition = this.activeChannel.position;
\r
122 this.activeChannel.stop();
\r
126 * Play a sound, if no url is specified will attempt to resume the activeSound
\r
127 * @param {String} url (optional)
\r
128 * Url resource to play
\r
130 play: function(url) {
\r
133 var req = new air.URLRequest(url);
\r
134 this.activeSound = new air.Sound();
\r
135 this.activeSound.addEventListener(air.Event.SOUND_COMPLETE, this.onSoundFinishedDelegate);
\r
136 this.activeSound.addEventListener(air.Event.COMPLETE, this.onSoundLoadDelegate);
\r
137 this.activeSound.addEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
\r
138 this.activeSound.load(req);
\r
140 this.onSoundLoad();
\r
145 * Skip to a specific position in the song currently playing.
\r
146 * @param {Object} pos
\r
148 skipTo: function(pos) {
\r
149 if (this.activeChannel) {
\r
150 this.activeChannel.stop();
\r
151 this.activeChannel = this.activeSound.play(pos);
\r
152 this.activeChannel.soundTransform = this.activeTransform;
\r
153 this.fireEvent('skip', this.activeChannel, this.activeSound, pos);
\r
158 * Returns whether or not there is an active SoundChannel.
\r
160 hasActiveChannel: function() {
\r
161 return !!this.activeChannel;
\r
165 onSoundLoad: function(event) {
\r
166 if (this.activeSound) {
\r
167 if (this.activeChannel) {
\r
168 this.activeChannel.stop();
\r
170 this.activeChannel = this.activeSound.play(this.pausePosition);
\r
171 this.activeChannel.soundTransform = this.activeTransform;
\r
172 this.fireEvent('load', this.activeChannel, this.activeSound);
\r
176 onSoundFinished: function(event) {
\r
178 this.fireEvent('complete', event);
\r
181 onSoundID3Load: function(event) {
\r
182 this.activeSound.removeEventListener(air.Event.ID3, this.onSoundID3LoadDelegate);
\r
183 var id3 = event.target.id3;
\r
184 this.fireEvent('id3info', id3);
\r
187 notifyProgress: function() {
\r
188 if (this.activeChannel && this.activeSound) {
\r
189 var playbackPercent = 100 * (this.activeChannel.position / this.activeSound.length);
\r
190 // SOUND_COMPLETE does not seem to work consistently.
\r
191 if (playbackPercent > 99.7) {
\r
192 this.onSoundFinished();
\r
194 this.fireEvent('progress', this.activeChannel, this.activeSound);
\r