Skip to main content
Inspiring
September 25, 2012
Question

playing video 16:9 Aspect Ratio

  • September 25, 2012
  • 2 replies
  • 1738 views

my player is 16:9, so i trying to get netStream width,height after i playing video Aspect Ratio to 16:9 (like show youtube black side pillers);

but here my code playing video default size 320x240,

can you help me anyone?

My code:

var nConnection:NetConnection;

var ns:NetStream;

var video:Video = new Video();

addChild(video);

 

nConnection = new NetConnection();

nConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

nConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

nConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ayncErrorHandler);

nConnection.connect(serverURL);

function netStatusHandler(event:NetStatusEvent):void

{

          switch (event.info.code)

          {

                    case "NetConnection.Connect.Success" :

                              connectStream();

                              break;

                    case "NetStream.Play.StreamNotFound" :

                              trace("Stream not found: " + videoURL);

                              break;

                    case "NetStream.Play.Start" :

                              break;

                    case "NetStream.Play.Stop" :

                              break;

                    default :

          }

}

function connectStream():void

{

          ns = new NetStream(nConnection);

          ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

          ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ayncErrorHandler);

          ns.client = {onMetaData:ns_onMetaData};

          ns.bufferTime = 10;

          video.attachNetStream(ns);

          ns.play(videoURL);

          video.smoothing = true;

}

function ns_onMetaData(item:Object):void

{

     video.width = item.width;

     video.height = item.height;

     trace("vid wid "+videoWidth);

     trace("vid hei "+videoHeight);

}

function securityErrorHandler(event:SecurityErrorEvent):void

{

          trace("securityErrorHandler: " + event);

}

function ayncErrorHandler(event: AsyncErrorEvent):void

{

}

This topic has been closed for replies.

2 replies

Inspiring
September 26, 2012

If your task is to fit and center video within particular dimensions try this (code assumes that video is placed into the same display list/scope where the rest of the code is).

function ns_onMetaData(item:Object):void

{

    var scale:Number = this.width / this.height > item.width / item.height ? this.height / item.height : this.width / item.width;

    video.width *= scale;

    video.height *= scale;

    video.x = (this.width - video.width) * .5;

    video.y = (this.height - video.height) * .5;

}

esdebon
Inspiring
September 25, 2012

Since the apsectratio parameter defaults to null, we can then ignore that parameter if we simply just want the video to flush with the dimensions passed in the args object on declaration.  So now putting it all together, add your videoPlayer to the display list as follows:

import com. videoPlayer ;  // create video instance  var videoContain:Sprite = new Sprite ( ) ;  var myVid:videoPlayer = new videoPlayer ( {  target : videoContain,  width : 432 ,  height : 240  } ) ;  addChild ( videoContain ) ;

  Then to play a video call the playVideo method:

myVid. playVideo ( "yourfile.flv" , [ 4 , 3 ] ) ;

http://www.mikethenderson.com/2009/02/maintain-aspect-ratio-with-netstream-in-as3/