Menu

[r267]: / trunk / plugins / AdvertisementPlugin / src / org / osmf / advertisementplugin / AdvertisementPluginInfo.as  Maximize  Restore  History

Download this file

358 lines (311 with data), 11.9 kB

  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
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.advertisementplugin
{
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.utils.Dictionary;
import org.osmf.containers.MediaContainer;
import org.osmf.elements.ProxyElement;
import org.osmf.elements.VideoElement;
import org.osmf.events.AudioEvent;
import org.osmf.events.BufferEvent;
import org.osmf.events.ContainerChangeEvent;
import org.osmf.events.DisplayObjectEvent;
import org.osmf.events.PlayEvent;
import org.osmf.events.TimeEvent;
import org.osmf.events.TimelineMetadataEvent;
import org.osmf.layout.LayoutMetadata;
import org.osmf.layout.ScaleMode;
import org.osmf.media.*;
import org.osmf.metadata.TimelineMarker;
import org.osmf.metadata.TimelineMetadata;
import org.osmf.traits.PlayState;
import org.osmf.utils.OSMFSettings;
/**
* The AdvertisementPluginInfo object for the reference plug-in for advertisement.
*/
public class AdvertisementPluginInfo extends PluginInfo
{
private var prerollURL:String;
private var postrollURL:String;
private var midrollURL:String;
private var midrollTime:int;
private var overlayURL:String;
private var overlayTime:int;
private var seekWorkarround:Boolean = true;
public function AdvertisementPluginInfo()
{
super();
if (ExternalInterface.available)
{
ExternalInterface.addCallback("displayNonLiniarAd", displayNonLiniarAd);
ExternalInterface.addCallback("displayLiniarAd", displayLiniarAd);
}
}
/**
* Innitialize the plugin.
*/
override public function initializePlugin(resource:MediaResourceBase):void
{
prerollURL = resource.getMetadataValue("preroll") as String;
postrollURL = resource.getMetadataValue("postroll") as String;
midrollURL = resource.getMetadataValue("midroll") as String;
midrollTime = int(resource.getMetadataValue("midrollTime"));
if (midrollTime == 0) {
midrollTime = 10;
}
overlayURL = resource.getMetadataValue("overlay") as String;
overlayTime = int(resource.getMetadataValue("overlayTime"));
if (overlayTime == 0)
{
overlayTime = 20;
}
// Expose so that we can disable the seek WORKARROUND: http://bugs.adobe.com/jira/browse/ST-397
// GPU Decoding issue on stagevideo: Win7, Flash Player version WIN 10,2,152,26 (debug)
seekWorkarround = resource.getMetadataValue("seekWorkarround") != "false";
mediaPlayer = resource.getMetadataValue("MediaPlayer") as MediaPlayer;
mediaContainer = resource.getMetadataValue("MediaContainer") as MediaContainer;
mediaFactory = resource.getMetadataValue(PluginInfo.PLUGIN_MEDIAFACTORY_NAMESPACE) as MediaFactory;
if (prerollURL)
{
// NOTE: For progressive video the pause will not take effect immediately after the playback has started.
// That's why we need to pause the main media before it starts playing, that's why we need to handle the
// BufferEvent.BUFFERING_CHANGE event, instead of PlayEvent.PLAY_STATE_CHANGE.
// mediaPlayer.addEventListener(PlayEvent.PLAY_STATE_CHANGE, onPlayStateChange);
mediaPlayer.addEventListener(BufferEvent.BUFFERING_CHANGE, onBufferChange);
}
if (postrollURL)
{
mediaPlayer.addEventListener(TimeEvent.COMPLETE, onComplete);
}
if (midrollURL)
{
mediaPlayer.addEventListener(TimeEvent.CURRENT_TIME_CHANGE, onMidrollCurrentTimeChange);
}
if (overlayURL)
{
mediaPlayer.addEventListener(TimeEvent.CURRENT_TIME_CHANGE, onOverlayCurrentTimeChange);
}
mediaPlayer.addEventListener(AudioEvent.MUTED_CHANGE, function(event:Event):void {
for (var adPlayer:* in adPlayers)
{
adPlayer.muted = mediaPlayer.muted;
}
});
mediaPlayer.addEventListener(AudioEvent.VOLUME_CHANGE, function(event:Event):void {
for (var adPlayer:* in adPlayers)
{
adPlayer.volume = mediaPlayer.volume;
}
});
}
/*
* Displays a liniar advertisement.
*
* The method does not check if an advertisement is currently being played or not.
* This is up to the caller to check.
*
* The advertisement will use the same layout as the main media.
* @param url - the path to the advertisement media which will be displayed.
* @resumePlaybackAfterTheAd - indicates if the playback of the main media should resume after the playback of the ad.
*/
public function displayLiniarAd(url:String, resumePlaybackAfterTheAd:Boolean = true):void
{
displayAd(url, true, resumePlaybackAfterTheAd, true, null);
}
/**
* Displays a non liniar (overlay) advertisement.
* If another advertisement is already being played, the new advertisement will be added on top.
*
* @param url - the path to the media
* @param layoutMetadata - information about the advertisement layout
*/
public function displayNonLiniarAd(url:String, layoutInfo:Object):void
{
displayAd(url, false, false, true, layoutInfo);
}
// Internals
/**
* Utility function which will play an advertisement.
*
* @param url - the path to the advertisement media which will be displayed.
* @pauseTheMainMediaWhilePlayingTheAd - indicates if the main media needs to be paused while playing the ad.
* @resumePlaybackAfterTheAd - indicates if the playback of the main media should resume after the playback of the ad.
* @layoutInfo - optional LayoutMetadata.
*/
private function displayAd(url:String,
pauseTheMainMediaWhilePlayingTheAd:Boolean = true,
resumePlaybackAfterTheAd:Boolean = true,
preBufferAd:Boolean = true,
layoutInfo:Object = null):void
{
// Setup the Advertisement
var adMediaElement:MediaElement = mediaFactory.createMediaElement(new URLResource(url));
// Set the layout metadata, if present
if (layoutInfo != null)
{
var layoutMetadata:LayoutMetadata = new LayoutMetadata();
for (var key:String in layoutInfo)
{
layoutMetadata[key] = layoutInfo[key];
}
layoutMetadata.index = adPlayerCount + 100;
adMediaElement.metadata.addValue(LayoutMetadata.LAYOUT_NAMESPACE, layoutMetadata);
}
var adMediaPlayer:MediaPlayer = new MediaPlayer();
adMediaPlayer.autoPlay = true;
adMediaPlayer.media = adMediaElement;
adPlayers[adMediaPlayer] = true;
adPlayerCount ++;
// Copy the volume values
adMediaPlayer.volume = mediaPlayer.volume;
// TODO: Implement prebuffering behaviour
adMediaPlayer.bufferTime = 1;
adMediaPlayer.addEventListener(TimeEvent.COMPLETE, onAdComplete);
adMediaPlayer.muted = true;
// preBufferAd = false;
if (preBufferAd || adMediaPlayer.canPlay)
{
adMediaPlayer.addEventListener(BufferEvent.BUFFERING_CHANGE, onBufferingChange);
}
else
{
playAd();
}
function onBufferingChange(event:BufferEvent):void
{
if (event.buffering == false)
{
adMediaPlayer.removeEventListener(BufferEvent.BUFFERING_CHANGE, onBufferingChange);
playAd();
}
}
function playAd():void
{
adMediaPlayer.muted = mediaPlayer.muted;
if (pauseTheMainMediaWhilePlayingTheAd)
{
mediaPlayer.media.metadata.addValue("Advertisement", url);
mediaPlayer.pause();
if (mediaContainer.containsMediaElement(mediaPlayer.media))
{
mediaContainer.removeMediaElement(mediaPlayer.media);
}
else
{
mediaPlayer.media.addEventListener(ContainerChangeEvent.CONTAINER_CHANGE,
function(event:ContainerChangeEvent):void
{
if (mediaContainer.containsMediaElement(mediaPlayer.media))
{
mediaContainer.removeMediaElement(mediaPlayer.media);
}
});
}
}
mediaContainer.addMediaElement(adMediaElement);
}
function onAdComplete(event:Event):void
{
var adMediaPlayer:MediaPlayer = event.target as MediaPlayer;
adMediaPlayer.removeEventListener(TimeEvent.COMPLETE, onAdComplete);
mediaContainer.removeMediaElement(adMediaPlayer.media);
adPlayerCount --;
delete adPlayers[adMediaPlayer];
adMediaPlayer = null;
if (pauseTheMainMediaWhilePlayingTheAd)
{
mediaPlayer.media.metadata.removeValue("Advertisement");
mediaContainer.addMediaElement(mediaPlayer.media);
}
if (pauseTheMainMediaWhilePlayingTheAd && resumePlaybackAfterTheAd)
{
// WORKARROUND: http://bugs.adobe.com/jira/browse/ST-397 - GPU Decoding issue on stagevideo: Win7, Flash Player version WIN 10,2,152,26 (debug)
// if (seekWorkarround && mediaPlayer.canSeek)
// {
// mediaPlayer.seek(mediaPlayer.currentTime);
// }
mediaPlayer.play();
}
}
}
/**
* Sample Non-Liniar advertisment code. Uses the flash vars configuration
*/
private function onOverlayCurrentTimeChange(event:TimeEvent):void
{
if (mediaPlayer.currentTime > overlayTime)
{
mediaPlayer.removeEventListener(TimeEvent.CURRENT_TIME_CHANGE, onOverlayCurrentTimeChange);
var overlayTime:int = 10;
var overlayMetadata:Object = {
right: 10,
bottom: 10,
width: 200,
height: 140,
scaleMode: ScaleMode.STRETCH
};
displayNonLiniarAd(overlayURL, overlayMetadata);
}
}
// Liniar ads
/**
* Display the pre roll advertisement.
*/
private function onBufferChange(event:BufferEvent):void
{
if (event.buffering)
{
mediaPlayer.removeEventListener(BufferEvent.BUFFERING_CHANGE, onBufferChange);
// Do not prebuffer the Advertisement if playing a preroll ad.
// Let the main content prebuffer while the ad is playing instead.
displayAd(prerollURL, true, true, false, null);
}
}
/**
* Display the mid roll advertisement.
*/
private function onMidrollCurrentTimeChange(event:TimeEvent):void
{
if (mediaPlayer.currentTime > midrollTime)
{
mediaPlayer.removeEventListener(TimeEvent.CURRENT_TIME_CHANGE, onMidrollCurrentTimeChange);
displayLiniarAd(midrollURL);
}
}
/**
* Display the post roll advertisement.
*/
private function onComplete(event:Event):void
{
mediaPlayer.removeEventListener(TimeEvent.COMPLETE, onComplete);
// Resume the playback after the add only if loop is set to true
displayLiniarAd(postrollURL, mediaPlayer.loop);
}
private var mediaPlayer:MediaPlayer;
private var mediaContainer:MediaContainer;
private var mediaFactory:MediaFactory;
private var adPlayerCount:int = 0;
private var adPlayers:Dictionary = new Dictionary();
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.