0

我正在构建一个自定义视频播放器,其中包含不同的外部和内部(osmf)播放器对象。我的库中有单独的 as3 组件中的播放器代码。直接在 mxml 中添加播放器对象会使 swf 文件大小大于所需大小,因为所有对象都已添加(但未加载)

<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);
}

这不起作用,我已经尝试了我能想到的一切,有人得到一些有用的建议吗?

4

1 回答 1

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 文档。

OSMF

并参考 Adob​​e 官方示例代码。

OSMF 市场

于 2013-02-07T17:10:28.953 回答