summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/av/FLAudio.js
blob: 07fd9461e00f58e145e1a8d7c1e4f4fc7bb8a41a (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//>>built
define("dojox/av/FLAudio", ['dojo', 'dojox/embed/Flash', 'dojox/timing/doLater'],function(dojo, dijit){

dojo.experimental("dojox.av.FLVideo");

dojo.declare("dojox.av.FLAudio", null, {

	// summary:
	//		Play MP3 files through the Flash SWF built in the
	//		DEFT project.
	// description:
	//		This class is brand new, so there is a lot of
	//		functionality not yet available. The initial
	//		purpose is for playing "event" sounds like button
	//		clicks, and for loading and controlling multiple
	//		sounds at once. As of yet, streaming is not supported
	//		and polling the sounds for events during playback
	//		may still be missing information. Markup is not
	//		supported, as it may not be needed.
	//
	//	TODO:
	//		Streaming, playback events, crossdomain, CDN support,
	//		(alternate SWF location), global volume, ID3 tag,
	//		factor out doLater, onLoadStatus needs work,
	//		play(position) / seek()
	//
	// example:
	//		|	new dojox.av.FLAudio({
	//		|		initialVolume:.7,
	//		|		initialPan:0,
	//		|		autoPlay:false
	//		|	});
	//
	//  id: String?
	//		The id of this widget and the id of the SWF movie.
	id:"",
	//
	//	initialVolume: Number
	//		From 0-1
	//		Sets volume for all files unless changed with doPlay
	//		or setVolume
	initialVolume: 0.7,
	//
	//	initialPan: Number
	//		From -1 to 1 (-1 is left, 1 is right, 0 is middle)
	//		Sets pan for all files unless changed with play
	//		or setPan
	initialPan: 0,
	//
	//	autoPlay: Boolean
	//		If true, all files will play upon load. If false,
	//		they load and wait for doPlay() command.
	//
	// isDebug: Boolean?
	//		Setting to true tells the SWF to output log messages to Firebug.
	isDebug: false,
	//
	//	statusInterval: Number
	//		How often in milliseconds that the status of the
	//		player is checked - both load and play
	statusInterval:200,
	//
	// _swfPath: Uri
	//		The path to the video player SWF resource
	_swfPath: dojo.moduleUrl("dojox.av", "resources/audio.swf"),
	//
	//
	// allowScriptAccess: String
	//		Whether the SWF can access the container JS
	allowScriptAccess:"always",
	//
	// allowNetworking: String
	//		Whether SWF is restricted to a domain
	allowNetworking: "all",
	//

	constructor: function(/*Object*/options){

		// Provide this function for the SWF to ensure that the it is playing
		// in HTML.
		dojo.global.swfIsInHTML = function(){ return true; }

		dojo.mixin(this, options || {});
		if(!this.id){ this.id = "flaudio_"+new Date().getTime(); }
		this.domNode = dojo.doc.createElement("div");
		dojo.style(this.domNode, {
			position:"relative",
			width:"1px",
			height:"1px",
			top:"1px",
			left:"1px"
		});
		dojo.body().appendChild(this.domNode);
		this.init();
	},

	init: function(){
		// summary:
		// Initialize the media.
		//
		//
		this._subs = [];
		this.initialVolume = this._normalizeVolume(this.initialVolume);

		var args = {
			path:this._swfPath.uri,
			width:"1px",
			height:"1px",
			minimumVersion:9, // this may need to be 10, not sure
			expressInstall:true,
			params:{
				wmode:"transparent",
				allowScriptAccess:this.allowScriptAccess,
				allowNetworking:this.allowNetworking
			},
			// only pass in simple variables - no deep objects
			vars:{
				id:this.id,
				autoPlay:this.autoPlay,
				initialVolume:this.initialVolume,
				initialPan:this.initialPan,
				statusInterval:this.statusInterval,
				isDebug:this.isDebug
			}
		};

		this._sub("mediaError",    "onError");
		this._sub("filesProgress", "onLoadStatus");
		this._sub("filesAllLoaded", "onAllLoaded");
		this._sub("mediaPosition", "onPlayStatus");
		this._sub("mediaEnd", "onComplete");
		this._sub("mediaMeta",     "onID3");

		this._flashObject = new dojox.embed.Flash(args, this.domNode);
		this._flashObject.onError = function(err){
			console.warn("Flash Error:", err);
		};
		this._flashObject.onLoad = dojo.hitch(this, function(mov){
			this.flashMedia = mov;
			this.isPlaying = this.autoPlay;
			this.isStopped = !this.autoPlay;
			this.onLoad(this.flashMedia);
		});
	},

	//  ==============  //
	//  Loading Files   //
	//  ==============  //

	load: function(/*Object*/options){
		// summary:
		//		Adds a media object to the playlist
		//		***This can be called repeatedly to add multiple items.
		//	options: Object
		//		url: String
		//			(required) path to MP3 media
		//			url must be absolute or relative to SWF,
		//			not dojo or the html. An effort will be made
		//			to fix incorrect paths.
		//		id: String
		//			(optional) an identifier to later determine
		//			which media to control.
		//	returns:
		//		The normalized url, which can be used to identify the
		//		audio.
		//
		if(dojox.timing.doLater(this.flashMedia, this)){ return false; }
		if(!options.url){
			throw new Error("An url is required for loading media");
			return false;
		}else{
			options.url = this._normalizeUrl(options.url);
		}
		this.flashMedia.load(options);

		return options.url; // String
	},

	//  =============================  //
	//  Methods to control the sound   //
	//  =============================  //

	doPlay: function(/*Object*/options){
		// summary:
		//		Tell media to play, based on
		//		the options passed.
		// options: Object
		//		volume: Number
		//			Sets the volume
		//		pan: Number
		//			Sets left/right pan
		//		index:Number OR id:String OR url:String
		//			Choose one of the above to indentify
		//			the media you wish to control. id is
		//			set by you. index is the order in which
		//			media was added (zero based)
		//			NOTE: lack of an identifier will default
		//			to first (or only) item.
		//	NOTE: Can't name this method "play()" as it causes
		//			an IE error.
		this.flashMedia.doPlay(options);
	},

	pause: function(/*Object*/options){
		// summary:
		//		Tell media to pause, based on identifier in
		//		the options passed.
		// options: Object
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		this.flashMedia.pause(options);
	},

	stop: function(/*Object*/options){
		// summary:
		//		Tell media to stop, based on identifier in
		//		the options passed.
		// options:
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		this.flashMedia.doStop(options);
	},

	setVolume: function(/*Object*/options){
		// summary:
		//		Set media volume, based on identifier in
		//		the options passed.
		// options:
		//		volume: Number
		//		0 to 1
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		this.flashMedia.setVolume(options);
	},

	setPan: function(/*Object*/options){
		// summary:
		//		Set media pan, based on identifier in
		//		the options passed.
		// options:
		//		pan:Number
		//			-1 to 1
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		this.flashMedia.setPan(options);
	},

	getVolume: function(/*Object*/options){
		// summary:
		//		Get media volume, based on identifier in
		//		the options passed.
		// options:
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		return this.flashMedia.getVolume(options);
	},

	getPan: function(/*Object*/options){
		// summary:
		//		Set media pan, based on identifier in
		//		the options passed.
		// options:
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		return this.flashMedia.getPan(options);
	},

	getPosition: function(/*Object*/options){
		// summary:
		//		Get the current time.
		// options:
		//		index:Number OR id:String OR url:String
		//			See doPlay()
		//
		return this.flashMedia.getPosition(options);
	},

	//  =============  //
	//  Sound Events   //
	//  =============  //
	onError: function(msg){
		// summary:
		//		stub fired when an error occurs
		console.warn("SWF ERROR:", msg)
	},

	onLoadStatus: function(/*Array*/events){
		// summary:
	},

	onAllLoaded: function(){
		// summary:
		//		stub fired
	},

	onPlayStatus: function(/*Array*/events){
		// summary:
	},

	onComplete: function(/*Array*/events){
		// summary:
		//		Fired at the end of a media file.
	},

	onLoad: function(){
		// summary:
		//		stub fired when SWF is ready
	},
	onID3: function(evt){
		//	summary:
		//		Fired when the ID3 data is received.
	},



	destroy: function(){
		// summary:
		// 		destroys flash
		if(!this.flashMedia){
			this._cons.push(dojo.connect(this, "onLoad", this, "destroy"));
			return;
		}
		dojo.forEach(this._subs, function(s){
			dojo.unsubscribe(s);
		});
		dojo.forEach(this._cons, function(c){
			dojo.disconnect(c);
		});
		this._flashObject.destroy();
		//dojo._destroyElement(this.flashDiv);
	},



	_sub: function(topic, method){
		// summary:
		// helper for subscribing to topics
		dojo.subscribe(this.id+"/"+topic, this, method);
	},

	_normalizeVolume: function(vol){
		// summary:
		//		Ensures volume is less than one
		//
		if(vol>1){
			while(vol>1){
				vol*=.1
			}
		}
		return vol;
	},

	_normalizeUrl: function(_url){
		// summary:
		//		Checks that path is relative to HTML file or
		//		convertes it to an absolute path.
		//
		if(_url && _url.toLowerCase().indexOf("http")<0){
			//
			// Appears to be a relative path. Attempt to  convert it to absolute,
			// so it will better target the SWF.
			var loc = window.location.href.split("/");
			loc.pop();
			loc = loc.join("/")+"/";

			_url = loc+_url;
		}
		return _url;
	}

});
return dojox.av.FLAudio;
});