0

I'm building a custom video player containing different external and internal (osmf) player objects. I have the player code in seperate as3 components in my library. Adding the player object directly in the mxml makes the swf filesize bigger than it needs to be because all obejct are already added (but not loaded)

<osmf:OSMFPlayer id="playerOSMF" depth="1" />

how can i add the as3 components dynamically to the stage?

my mxml contains a main script

<fx:Script source="main.as" />

when i add in the init() function on applicationComplete()

public var player:OSMFPlayer;

private function init():void {
  player = new OSMFPlayer();
  addChild(player);
}

this doesn't work and i already tried everything i could think of, anybody got some helpful advice?

1 Answer 1

1

You can't directly add OSMFPlayer to stage.

see a below code.

//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();

I recommend, read a OSMF Documentation.

OSMF

and refer a Adobe Official Sample code.

OSMF Market Place

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.