<?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" backgroundColor="#000000"
xmlns:samples="org.osmf.samples.*" applicationComplete="init()">
<mx:Style source="SMILSample.css" />
<mx:Script>
<![CDATA[
import org.osmf.media.MediaResourceBase;
import org.osmf.utils.TimeUtil;
import org.osmf.plugin.PluginInfoResource;
import mx.controls.Alert;
import org.osmf.events.MediaElementEvent;
import org.osmf.events.DisplayObjectEvent;
import org.osmf.media.MediaInfo;
import org.osmf.metadata.ObjectIdentifier;
import org.osmf.utils.FMSURL;
import org.osmf.events.MediaErrorEvent;
import org.osmf.plugin.PluginManager;
import org.osmf.media.MediaFactory;
import org.osmf.media.MediaElement;
import org.osmf.metadata.KeyValueFacet;
import org.osmf.media.URLResource;
import org.osmf.events.PluginLoadEvent;
import org.osmf.media.MediaResourceBase;
import org.osmf.media.URLResource;
import org.osmf.net.NetLoader;
import org.osmf.traits.MediaTraitType;
import org.osmf.video.VideoElement;
import org.osmf.utils.URL;
import org.osmf.events.SeekEvent;
import org.osmf.events.MetadataEvent;
import org.osmf.events.TimeEvent;
// Force the inclusion of the PluginInfo class so we can
// load the plugin statically
import org.osmf.smil.SMILPluginInfo;
private static const forceReference:SMILPluginInfo = null;
private static const SMIL_TEST:Array = [ "http://mediapm.edgesuite.net/osmf/content/test/smil/elephants_dream.smil",
"http://www.streamflashhd.com/video/train.smil",
"http://mediapm.edgesuite.net/osmf/content/test/smil/spacealonehd_sounas_640.smil",
"http://mediapm.edgesuite.net/osmf/content/test/smil/sweet.smil",
"http://mediapm.edgesuite.net/osmf/content/test/smil/seq-test.smil",
"http://mediapm.edgesuite.net/osmf/content/test/smil/par-test.smil" ];
private static const DEFAULT_PROGRESS_DELAY:uint = 100;
private static const MAX_VIDEO_WIDTH:int = 480;
private static const MAX_VIDEO_HEIGHT:int = 270;
private var sliderDragging:Boolean;
private var waitForSeek:Boolean;
private var pluginManager:PluginManager;
private var mediaFactory:MediaFactory;
[Bindable]
private var sampleDescription:String = "<p>This sample application loads the OSMF SMIL plugin,"+
" and creates media elements based on the SMIL files"
" contents.</p>";
private function init():void
{
mediaFactory = new MediaFactory();
pluginManager = new PluginManager(mediaFactory);
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, onSeekEnd);
mediaPlayerWrapper.mediaPlayer.addEventListener(SeekEvent.SEEK_BEGIN, onSeekBegin);
mediaPlayerWrapper.mediaPlayer.currentTimeUpdateInterval = DEFAULT_PROGRESS_DELAY;
sliderDragging = false;
waitForSeek = false;
loadPlugin("org.osmf.smil.SMILPluginInfo");
}
private function loadMedia(url:String):void
{
var resource:URLResource = new URLResource(new FMSURL(url));
//mediaFactory.addMediaInfo(new MediaInfo("org.osmf.video", new NetLoader(), createVideoElement));
var mediaElement:MediaElement = mediaFactory.createMediaElement(resource);
// 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);
// Listen for metadata to be added so we can add any desired event listeners on any
// metadata facets we care about. For example, the temporalFacet.
mediaElement.metadata.addEventListener(MetadataEvent.FACET_ADD, onFacetAdd);
// Listen for metadata to be removed so we remove an event listeners
mediaElement.metadata.addEventListener(MetadataEvent.FACET_REMOVE, onFacetRemove);
mediaElement.addEventListener(MediaErrorEvent.MEDIA_ERROR, onMediaError, false, 0, true);
mediaPlayerWrapper.element = mediaElement;
enablePlayerControls(true);
}
private function createVideoElement():MediaElement
{
return new VideoElement(new NetLoader());
}
private function loadPlugin(source:String, load:Boolean=true):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(new URL(source));
}
else
{
// Assume this is a class
var pluginInfoRef:Class = flash.utils.getDefinitionByName(source) as Class;
pluginResource = new PluginInfoResource(new pluginInfoRef);
}
if (load)
{
loadPluginFromResource(pluginResource);
}
else
{
unloadPluginFromResource(pluginResource);
}
}
private function loadPluginFromResource(pluginResource:MediaResourceBase):void
{
pluginManager.addEventListener(PluginLoadEvent.PLUGIN_LOADED, onPluginLoaded);
pluginManager.addEventListener(PluginLoadEvent.PLUGIN_LOAD_FAILED, onPluginLoadFailed);
pluginManager.loadPlugin(pluginResource);
}
private function unloadPluginFromResource(pluginResource:MediaResourceBase):void
{
pluginManager.addEventListener(PluginLoadEvent.PLUGIN_UNLOADED, onPluginUnloaded);
pluginManager.unloadPlugin(pluginResource);
}
private function unloadPlugin(source:String):void
{
loadPlugin(source, false);
}
private function onPluginLoaded(event:PluginLoadEvent):void
{
trace("Plugin LOADED!");
loadMedia(SMIL_TEST[4]);
}
private function onPluginUnloaded(event:PluginLoadEvent):void
{
trace("Plugin UNLOADED!");
}
private function onPluginLoadFailed(event:PluginLoadEvent):void
{
trace("Plugin LOAD FAILED!");
}
private function onMediaError(event:MediaErrorEvent):void
{
Alert.show("Media Load Error : "+event.error.errorID + " - " + event.error.message);
}
private function onMediaSizeChange(event:DisplayObjectEvent):void
{
var width:int = event.newWidth;
var height:int = event.newHeight;
if (width == 0 || height == 0)
{
return;
}
// 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 = width;
mediaPlayerWrapper.height = height;
}
trace("*** mediaPlayerWrapper.width="+mediaPlayerWrapper.width);
trace("*** mediaPlayerWrapper.height="+mediaPlayerWrapper.height);
}
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 onSeekEnd(event:SeekEvent):void
{
waitForSeek = false;
}
private function onSeekBegin(event:SeekEvent):void
{
}
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 onFacetAdd(event:MetadataEvent):void
{
}
private function onFacetRemove(event:MetadataEvent):void
{
}
private function onClickPlayBtn(event:Event):void
{
if (mediaPlayerWrapper.mediaPlayer.playing && mediaPlayerWrapper.mediaPlayer.canPause)
{
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 = seekBar.enabled = enable;
}
private function showScrubTime(val:String):String
{
return TimeUtil.formatAsTimeCode(Number(val));
}
]]>
</mx:Script>
<mx:VBox id="mainContainer" paddingLeft="20" paddingTop="20">
<mx:HBox width="100%">
<mx:Label styleName="title" text="OSMF SMIL Sample" />
</mx:HBox>
<mx:Spacer height="5" />
<mx:HBox>
<mx:VBox>
<mx:VBox id="videoContainer">
<samples:MediaPlayerWrapper id="mediaPlayerWrapper" width="480" height="270" />
<mx:HSlider id="seekBar" width="480" thumbPress="toggleDragging(true)" thumbRelease="toggleDragging(false)"
dataTipFormatFunction="showScrubTime" enabled="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" width="70" click="onClickPlayBtn(event)" enabled="false" />
</mx:HBox>
</mx:VBox>
</mx:VBox>
<mx:Spacer width="20" />
<mx:VBox id="infoContainer">
<mx:TextArea wordWrap="true" htmlText="{sampleDescription}" enabled="false" width="480" height="270" />
</mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:Application>