-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.Media.js
706 lines (653 loc) · 34 KB
/
API.Media.js
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/**
* Copyright (c) 2014 by Center Open Middleware. All Rights Reserved.
* Titanium Appcelerator 3.3.0GA
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
"use strict";
/**
* PhotoGalleryOptionsType.
* @typedef {Object} PhotoGalleryOptionsType
* @property {callback} success
* @property {callback} error
* @property {callback} cancel
*/
/* FYI: http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Media*/
var Media = function (APIReferences) {
var Yaast = {
API: APIReferences
};
/** It allows to manage photo gallery and other native services.
* @author Santiago Blanco & Carlos Blanco
* @version 1.0.0
* @alias API.Media
* @namespace */
var self = {};
/** Opens the photo gallery image picker.
* @param {PhotoGalleryOptionsType} */
self.openPhotoGallery = function openPhotoGallery(options) {
Titanium.Media.openPhotoGallery(options);
};
/** Takes a screen shot of the visible UI on the device. This method is
* asynchronous.
* @param {screenSotCallback} callback that will receive screenshot image as Blob object */
self.takeScreenshot = function takeScreenshot(callback) {
Titanium.Media.takeScreenshot(callback);
};
/** Takes a screen shot of the visible UI on the device. This method is
* asynchronous.
* @param {pattern} [Number[]=[100, 300, 100, 200, 100, 50]] optional vibrate pattern only available for Android.*/
self.vibrate = function vibrate(pattern) {
if (Yaast.API.HW.System.isApple() || pattern == null || !(pattern instanceof Array)) {
Ti.Media.vibrate();
}
// pattern only available for Android
Titanium.Media.vibrate(pattern);
};
// Audio Player
/** audioPlayerOptions
* @typedef {Object} audioPlayerOptions
* @property {Boolean} paused
* @property {Boolean} paused
* @property {Boolean} playing
* @property {String} url
* @property {Number} [0.0 ... 1.0] volume
* On iOS, to adjust the volume of the device, set the volume property
* of Titanium.Media.appMusicPlayer and set the Titanium.Media.audioSessionMode
* property to either Titanium.Media.AUDIO_SESSION_MODE_AMBIENT,
* Titanium.Media.AUDIO_SESSION_MODE_SOLO_AMBIENT,
* or Titanium.Media.AUDIO_SESSION_MODE_PLAYBACK.
*/
var audioPlayerList = {};
var audioPlayerCounters = {};
var audioPlayerId = 0;
var APHandlers = {};
var APHandlersinfo = {};
/** Create new AudioPlayer
* @param {audioPlayerOptions} options
* @return {Number} audioPlayer ID*/
self.createAudioPlayer = function createAudioPlayer(options) {
var tiAudioPlayer;
audioPlayerId ++;
Ti.API.info('[API.Media.createAudioPlayer] ID: ' + audioPlayerId);
tiAudioPlayer = Ti.Media.createSound({});
// Events
APHandlers[audioPlayerId] = {'progress': [], 'change': [], 'complete': []};
APHandlersinfo[audioPlayerId] = {'progress': [], 'change': [], 'complete': []};
// Creation
audioPlayerCounters[audioPlayerId] = {
'progress': 0,
'change': 0,
'success': 0
};
audioPlayerList[audioPlayerId] = tiAudioPlayer;
Ti.API.info('[API.Media] Audio Player created: ' + audioPlayerId);
return audioPlayerId;
};
/** Check if a player is paused
* @param {Number} playerId, the audio player ID number
* @return {Boolean} Boolean indicating if audio playback is paused */
self.isAudioPlayerPaused = function isAudioPlayerPaused(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.isAudioPlayerPaused] Unknown Audio Player ID: ' + audioPlayerId);
return false;
}
Ti.API.info('[API.Media.isAudioPlayerPaused] Audio Player paused: ' + audioPlayerList[playerId].paused);
return audioPlayerList[playerId].paused;
};
/** Check if a player is playing
* @param {Number} playerId, the audio player ID number
* @return {Boolean} Boolean indicating if audio playback is playing */
self.isAudioPlayerPlaying = function isAudioPlayerPlaying(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.isAudioPlayerPlaying] Unknown Audio Player ID: ' + audioPlayerId);
return false;
}
Ti.API.info('[API.Media.isAudioPlayerPlaying] Audio Player playing: ' + audioPlayerList[playerId].playing);
return audioPlayerList[playerId].playing;
};
/** Get Audio Player URL
* @param {Number} playerId, the audio player ID number
* @return {String} The Audio Player URL */
self.getAudioPlayerURL = function getAudioPlayerURL(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.getAudioPlayerURL] Unknown Audio Player ID: ' + audioPlayerId);
return false;
}
Ti.API.info('[API.Media.getAudioPlayerURL] Audio Player URL: ' + audioPlayerList[playerId].url);
return audioPlayerList[playerId].url;
};
/** Set Audio Player URL
* @param {Number} playerId, the audio player ID number
* @param {String} url, The Audio Player URL */
self.setAudioPlayerURL = function getAudioPlayerURL(playerId, url) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.setAudioPlayerURL] Unknown Audio Player ID: ' + playerId);
return false;
}
audioPlayerList[playerId].url = url;
Ti.API.info('[API.Media.setAudioPlayerURL] Audio Player URL changed: ' + audioPlayerList[playerId].url);
return true;
};
/** Get Audio Player volume
* @param {Number} playerId, the audio player ID number
* @return {Number} The Audio Player volume */
self.getAudioPlayerVolume = function getAudioPlayerVolume(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.getAudioPlayerVolume] Unknown Audio Player id: ' + playerId);
return false;
}
Ti.API.info('[API.Media.getAudioPlayerVolume] AudioPlayer ' + playerId + ' volume:' + audioPlayerList[playerId].volume);
return audioPlayerList[playerId].volume;
};
/** Set Audio Player volume
* @param {Number} playerId, the audio player ID number
* @param {Number} volume, volume of the audio, from 0.0 (muted) to 1.0 (loudest) */
self.setAudioPlayerVolume = function getAudioPlayerVolume(playerId, volume) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.setAudioPlayerVolume] Unknown Audio Player id: ' + playerId);
return false;
}
// TODO: check iOS
audioPlayerList[playerId].volume = volume;
Ti.API.info('[API.Media.setAudioPlayerVolume] AudioPlayer ' + playerId + ' new volume: ' + audioPlayerList[playerId].volume);
return true;
};
/** Pause Audio Player
* @param {Number} playerId, the audio player ID number */
self.pauseAudioPlayer = function pauseAudioPlayer(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.pauseAudioPlayer] Unknown Audio Player id: ' + playerId);
return false;
}
audioPlayerList[playerId].pause();
Ti.API.info('[API.Media.pauseAudioPlayer] AudioPlayer ' + playerId + ' Paused!');
return true;
};
/** Play Audio Player
* @param {Number} playerId, the audio player ID number */
self.playAudioPlayer = function playAudioPlayer(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.playAudioPlayer] Unknown Audio Player id: ' + playerId);
return false;
}
if (audioPlayerList[playerId].paused == true) {
audioPlayerList[playerId].start();
Ti.API.info('[API.Media.playAudioPlayer] Resume player ' + playerId + ' with URL: ' + audioPlayerList[playerId].url);
} else {
audioPlayerList[playerId].play();
Ti.API.info('[API.Media.playAudioPlayer] Play activated in player ' + playerId + ' with URL: ' + audioPlayerList[playerId].url);
}
return true;
};
/** Stop Audio Player
* @param {Number} playerId, the audio player ID number */
self.stopAudioPlayer = function stopAudioPlayer(playerId) {
if (audioPlayerList[playerId] == null) {
Ti.API.info('[API.Media.stopAudioPlayer] Unknown Audio Player ID: ' + playerId);
//TODO: error. Unknown Audio Player ID
return false;
}
Ti.API.info('[API.Media.stopAudioPlayer] Audio player playing: ' + audioPlayerList[playerId].playing + '. paused: ' + audioPlayerList[playerId].paused);
if (!audioPlayerList[playerId].playing && !audioPlayerList[playerId].paused) {
Ti.API.info('[API.Media.stopAudioPlayer] Audio player ' + playerId + '. Is stopped yet');
/*// Only for AudioPlayer. not for Sound
if(Yaast.API.HW.System.isApple()){
audioPlayerList[playerId].stop();
}
else {*/
//audioPlayerList[playerId].stop();
Ti.API.info('[API.Media.stopAudioPlayer] *release()');
audioPlayerList[playerId].release();
//}
return false;
}
audioPlayerList[playerId].release();
/*// Only for AudioPlayer. not for Sound
if (Yaast.API.HW.System.isApple()) {
audioPlayerList[playerId].stop();
} else {
Ti.API.info('[API.Media.stopAudioPlayer] release() for Android ');
audioPlayerList[playerId].release();
}*/
Ti.API.info('[API.Media.stopAudioPlayer] Stopping Audio player (release)' + playerId);
return true;
};
/** Release Audio Player
* @param {Number} playerId, the audio player ID number */
self.releaseAudioPlayer = function releaseAudioPlayer(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.releaseAudioPlayer] Unknown Audio Player id: ' + playerId);
return false;
}
audioPlayerList[playerId].release();
Ti.API.info('[API.Media.releaseAudioPlayer] AudioPlayer ' + playerId + ' Paused!');
return true;
};
/** Reset Audio Player
* @param {Number} playerId, the audio player ID number */
self.resetAudioPlayer = function resetAudioPlayer(playerId) {
if (audioPlayerList[playerId] == null) {
//TODO: error. Unknown Audio Player ID
Ti.API.info('[API.Media.resetAudioPlayer] Unknown Audio Player id: ' + playerId);
return false;
}
audioPlayerList[playerId].reset();
Ti.API.info('[API.Media.resetAudioPlayer] AudioPlayer ' + playerId + ' Paused!');
return true;
};
// TODO
// Private?
self.convertPointToView = function convertPointToView(point, destinationView) {
if (destinationView == null) {
destinationView = 'Titanium.UI.View';
}
convertPointToView(point, destinationView);
};
// Video Player
/** videoPlayerOptions
* @typedef {Object} videoPlayerOptions
* TODO: check accesibility methods
* @property {Boolean} autoplay
* @property {String} backgroundColor
* @property {Boolean} backgroundRepeat
* @property {String} borderColor
* @property {Number} borderRadius
* @property {Number} borderWidth
* @property {Number/String} bottom, View's bottom position. Can be either a float value or a dimension string (for example, '50%' or '10px')
* @property {Number/String} top, View's top position. Can be either a float value or a dimension string (for example, '50%' or '10px')
* @property {Number/String} left, View's left position. Can be either a float value or a dimension string (for example, '50%' or '10px')
* @property {Number/String} rigth, View's right position. Can be either a float value or a dimension string (for example, '50%' or '10px')
* @property {Number/String} height, View's height. Can be either a float value or a dimension string (for example, '50%' or '40dp')
* @property {Number/String} width, View's width. Can be either a float value or a dimension string (for example, '50%' or '40dp')
* @property {String} layout, Specifies how the view positions its children. One of: 'composite', 'vertical', or 'horizontal'
* @property {Number/String} height, Can be either a float value or a dimension string (for example, '50%' or '40dp')
* @property {Boolean} horizontalWrap, Determines whether the layout has wrapping behavior
* @property {Point} center, View's center position, in the parent view's coordinates. // TODO: check this pos
* @property {Number} currentPlaybackTime, Current playback time of the current movie in milliseconds
* @property {Number} duration, The duration of the current movie in milliseconds, or 0.0 if not known
* @property {Boolean} fullscreen, Determines if the movie is presented in the entire screen (obscuring all other application content)
* @property {Number} initialPlaybackTime, The start time of movie playback, in milliseconds
* @property {Number} loadState, Returns the network load state of the movie player
* @property {Number} opacity, Opacity of this view, from 0.0 (transparent) to 1.0 (opaque)
* @property {Number} playbackState, Current playback state of the video player. One of the VIDEO_PLAYBACK_STATE constants defined in Titanium.Media
* @property {Number} playing, Indicate if the player has started playing
* @property {Dimension} rect, The bounding box of the view relative to its parent, in system units
* @property {Dimension} size, The size of the view in system units
* @property {String} url, URL identifying a local or remote video to play.
* @property {Boolena} visible, Determines whether the view is visible.
* @property {Number} zIndex, Z-index stack order position, relative to other sibling views.
*/
/** point
* @typedef {Object} point
* @property {Number} x
* @property {Number} y
*/
/** dimension
* @typedef {Object} dimension
* @property {Number} x
* @property {Number} y
* @property {Number} height
* @property {Number} width
*/
var videoPlayerList = {};
var videoPlayerCounters = {};
var videoPlayerId = 0;
var VPHandlers = {};
var VPHandlersinfo = {};
/** Create new VideoPlayer
* @param {videoPlayerOptions} options
* @return {Number} videoPlayer ID*/
self.createVideoPlayer = function createVideoPlayer(viewId, options) {
var tiVideoPlayer;
videoPlayerId ++;
Ti.API.info('[API.Media.createVideoPlayer] ID: ' + videoPlayerId + ', viewId: ' + viewId + ', options: ' + JSON.stringify(options));
videoPlayerCounters[videoPlayerId] = {
'complete': 0
};
// Autoplay
if (typeof options.autoplay === 'undefined') {
options.autoplay = true;
}
// FullScreen
if (typeof options.fullscreen === 'undefined') {
options.fullscreen = '#000000';
}
// Size
if (typeof options.width === 'undefined' || typeof options.height === 'undefined') {
options.width = parseInt(Yaast.Sandbox.tabView.rect.width * 0.7);
options.height = parseInt(Yaast.Sandbox.tabView.rect.height * 0.5);
options.top = parseInt((Yaast.Sandbox.tabView.rect.height * 0.5) / 2);
options.left = parseInt((Yaast.Sandbox.tabView.rect.width * 0.3) / 2);
} else {
// Position
if (typeof options.top !== 'undefined' || typeof options.bottom !== 'undefined') {
if (typeof options.bottom === 'undefined') {
options.top = parseInt(options.top + Ti.App.componentPos[viewId].top);
} else {
options.top = parseInt(Ti.App.componentPos[viewId].top + (Ti.App.componentPos[viewId].height - options.bottom));
}
}
if (typeof options.left !== 'undefined' || typeof options.right !== 'undefined') {
if (typeof options.right === 'undefined') {
options.left = parseInt(options.left + Ti.App.componentPos[viewId].left);
} else {
options.left = parseInt(Ti.App.componentPos[viewId].left + (Ti.App.componentPos[viewId].width - options.right));
}
}
}
// Events
VPHandlers[videoPlayerId] = {'complete': []};
VPHandlersinfo[videoPlayerId] = {'complete': {}};
Ti.API.info('[API.Media.createVideoPlayer] VPHandlers: ' + JSON.stringify(VPHandlers));
Ti.API.info('[API.Media.createVideoPlayer] VPHandlersinfo: ' + JSON.stringify(VPHandlersinfo));
// Creation
tiVideoPlayer = Ti.Media.createVideoPlayer({
url : options.url,
fullscreen : options.fullscreen,
autoplay : options.autoplay,
//backgroundColor : options.background,
height: options.height,
width: options.width,
top: options.top,
left: options.left,
mediaControlStyle : Titanium.Media.VIDEO_CONTROL_NONE,
scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT
});
Yaast.Sandbox.tabView.add(tiVideoPlayer);
videoPlayerList[videoPlayerId] = tiVideoPlayer;
return videoPlayerId;
};
// TODO generic get and set methods?
self.getVideoPlayerAttribute = function getVideoPlayerAttribute(playerId, attr) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.getVideoPlayerAttribute] Unknown Video Player id: ' + playerId);
return false;
}
Ti.API.info('[API.Media.getVideoPlayerAttribute] VideoPlayer: ' + playerId + ' getting: ' + attr + ', return: ' + videoPlayerList[playerId][attr]);
return videoPlayerList[playerId][attr];
};
self.setVideoPlayerAttribute = function setVideoPlayerAttribute(playerId, attr, value) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.setVideoPlayerAttribute] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId][attr] = value;
Ti.API.info('[API.Media.setVideoPlayerAttribute] VideoPlayer: ' + playerId + ', setting: ' + attr + ', value: ' + value);
return true;
};
self.setVideoPlayerBound = function setVideoPlayerBound(viewId, playerId, options) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.setVideoPlayerBound] Unknown Video Player id: ' + playerId);
return false;
}
if (typeof options.width === 'undefined' || typeof options.height === 'undefined') {
options.width = parseInt(Yaast.Sandbox.tabView.rect.width * 0.7);
options.height = parseInt(Yaast.Sandbox.tabView.rect.height * 0.5);
options.top = 'undefined';
options.left = 'undefined';
} else {
// Position
if (typeof options.top !== 'undefined' || typeof options.bottom !== 'undefined') {
if (typeof options.bottom === 'undefined') {
options.top = parseInt(options.top + Ti.App.componentPos[viewId].top);
} else {
options.top = parseInt(Ti.App.componentPos[viewId].top + (Ti.App.componentPos[viewId].height - options.bottom));
}
}
if (typeof options.left !== 'undefined' || typeof options.right !== 'undefined') {
if (typeof options.right === 'undefined') {
options.left = parseInt(options.left + Ti.App.componentPos[viewId].left);
} else {
options.left = parseInt(Ti.App.componentPos[viewId].left + (Ti.App.componentPos[viewId].width - options.right));
}
}
}
videoPlayerList[playerId].height = options.height;
videoPlayerList[playerId].width = options.width;
videoPlayerList[playerId].top = options.top;
videoPlayerList[playerId].left = options.left;
Ti.API.info('[API.Media.setVideoPlayerBound] VideoPlayer: ' + playerId);
return true;
};
/** Hide a VideoPlayer */
self.hideVideoPlayer = function hideVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.hideVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].hide();
Ti.API.info('[API.Media.hideVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
/** Show a VideoPlayer */
self.showVideoPlayer = function showVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.showVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].show();
Ti.API.info('[API.Media.showVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
/** Pause a VideoPlayer */
self.pauseVideoPlayer = function pauseVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.pauseVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].pause();
Ti.API.info('[API.Media.pauseVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
/** Stop a VideoPlayer */
self.stopVideoPlayer = function stopVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.stopVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].stop();
Ti.API.info('[API.Media.stopVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
/** Release a VideoPlayer. Releases the internal video resources immediately.
* This is not usually necessary but can help if you no longer need to use the
* player after it is used to help converse memory */
self.releaseVideoPlayer = function releaseVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.releaseVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].release();
Ti.API.info('[API.Media.releaseVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
/** Capture an image of the rendered view, as a Blob.
* @param {callback} Function to be invoked upon completion with the
* image Titanium.Blob ass param(TODO:check this) */
self.captureVideoPlayer = function captureVideoPlayer(playerId, callback) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.captureVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
videoPlayerList[playerId].toImage(callback);
Ti.API.info('[API.Media.captureVideoPlayer] VideoPlayer: ' + playerId);
return true;
};
self.destroyVideoPlayer = function destroyVideoPlayer(playerId) {
if (videoPlayerList[playerId] == null) {
//TODO: error. Unknown Video Player ID
Ti.API.info('[API.Media.destroyVideoPlayer] Unknown Video Player id: ' + playerId);
return false;
}
// Remove events
/*for (VPHandlers[id][event]) {
VPHandlersinfo[id][event][VPHandlers[id][event].indexOf(handler)]
}*/
videoPlayerList[playerId].hide();
videoPlayerList[playerId].release();
Yaast.Sandbox.tabView.remove(videoPlayerList[playerId]);
videoPlayerList[playerId] = null;
Ti.API.info('[API.Media.destroyVideoPlayer] playerId: ' + playerId);
return true;
};
self.addEventListener = function addEventListener(event, handler, id, dummy) {
Ti.API.info('[API.Media.addEventListener] event: ' + event);
if (dummy == 'audio') {
Ti.API.info('[API.Media.addEventListener] event: ' + event + ', id: ' + id +', audioPlayerList: ' + JSON.stringify(audioPlayerList) + ', dummy: ' + dummy);
var handler_aux = function(e) {
Ti.API.info('*******************AUDIO******************************' + e.type);
if (e.type == 'complete') {
Ti.API.info('[API.Media.handler_aux false] (stopped), paused: ' + audioPlayerList[id].paused + ', playing: ' + audioPlayerList[id].playing + '---------------REAL e: ' + JSON.stringify(e));
var d = {};
d['order'] = audioPlayerCounters[id].change;
d['description'] = 'stopped';
d['state'] = 5;
d['type'] = 'change';
d['source'] = e.source;
Ti.API.info('[API.Media.handler_aux false] change ' + d['order']);
audioPlayerCounters[id].change ++;
handler(d);
return;
} else if (e.type == 'change') {
if (e.state == 5) {
Ti.API.info('[API.Media.handler_aux] ChangeEvent.stop killed');
return;
}
Ti.API.info('[API.Media.handler_aux] (' + e.description + '), paused: ' + audioPlayerList[id].paused + ', playing: ' + audioPlayerList[id].playing + '---------------REAL e: ' + JSON.stringify(e));
e['order'] = audioPlayerCounters[id].change;
Ti.API.info('[API.Media.handler_aux] change ' + e['order']);
audioPlayerCounters[id].change ++;
} else if (e.type == 'progress') {
Ti.API.info('[API.Media.handler_aux] (' + e.progress + '), paused: ' + audioPlayerList[id].paused + ', playing: ' + audioPlayerList[id].playing + '---------------REAL e: ' + JSON.stringify(e));
e['order'] = audioPlayerCounters[id].progress;
Ti.API.info('[API.Media.handler_aux] progress ' + e['order']);
audioPlayerCounters[id].progress ++;
}
handler(e);
};
APHandlers[id][event].push(handler);
APHandlersinfo[id][event][APHandlers[id][event].indexOf(handler)] = {'handler_aux': handler_aux};
audioPlayerList[id].addEventListener(event, handler_aux);
Ti.API.info('[API.Media.addEventListener] event: ' + event + ', isApple: ' + Yaast.API.HW.System.isApple());
if (event == 'change') {
Ti.API.info('[API.Media.addEventListener] event change and complete!!');
APHandlers[id]['complete'].push(handler);
APHandlersinfo[id]['complete'][APHandlers[id][event].indexOf(handler)] = {'handler_aux': handler_aux};
audioPlayerList[id].addEventListener('complete', handler_aux);
}
} else if (dummy == 'video') {
Ti.API.info('[API.Media.addEventListener] event: ' + event + ', id: ' + id +', videoPlayerList: ' + JSON.stringify(videoPlayerList) + ', dummy: ' + dummy);
var completeHandler = function completeHandler(e) {
Ti.API.info('*************************************************' + e.type);
if (e.type == 'complete') {
Ti.API.info('[API.Media.completeHandler] playing: ' + videoPlayerList[id].playing + '---------------REAL e: ' + JSON.stringify(e));
e['order'] = videoPlayerCounters[id].complete;
Ti.API.info('[API.Media.completeHandler] complete ' + e['order']);
videoPlayerCounters[id].complete ++;
}
handler(e);
};
Ti.API.info('[API.Media.addEventListener] VIDEO event: ' + event + '; VPHandlers: ' + JSON.stringify(VPHandlers));
Ti.API.info('[API.Media.addEventListener] VIDEO event: ' + event + '; VPHandlersinfo: ' + JSON.stringify(VPHandlersinfo));
VPHandlers[id][event].push(handler);
VPHandlersinfo[id][event][VPHandlers[id][event].indexOf(handler)] = {'handler_aux': completeHandler};
Ti.API.info('[API.Media.addEventListener] VIDEO event: ' + event + '; VPHandlers: ' + JSON.stringify(VPHandlers));
Ti.API.info('[API.Media.addEventListener] VIDEO event: ' + event + '; VPHandlersinfo: ' + JSON.stringify(VPHandlersinfo));
videoPlayerList[id].addEventListener(event, completeHandler);
Ti.API.info('[API.Media.addEventListener] end VIDEO addEventListener: ' + event);
} else {
Ti.API.info('[API.Media.addEventListener] event listener error. Unknown dummy: ' + dummy);
}
};
self.removeEventListener = function removeEventListener(event, handler, id, dummy) {
var theHandler, index;
Ti.API.info('[API.Media.removeEventListener]removeEventListener; event: ' + event + ', dummy: ' + dummy + ', id: ' + id);
if (dummy == 'audio') {
Ti.API.info('[API.Media.removeEventListener]AUDIO removeEventListener; event: ' + event + ', dummy: ' + dummy + ', id: ' + id +', audioPlayerList[id]: ' + JSON.stringify(audioPlayerList[id]));
audioPlayerList[id].removeEventListener(event, theHandler);
if (event == 'change') {
Ti.API.info('[API.Media.removeEventListener] removing AUDIO event change and complete!!');
index = APHandlers[id]['complete'].indexOf(handler);
theHandler = APHandlersinfo[id]['complete'][index].handler_aux;
delete APHandlersinfo[id][event][index];
APHandlers[id][event].splice(index, 1);
audioPlayerList[id].removeEventListener('complete', theHandler);
}
} else if (dummy == 'video') {
Ti.API.info('[API.Media.removeEventListener] removing VIDEO event: ' + event + ', dummy: ' + dummy + ', id: ' + id +', videoPlayerList[id]: ' + JSON.stringify(videoPlayerList[id]));
Ti.API.info('[API.Media.removeEventListener] removing VIDEO event: ' + event + '; VPHandlers: ' + JSON.stringify(VPHandlers));
Ti.API.info('[API.Media.removeEventListener] removing VIDEO event: ' + event + '; VPHandlersinfo: ' + JSON.stringify(VPHandlersinfo));
index = VPHandlers[id][event].indexOf(handler);
theHandler = VPHandlersinfo[id][event][index].handler_aux;
delete VPHandlersinfo[id][event][index];
VPHandlers[id][event].splice(index, 1);
videoPlayerList[id].removeEventListener(event, theHandler);
Ti.API.info('[API.Media.removeEventListener] removing VIDEO event: ' + event + '; VPHandlers: ' + JSON.stringify(VPHandlers));
Ti.API.info('[API.Media.removeEventListener] removing VIDEO event: ' + event + '; VPHandlersinfo: ' + JSON.stringify(VPHandlersinfo));
} else {
Ti.API.info('[API.Media.removeEventListener] error. Unknown dummy: ' + dummy);
}
};
self.events = {
/** Fired when the state of the playback changes.This event can be generated by programmatic
* events, such as pausing or stopping the audio, and also by external events, such as the
* current state of network buffering.
* @typedef {event} audioChange
* @property {Object} description. Text description of the state of playback. //TODO: check this Object. String??
* @property {Object} source. Source object that fired the event. //TODO: check this Object. Ti Object???
* @property {Number} name. Current state of playback. specified using one of the next STATE constants:
* STATE_BUFFERING, STATE_INITIALIZED, STATE_PAUSED, STATE_PLAYING, STATE_STARTING, STATE_STOPPED,
* STATE_STOPPING, STATE_WAITING_FOR_DATA, STATE_WAITING_FOR_QUEUE
* @property {String} type. Name of the event fired. */
'audioChange': {
event: 'change',
listener: null,
source: "Ti.Media.Sound",
keylist: ['description', 'state', 'type', 'order'],
dummy: true
},
/** Fired once per second with the current progress during playback.
* @typedef {event} audioChange
* @property {Object} progress. Current progress, in milliseconds. //TODO: check this Object. String??
* @property {Object} source. Source object that fired the event. //TODO: check this Object. Ti Object???
* STATE_BUFFERING, STATE_INITIALIZED, STATE_PAUSED, STATE_PLAYING, STATE_STARTING, STATE_STOPPED,
* STATE_STOPPING, STATE_WAITING_FOR_DATA, STATE_WAITING_FOR_QUEUE
* @property {String} type. Name of the event fired. */
'audioProgress': {
event: 'progress',
listener: null,
source: "Ti.Media.Sound",
keylist: ['progress', 'type', 'order'],
dummy: true
},
/* */
'videoComplete': {
event: 'complete',
listener: null,
source: "Ti.Media.VideoPlayer",
keylist: ['code', 'error', 'reason', 'success', 'type', 'order'],
dummy: true
}
};
return self;
};
module.exports = Media;