我正在构建一个包含不同外部和内部(osmf)播放器对象的自定义视频播放器。我的播放器代码在我的库中单独的as3组件。直接在mxml中添加播放器对象会使swf文件大小超出需要的范围,因为所有obejct都已添加(但未加载)
<osmf:OSMFPlayer id="playerOSMF" depth="1" />
如何将as3组件动态添加到舞台上?
我的mxml包含一个主脚本
<fx:Script source="main.as" />
当我在applicationComplete()
上添加init()函数时public var player:OSMFPlayer;
private function init():void {
player = new OSMFPlayer();
addChild(player);
}
这不起作用,我已经尝试了我能想到的一切,有人得到了一些有用的建议吗?
答案 0 :(得分:1)
您无法直接将OSMFPlayer添加到舞台。
请参阅以下代码。
//flash imports
import flash.display.Sprite ;
import flash.events.MouseEvent ;
//osmf imports
import org.osmf.containers.MediaContainer ;
import org.osmf.layout.LayoutMetadata ;
import org.osmf.media.MediaPlayer ;
import org.osmf.media.MediaElement ;
import org.osmf.media.MediaFactory ;
import org.osmf.media.DefaultMediaFactory ;
import org.osmf.media.URLResource ;
import org.osmf.elements.SerialElement ;
//point to two separate pieces of media
const MEDIA_URL:String = "myVideo.mp4";
const VIDEO_WIDTH:int = 1024;
const VIDEO_HEIGHT:int = 768;
//variable declarations
var mediaFactory:MediaFactory;
var serialElement:SerialElement;
var mediaElement:MediaElement;
var mediaPlayer:MediaPlayer;
var mediaContainer:MediaContainer;
var layout:LayoutMetadata ;
function loadPlayer()
{
//create a new DefaultMediaFactory
mediaFactory = new DefaultMediaFactory();
//use the mediaFactory to create two new MediaElement objects
mediaElement = mediaFactory.createMediaElement(new URLResource(MEDIA_URL));
//create, size and position two LayoutMetadata objects
layout1 = new LayoutMetadata();
layout1.width = VIDEO_WIDTH;
layout1.height = VIDEO_HEIGHT;
layout1.x = 0;
layout1.y = 0;
//create a new SerialElement
serialElement = new SerialElement () ;
//tie each LayoutMetadata object to one of the MediaElement objects
mediaElement.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layout);
//create a new MediaPlayer instance
mediaPlayer = new MediaPlayer();
//set the mediaPlayer not to begin playback by default
mediaPlayer.autoPlay = false;
//set the media property of the mediaPlayer to the serialElement
mediaPlayer.media = serialElement;
//create a new MediaContainer
mediaContainer = new MediaContainer();
//add both MediaElement instances to the serialElement
serialElement.addChild ( mediaElement ) ;
//add the serialElement to the MediaContainer
mediaContainer.addMediaElement(serialElement);
//add the mediaContainer to the display list
addChild(mediaContainer);
//listen for the click event on the stage
stage.addEventListener ( MouseEvent.CLICK , onPlayToggle ) ;
}
function onPlayToggle ( evt : MouseEvent ) : void
{
//if the mediaPlayer is running
if ( mediaPlayer.playing )
//pause the mediaPlayer
mediaPlayer.pause ( ) ;
else
//resume the mediaPlayer
mediaPlayer.play ( ) ;
}
loadPlayer();
我建议您阅读OSMF文档。
并参考Adobe官方示例代码。