This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.as
444 lines (327 loc) · 11.9 KB
/
Main.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.StageOrientationEvent;
import flash.events.StatusEvent;
import flash.text.AntiAliasType;
import flash.text.AutoCapitalize;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.InvokeEvent;
import flash.filesystem.File;
import flash.media.Sound;
import flash.media.SoundChannel;
import com.myflashlab.air.extensions.volume.Volume;
import com.myflashlab.air.extensions.volume.VolumeEvent;
import com.myflashlab.air.extensions.volume.NativeSound;
import com.doitflash.text.modules.MySprite;
import com.doitflash.starling.utils.list.List;
import com.doitflash.consts.Direction;
import com.doitflash.consts.Orientation;
import com.doitflash.consts.Easing;
import com.doitflash.mobileProject.commonCpuSrc.DeviceInfo;
import com.luaye.console.C;
/**
* ...
* @author Hadi Tavakoli - 2/9/2013 12:06 PM
*/
public class Main extends Sprite
{
private const BTN_WIDTH:Number = 150;
private const BTN_HEIGHT:Number = 60;
private const BTN_SPACE:Number = 2;
private var _txt:TextField;
private var _body:Sprite;
private var _list:List;
private var _numRows:int = 1;
[Embed(source = "myGameMusic.mp3")]
private var MySound:Class;
private var _myMusic:Sound;
private var _myMusicChannel:SoundChannel;
private var _nativeSound1:NativeSound;
private var _nativeSound2:NativeSound;
public function Main():void
{
Multitouch.inputMode = MultitouchInputMode.GESTURE;
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke, false, 0, true);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
stage.addEventListener(Event.RESIZE, onResize);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
C.startOnStage(this, "`");
C.commandLine = false;
C.commandLineAllowed = false;
C.x = 20;
C.width = 250;
C.height = 150;
C.strongRef = true;
C.visible = true;
C.scaleX = C.scaleY = DeviceInfo.dpiScaleMultiplier;
_txt = new TextField();
_txt.autoSize = TextFieldAutoSize.LEFT;
_txt.antiAliasType = AntiAliasType.ADVANCED;
_txt.multiline = true;
_txt.wordWrap = true;
_txt.embedFonts = false;
_txt.htmlText = "<font face='Arimo' color='#333333' size='20'><b>Volume Manager ANE V"+Volume.VERSION+"</b></font>";
_txt.scaleX = _txt.scaleY = DeviceInfo.dpiScaleMultiplier;
this.addChild(_txt);
_body = new Sprite();
this.addChild(_body);
_list = new List();
_list.holder = _body;
_list.itemsHolder = new Sprite();
_list.orientation = Orientation.VERTICAL;
_list.hDirection = Direction.LEFT_TO_RIGHT;
_list.vDirection = Direction.TOP_TO_BOTTOM;
_list.space = BTN_SPACE;
init();
onResize();
}
private function onInvoke(e:InvokeEvent):void
{
NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke);
}
private function handleActivate(e:Event):void
{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
private function handleKeys(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
Volume.service.removeEventListener(VolumeEvent.VOLUME_CHANGE, onDeviceVolumeChanged);
Volume.service.removeEventListener(VolumeEvent.MUTE_STATE, onDeviceMuteChanged);
Volume.service.removeEventListener(VolumeEvent.ERROR, onError);
// when you are done with the extension or when you are leaving the app, make sure to dispose the ANE.
// When you dispose the ANE, you HAVE to initialize it again with Volume.init(); if you wish to use it again.
Volume.dispose();
e.preventDefault();
NativeApplication.nativeApplication.exit();
}
}
private function onResize(e:*=null):void
{
if (_txt)
{
_txt.width = stage.stageWidth * (1 / DeviceInfo.dpiScaleMultiplier);
C.x = 0;
C.y = _txt.y + _txt.height + 0;
C.width = stage.stageWidth * (1 / DeviceInfo.dpiScaleMultiplier);
C.height = 300 * (1 / DeviceInfo.dpiScaleMultiplier);
}
if (_list)
{
_numRows = Math.floor(stage.stageWidth / (BTN_WIDTH * DeviceInfo.dpiScaleMultiplier + BTN_SPACE));
_list.row = _numRows;
_list.itemArrange();
}
if (_body)
{
_body.y = stage.stageHeight - _body.height;
}
}
private function init():void
{
// initialize the extension
Volume.init();
// listen to possible error messages which may happen on iOS side. explained in details at the bottom of this document.
Volume.service.addEventListener(VolumeEvent.ERROR, onError);
// if you don't need the following two services, you shouldn't listen to them at all. because when you're listening to them,
// the ANE will register an observer on the native side to check for the values. and if you remove them, those native observers
// will also be unregistered.
Volume.service.addEventListener(VolumeEvent.VOLUME_CHANGE, onDeviceVolumeChanged);
Volume.service.addEventListener(VolumeEvent.MUTE_STATE, onDeviceMuteChanged);
Volume.service.addEventListener(VolumeEvent.SOUND_LOADED, onSoundLoaded);
// ----------------------------------------------------------------------------
var btn1:MySprite = createBtn("play my music");
btn1.addEventListener(MouseEvent.CLICK, start);
_list.add(btn1);
function start(e:MouseEvent):void
{
if(!_myMusic) _myMusic = (new MySound) as Sound;
var btn:MySprite = e.target as MySprite;
if (btn.data.txt.text == "play my music")
{
btn.data.txt.text = "stop my music";
_myMusicChannel = _myMusic.play(0, 999);
}
else if (btn.data.txt.text == "stop my music")
{
btn.data.txt.text = "play my music";
_myMusicChannel.stop();
}
}
// ----------------------------------------------------------------------------
var btn2:MySprite = createBtn("Request Audio Focus");
btn2.addEventListener(MouseEvent.CLICK, setAudioFocus);
_list.add(btn2);
function setAudioFocus(e:MouseEvent):void
{
var btn:MySprite = e.target as MySprite;
if (btn.data.txt.text == "Request Audio Focus")
{
C.log("request Focus result = ", Volume.requestFocus());
btn.data.txt.text = "Abandon Audio Focus";
}
else if (btn.data.txt.text == "Abandon Audio Focus")
{
C.log("abandon Focus result = ", Volume.abandonFocus());
btn.data.txt.text = "Request Audio Focus";
}
}
// ----------------------------------------------------------------------------
var btn3:MySprite = createBtn("volume +");
btn3.addEventListener(MouseEvent.CLICK, increaseVolume);
_list.add(btn3);
function increaseVolume(e:MouseEvent):void
{
Volume.value = Volume.value + 0.1;
}
// ----------------------------------------------------------------------------
var btn4:MySprite = createBtn("volume -");
btn4.addEventListener(MouseEvent.CLICK, decreaseVolume);
_list.add(btn4);
function decreaseVolume(e:MouseEvent):void
{
Volume.value = Volume.value - 0.1;
}
// ----------------------------------------------------------------------------
var btn9:MySprite = createBtn("1) init Native Sound");
btn9.addEventListener(MouseEvent.CLICK, initNativeSound);
_list.add(btn9);
function initNativeSound(e:MouseEvent):void
{
Volume.initNativeSoundObject()
}
// ----------------------------------------------------------------------------
var btn10:MySprite = createBtn("2) load sound effects");
btn10.addEventListener(MouseEvent.CLICK, loadSounds);
_list.add(btn10);
function loadSounds(e:MouseEvent):void
{
// you should be listening to VolumeEvent.SOUND_LOADED to get the sound objects
Volume.getNativeSoundObject(File.applicationDirectory.resolvePath("sound01.mp3"));
Volume.getNativeSoundObject(File.applicationDirectory.resolvePath("sound02.mp3"));
}
// ----------------------------------------------------------------------------
var btn11:MySprite = createBtn("3) play sound effects");
btn11.addEventListener(MouseEvent.CLICK, playSounds);
_list.add(btn11);
function playSounds(e:MouseEvent):void
{
_nativeSound1.play();
_nativeSound2.play();
}
// ----------------------------------------------------------------------------
var btn12:MySprite = createBtn("stop sound effects");
btn12.addEventListener(MouseEvent.CLICK, stopSounds);
_list.add(btn12);
function stopSounds(e:MouseEvent):void
{
Volume.stopAllNativeSounds();
}
// ----------------------------------------------------------------------------
var btn13:MySprite = createBtn("dispose sounds");
btn13.addEventListener(MouseEvent.CLICK, disposeSounds);
_list.add(btn13);
function disposeSounds(e:MouseEvent):void
{
Volume.disposeAllNativeSounds();
}
// ----------------------------------------------------------------------------
}
private function onSoundLoaded(e:VolumeEvent):void
{
// save a reference to sound objects for later use.
C.log("onSoundLoaded: " + e.nativeSound.file);
switch(e.nativeSound.file)
{
case "sound01.mp3":
_nativeSound1 = e.nativeSound;
break;
case "sound02.mp3":
_nativeSound2 = e.nativeSound;
break;
}
}
private function onDeviceVolumeChanged(e:VolumeEvent):void
{
C.log("volume = " + e.param);
}
private function onDeviceMuteChanged(e:VolumeEvent):void
{
C.log("is device mute? " + e.param);
}
private function onError(e:VolumeEvent):void
{
C.log("onError = " + e.param);
/*
If you are seeing AIR_SDK_BUG_ABANDON_FOCUS it means:
unfortunately there is a bug in iOS Adobe Air SDK which cannot abandon audio focus once obtained.
If you are seeing AIR_SDK_BUG_REQUEST_FOCUS it means:
Again this error is related to the former one above! but it happens only when you have played a sound
in your Air app prior to calling the Volume.requestFocus() method. Let me put it this way: When your
app starts, you MUST obtain focus by calling Volume.requestFocus() before you start playing ANY sound.
Once any sound is played in your Air app, due to the Air SDK bug explained above, you will never again
be able to obtain the audio focus unless your app starts again. So, always make sure to request for
audio focus before playing any sound in your app.
On the Android side though, everything works with no problem. you can simply request focus and abandon it
anytime you wish no matter if you are playing any sound or music in your app.
*/
}
private function createBtn($str:String):MySprite
{
var sp:MySprite = new MySprite();
sp.addEventListener(MouseEvent.MOUSE_OVER, onOver);
sp.addEventListener(MouseEvent.MOUSE_OUT, onOut);
sp.addEventListener(MouseEvent.CLICK, onOut);
sp.bgAlpha = 1;
sp.bgColor = 0xDFE4FF;
sp.drawBg();
sp.width = BTN_WIDTH * DeviceInfo.dpiScaleMultiplier;
sp.height = BTN_HEIGHT * DeviceInfo.dpiScaleMultiplier;
function onOver(e:MouseEvent):void
{
sp.bgAlpha = 1;
sp.bgColor = 0xFFDB48;
sp.drawBg();
}
function onOut(e:MouseEvent):void
{
sp.bgAlpha = 1;
sp.bgColor = 0xDFE4FF;
sp.drawBg();
}
var format:TextFormat = new TextFormat("Arimo", 16, 0x666666, null, null, null, null, null, TextFormatAlign.CENTER);
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.antiAliasType = AntiAliasType.ADVANCED;
txt.mouseEnabled = false;
txt.multiline = true;
txt.wordWrap = true;
txt.scaleX = txt.scaleY = DeviceInfo.dpiScaleMultiplier;
txt.width = sp.width * (1 / DeviceInfo.dpiScaleMultiplier);
txt.defaultTextFormat = format;
txt.text = $str;
txt.y = sp.height - txt.height >> 1;
sp.addChild(txt);
sp.data.txt = txt;
return sp;
}
}
}