<?xml version="1.0" encoding="utf-8"?>
<!--
/*****************************************************
*
* Copyright 2009 Akamai Technologies, Inc. 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 Akamai Technologies, Inc.
* Portions created by Akamai Technologies, Inc. are Copyright (C) 2009 Akamai
* Technologies, Inc. All Rights Reserved.
*
*****************************************************/
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns:samples="org.osmf.samples.*" applicationComplete="initApp()"
backgroundColor="#000000">
<mx:Style source="AkamaiPluginSample.css" />
<mx:Script>
<![CDATA[
import org.osmf.net.StreamType;
import org.osmf.net.StreamingURLResource;
import mx.controls.Alert;
import org.osmf.media.DefaultMediaFactory;
import org.osmf.plugin.PluginInfoResource;
import org.osmf.traits.LoadTrait;
import mx.events.DropdownEvent;
import org.osmf.events.*;
import org.osmf.media.MediaResourceBase;
import org.osmf.media.URLResource;
import org.osmf.media.MediaElement;
import org.osmf.media.MediaFactory;
import org.osmf.media.MediaFactoryItem;
import org.osmf.metadata.*;
import org.osmf.net.NetLoader;
import org.osmf.plugin.PluginManager;
import org.osmf.traits.LoadTrait;
import org.osmf.traits.LoadState;
import org.osmf.traits.MediaTraitType;
import org.osmf.utils.*;
import org.osmf.elements.VideoElement;
private var pluginManager:PluginManager;
private var mediaFactory:DefaultMediaFactory;
private var pluginElement:MediaElement;
private var sliderDragging:Boolean;
private var waitForSeek:Boolean;
private var pluginResourceMap:Dictionary;
import com.akamai.osmf.AkamaiBasicStreamingPluginInfo;
import org.osmf.mast.MASTPluginInfo;
import org.osmf.smil.SMILPluginInfo;
private static const DEFAULT_PROGRESS_DELAY:uint = 100;
private static const MAX_VIDEO_WIDTH:int = 640;
private static const MAX_VIDEO_HEIGHT:int = 360;
private static const AKAMAI_BASIC_STREAMING_PLUGIN_INFOCLASS:String = "com.akamai.osmf.AkamaiBasicStreamingPluginInfo";
private static const MAST_PLUGIN_INFOCLASS:String = "org.osmf.mast.MASTPluginInfo";
private static const SMIL_PLUGIN_INFOCLASS:String = "org.osmf.smil.SMILPluginInfo";
// We need these or getDefinitionByName will fail:
private static const forceReferenceAkamai:AkamaiBasicStreamingPluginInfo = null;
private static const forceReferenceMAST:MASTPluginInfo = null;
private static const forceReferenceSMIL:SMILPluginInfo = null;
// ------ TEST CONTENT ------
// FLV
private static const PROGRESSIVE_FLV:String = "http://mediapm.edgesuite.net/strobe/content/test/AFaerysTale_sylviaApostol_640_500_short.flv";
private static const PROGRESSIVE_FLV_2:String = "http://mediapm.edgesuite.net/osmf/content/test/akamai_10_year_f8_512K.flv";
private static const PROGRESSIVE_FLV_SSL:String = "https://a248.e.akamai.net/7/248/67129/v0001/mediapm.download.akamai.com/67129/osmf/content/test/akamai_10_year_f8_512K.flv";
private static const STREAMING_FLV:String = "rtmp://cp67126.edgefcs.net/ondemand/mediapm/osmf/content/test/akamai_10_year_f8_512K";
// MP4
private static const PROGRESSIVE_MP4:String = "http://mediapm.edgesuite.net/osmf/content/test/spacealonehd_sounas_640_700.mp4";
private static const PROGRESSIVE_MP4_SSL:String = "https://a248.e.akamai.net/7/248/67129/v0001/mediapm.download.akamai.com/67129/osmf/content/test/spacealonehd_sounas_640_700.mp4";
private static const STREAMING_MP4:String = "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/osmf/content/test/spacealonehd_sounas_640_700.mp4";
// F4V
private static const PROGRESSIVE_F4V:String = "http://mediapm.edgesuite.net/osmf/content/test/sample1_700kbps.f4v";
private static const PROGRESSIVE_F4V_SSL:String = "https://a248.e.akamai.net/7/248/67129/v0001/mediapm.download.akamai.com/67129/osmf/content/test/sample1_700kbps.f4v";
private static const STREAMING_F4V:String = "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/osmf/content/test/sample1_700kbps.f4v";
// STREAM WITH AUTH
private static const REMOTE_STREAM_WITH_AUTH:String = "rtmp://cp78634.edgefcs.net/ondemand/mp4:mediapmsec/osmf/content/test/SpaceAloneHD_sounas_640_700.mp4?auth=daEc2a9a5byaMa.avcxbiaoa8dBcibqbAa8-bkxDGK-b4toa-znnrqzzBvl&aifp=v0001";
// LIVE
private static const REMOTE_LIVE:String = "rtmp://cp34973.live.edgefcs.net/live/Flash_Live_Benchmark@632";
// MISC
private static const REMOTE_STREAM:String = "rtmp://cp67126.edgefcs.net/ondemand/mediapm/strobe/content/test/SpaceAloneHD_sounas_640_500_short";
// MOV
private static const PROGRESSIVE_MOV:String = "http://mediapm.edgesuite.net/osmf/content/test/akamai_10_year_500.mov";
private static const PROGRESSIVE_MOV_SSL:String = "https://a248.e.akamai.net/7/248/67129/v0001/mediapm.download.akamai.com/67129/osmf/content/test/akamai_10_year_500.mov";
private static const STREAMING_MOV:String = "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/osmf/content/test/akamai_10_year_500.mov";
// AUDIO
private static const STREAMING_AUDIO:String = "rtmp://cp67126.edgefcs.net/ondemand/mp3:mediapm/osmf/content/test/train_1500";
// MAST documents
private static const MAST_URL_POSTROLL:String = "http://mediapm.edgesuite.net/osmf/content/mast/mast_sample_onitemend.xml";
private static const MAST_URL_PREROLL:String = "http://mediapm.edgesuite.net/osmf/content/mast/mast_sample_onitemstart.xml";
// SMIL documents
private static const SMIL_TEST_1:String = "http://mediapm.edgesuite.net/osmf/content/test/smil/elephants_dream.smil";
private static const SMIL_TEST_2:String = "http://www.streamflashhd.com/video/train.smil";
private static const SMIL_TEST_3:String = "http://mediapm.edgesuite.net/osmf/content/test/smil/sweet.smil";
private static const SMIL_TEST_4:String = "http://mediapm.edgesuite.net/osmf/content/test/smil/seq-test.smil";
[Bindable]
private var plugins:Array = [ {label:"Akamai Basic Streaming Plugin", url:AKAMAI_BASIC_STREAMING_PLUGIN_INFOCLASS},
{label:"MAST plugin", url:MAST_PLUGIN_INFOCLASS},
{label:"SMIL plugin", url:SMIL_PLUGIN_INFOCLASS}
];
[Bindable]
private var streams:Array = [ {label:"Progressive FLV", url:PROGRESSIVE_FLV},
{label:"Progressive FLV (another)", url:PROGRESSIVE_FLV_2},
{label:"Progressive FLV (https)", url:PROGRESSIVE_FLV_SSL},
{label:"Streaming FLV", url:STREAMING_FLV},
{label:"Progressive MP4", url:PROGRESSIVE_MP4},
{label:"Progressive MP4 (https)", url:PROGRESSIVE_MP4_SSL},
{label:"Streaming MP4", url:STREAMING_MP4},
{label:"Progressive F4V", url:PROGRESSIVE_F4V},
{label:"Progressive F4V (https)", url:PROGRESSIVE_F4V_SSL},
{label:"Streaming F4V", url:STREAMING_F4V},
{label:"Stream with Auth", url:REMOTE_STREAM_WITH_AUTH},
{label:"Live", url:REMOTE_LIVE},
{label:"Remote Stream", url:REMOTE_STREAM},
{label:"Progressive MOV", url:PROGRESSIVE_MOV},
{label:"Progressive MOV (https)", url:PROGRESSIVE_MOV_SSL},
{label:"Streaming MOV", url:STREAMING_MOV},
{label:"Streaming Audio", url:STREAMING_AUDIO},
{label:"RTMP Dynamic Streaming 1 (SMIL)", url:SMIL_TEST_1},
{label:"RTMP Dynamic Streaming 2 (SMIL)", url:SMIL_TEST_2},
{label:"RTMP Dynamic Streaming 3 (SMIL)", url:SMIL_TEST_3},
{label:"SerialElement (SMIL)", url:SMIL_TEST_4},
];
private var _currentInfoLineNum:int;
private function initApp():void
{
mediaFactory = new DefaultMediaFactory();
pluginManager = new PluginManager(mediaFactory);
urlInput.text = plugins[0].url;
mediaInput.text = streams[0].url;
mediaPlayerWrapper.mediaPlayer.addEventListener(DisplayObjectEvent.MEDIA_SIZE_CHANGE, onMediaSizeChange);
mediaPlayerWrapper.mediaPlayer.addEventListener(TimeEvent.DURATION_CHANGE, onDurationChange);
mediaPlayerWrapper.mediaPlayer.addEventListener(TimeEvent.CURRENT_TIME_CHANGE, onCurrentTimeChange);
mediaPlayerWrapper.mediaPlayer.addEventListener(SeekEvent.SEEK_END, onSeekingChange);
mediaPlayerWrapper.mediaPlayer.currentTimeUpdateInterval = DEFAULT_PROGRESS_DELAY;
sliderDragging = false;
waitForSeek = false;
clearInfoText();
}
private function isLiveStream(url:String):Boolean
{
var isLive:Boolean = false;
if (url == REMOTE_LIVE)
{
isLive = true;
}
return isLive;
}
private function isMASTPluginLoaded():Boolean
{
var isLoaded:Boolean = false;
if (pluginResourceMap && pluginResourceMap[MAST_PLUGIN_INFOCLASS] != null)
{
isLoaded = true;
}
return isLoaded;
}
private function loadMedia(url:String):void
{
var resource:URLResource = new StreamingURLResource(url, isLiveStream(url) ? StreamType.LIVE : StreamType.RECORDED);
if (isMASTPluginLoaded())
{
// Assign to the resource the metadata that indicates that it should have a MAST
// document applied (and include the URL of that MAST document).
var kvFacet:KeyValueFacet = new KeyValueFacet("http://www.akamai.com/mast");
kvFacet.addValue(new ObjectIdentifier("url"), MAST_URL_PREROLL);
resource.metadata.addFacet(kvFacet);
}
var mediaElement:MediaElement = mediaFactory.createMediaElement(resource);
if (mediaElement == null)
{
Alert.show("No MediaInfo was found for the media. Did you load the proper plugin?");
}
else
{
// Listen for traits to be added, so we can adjust the UI. For example, enable the seek bar
// when ISeekable is added
mediaElement.addEventListener(MediaElementEvent.TRAIT_ADD, onTraitAdd, false, 0, true);
mediaElement.addEventListener(MediaElementEvent.TRAIT_REMOVE, onTraitRemove, false, 0, true);
mediaElement.addEventListener(MediaErrorEvent.MEDIA_ERROR, onMediaError, false, 0, true);
mediaPlayerWrapper.element = mediaElement;
enablePlayerControls(true);
}
}
private function unloadMedia():void
{
var mediaElement:MediaElement = mediaPlayerWrapper.element;
if (mediaElement != null && mediaElement.hasTrait(MediaTraitType.LOAD))
{
var loadTrait:LoadTrait = mediaElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
if (loadTrait.loadState == LoadState.READY)
{
loadTrait.unload();
}
}
enablePlayerControls(false);
}
private function loadPlugin(source:String):void
{
var pluginResource:MediaResourceBase;
if (source.substr(0, 4) == "http" || source.substr(0, 4) == "file")
{
// This is a URL, create a URLResource
pluginResource = new URLResource(source);
}
else
{
// Assume this is a class
var pluginInfoRef:Class = flash.utils.getDefinitionByName(source) as Class;
pluginResource = new PluginInfoResource(new pluginInfoRef);
}
if (pluginResourceMap == null)
{
pluginResourceMap = new Dictionary();
}
pluginResourceMap[source] = pluginResource;
loadPluginFromResource(pluginResource);
}
private function loadPluginFromResource(pluginResource:MediaResourceBase):void
{
pluginManager.addEventListener(PluginManagerEvent.PLUGIN_LOAD, onPluginLoaded);
pluginManager.addEventListener(PluginManagerEvent.PLUGIN_LOAD_ERROR, onPluginLoadFailed);
pluginManager.loadPlugin(pluginResource);
}
private function onPluginLoaded(event:PluginManagerEvent):void
{
var selection:Object = cbPlugin.selectedItem;
appendInfoText("Plugin LOADED: " + selection.label);
removePluginManagerEventListeners();
}
private function onPluginLoadFailed(event:PluginManagerEvent):void
{
var selection:Object = cbPlugin.selectedItem;
appendInfoText("Plugin LOAD FAILED: " + selection.label);
removePluginManagerEventListeners();
}
private function removePluginManagerEventListeners():void
{
pluginManager.removeEventListener(PluginManagerEvent.PLUGIN_LOAD, onPluginLoaded);
pluginManager.removeEventListener(PluginManagerEvent.PLUGIN_LOAD_ERROR, onPluginLoadFailed);
}
private function pluginComboBoxChanged(event:DropdownEvent):void
{
var cb:ComboBox = event.currentTarget as ComboBox;
var selection:Object = cb.selectedItem;
urlInput.text = selection.url;
}
private function urlComboBoxChanged(event:Event):void
{
var cb:ComboBox = event.currentTarget as ComboBox;
var selection:Object = cb.selectedItem;
mediaInput.text = selection.url;
}
private function onMediaSizeChange(event:DisplayObjectEvent):void
{
var width:int = event.newWidth;
var height:int = event.newHeight;
// Scale to native or smaller
if (width > MAX_VIDEO_WIDTH || height > MAX_VIDEO_HEIGHT)
{
if ((width/height) >= (MAX_VIDEO_WIDTH/MAX_VIDEO_HEIGHT))
{
mediaPlayerWrapper.width = MAX_VIDEO_WIDTH;
mediaPlayerWrapper.height = MAX_VIDEO_WIDTH * (height/width);
}
else
{
mediaPlayerWrapper.width = MAX_VIDEO_HEIGHT * (width/height);
mediaPlayerWrapper.height = MAX_VIDEO_HEIGHT;
}
}
else if (width > 0 && height > 0)
{
mediaPlayerWrapper.width = event.newWidth;
mediaPlayerWrapper.height = event.newHeight;
}
}
private function onDurationChange(event:TimeEvent):void
{
seekBar.maximum = event.time;
lblDuration.text = TimeUtil.formatAsTimeCode(event.time);
}
private function onCurrentTimeChange(event:TimeEvent):void
{
if (mediaPlayerWrapper.mediaPlayer.temporal && !sliderDragging && !waitForSeek)
{
seekBar.value = event.time;
lblPlayhead.text = TimeUtil.formatAsTimeCode(event.time);
}
}
private function onSeekingChange(event:SeekEvent):void
{
waitForSeek = false;
}
private function toggleDragging(state:Boolean):void
{
sliderDragging = state;
if (!state)
{
waitForSeek = true;
if (mediaPlayerWrapper.mediaPlayer.canSeek)
{
mediaPlayerWrapper.mediaPlayer.seek(seekBar.value);
}
}
}
private function onTraitAdd(event:MediaElementEvent):void
{
switch (event.traitType)
{
case MediaTraitType.SEEK:
seekBar.enabled = seekBar.visible = true;
break;
}
}
private function onTraitRemove(event:MediaElementEvent):void
{
switch (event.traitType)
{
case MediaTraitType.SEEK:
seekBar.enabled = seekBar.visible = false;
break;
}
}
private function appendInfoText(...args):void
{
var lineNum:int = _currentInfoLineNum++;
taInfo.text += lineNum + ":" + args + "\n";
callLater(autoScrollInfoText);
}
private function clearInfoText():void
{
taInfo.text = "";
_currentInfoLineNum = 1;
}
private function autoScrollInfoText():void
{
taInfo.verticalScrollPosition = taInfo.maxVerticalScrollPosition;
}
private function onMediaError(event:MediaErrorEvent):void
{
if (taInfo.text.length)
{
taInfo.text += "\n";
}
taInfo.text += taInfo.text + "error ID="+event.error.errorID+" message="+event.error.message;
}
private function onClickPlayBtn():void
{
if (mediaPlayerWrapper.mediaPlayer.playing)
{
playBtn.label = "Play";
mediaPlayerWrapper.mediaPlayer.pause();
}
else if (mediaPlayerWrapper.mediaPlayer.paused && mediaPlayerWrapper.mediaPlayer.canPlay)
{
playBtn.label = "Pause";
mediaPlayerWrapper.mediaPlayer.play();
}
}
private function enablePlayerControls(enable:Boolean=true):void
{
playBtn.enabled = enable;
mediaPlayerContainer.alpha = (enable ? 1.0 : .33);
}
]]>
</mx:Script>
<mx:VBox height="100%" width="100%" paddingLeft="10" paddingTop="10">
<mx:Label text="OSMF Akamai Plugin Sample" styleName="title" width="100%" textAlign="left" paddingLeft="20"/>
<mx:Spacer height="20" />
<mx:HBox height="60">
<mx:Spacer width="10" />
<mx:Label text="Plugin Name:"/>
<mx:ComboBox id="cbPlugin" close="pluginComboBoxChanged(event)" dataProvider="{plugins}" />
<mx:Label text="Plugin URL:" />
<mx:TextInput id="urlInput" width="500"/>
<mx:Button label="Load Plugin" click="{loadPlugin(urlInput.text)}"/>
</mx:HBox>
<mx:HBox height="60">
<mx:Spacer width="10" />
<mx:Label text="Media Type:"/>
<mx:ComboBox id="cbMediaURL" close="urlComboBoxChanged(event)" dataProvider="{this.streams}" />
<mx:Label text="URL:"/>
<mx:TextInput id="mediaInput" width="500"/>
<mx:Button label="Load Media Resource" click="{loadMedia(mediaInput.text)}"/>
<mx:Button label="Unload Media Resource" click="{unloadMedia()}"/>
</mx:HBox>
<mx:Spacer height="5" />
<mx:HBox>
<mx:VBox id="mediaPlayerContainer" paddingLeft="20" alpha=".33">
<mx:Spacer width="10" />
<samples:MediaPlayerWrapper id="mediaPlayerWrapper" width="{MAX_VIDEO_WIDTH}" height="{MAX_VIDEO_HEIGHT}" />
<mx:HSlider id="seekBar" width="100%" thumbPress="toggleDragging(true)" thumbRelease="toggleDragging(false)"
enabled="false" visible="false" />
<mx:HBox horizontalAlign="right" width="100%">
<mx:Label text="Position: " />
<mx:Label id="lblPlayhead" width="100" styleName="timeCode" />
<mx:Label text="Duration: " />
<mx:Label id="lblDuration" width="100" styleName="timeCode" />
<mx:Button id="playBtn" label="Pause" click="onClickPlayBtn()" enabled="false" />
</mx:HBox>
</mx:VBox>
<mx:Spacer width="20" />
<mx:VBox>
<mx:TextArea id="taInfo" wordWrap="true" enabled="false" width="420" height="240"/>
</mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:Application>