I am facing a problem in video resizing, could someone help me to resolve this issue.
I want to scale video to available content size (stage size) without distorting quality like VLC player.
Here is my code.
FLA Code:
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
////////////////////////////
var videoMC : MovieClip = new MovieClip ();
this.addChild (videoMC);
var videoMgr:VideoManager = VideoManager.getInstance();
videoMgr.init ("sampleFile.flv");
videoMC.addChild (videoMgr);
// #############
// this tells Flash NOT to allow the assets to be scaled
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// need to set up a listener object to say when the stage is resized.
stage.addEventListener (Event.RESIZE, onStageResize);
setBackground ();
// called when the stage is resized
function onStageResize (evt : Event)
{
setBackground();
}
function setBackground ()
{
// determine middle
var middleX = stage.stageWidth / 2;
var middleY = stage.stageHeight / 2;
// reposition the bkgd to middle
videoMC.x = 0;
videoMC.y = 0;
// scale background to fit width and height
videoMgr.updateVideoSize (stage.stageWidth, stage.stageHeight);
// see if it grew bigger horizontally or vertically and adjust other to match
// to maintain aspect ratio
//videoMC.scaleX > videoMC.scaleY ? videoMC.scaleY = videoMC.scaleX:videoMC.scaleX = videoMC.scaleY;
}
// #############
---------------------------------------------------------------------- --------------------------------
AS3 Class:
package
{
import flash.display.Sprite;
import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.StatusEvent;
import flash.events.TimerEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.Timer;
public class VideoManager extends Sprite
{
private static var _instance : VideoManager;
private var con : NetConnection;
private var vStream : NetStream;
private var video : Video;
private var vWidth : uint;
private var vHeight : uint;
private var filePath : String;
public function VideoManager (enforce : Singleton)
{
}
public static function getInstance () : VideoManager
{
if (_instance == null)
{
_instance = new VideoManager (new Singleton);
}
return _instance;
}
public function init (path : String, w : uint=720, h : uint=480) : void
{
filePath = path;
con = new NetConnection ();
con.addEventListener (NetStatusEvent.NET_STATUS, onNetStatus);
con.addEventListener (SecurityErrorEvent.SECURITY_ERROR, onNetSecurity);
con.connect (null);
}
public function updateVideoSize (w:uint, h:uint) : void
{
vWidth = w;
vHeight = h;
//
video.width = vWidth;
video.height = vHeight;
}
/*public function playVideo (startFrom : uint) : void
{
}
public function pauseVideo () : void
{
}*/
public function resumeVideo () : void
{
vStream.togglePause ();
}
public function stopVideo () : void
{
}
public function hideVideo () : void
{
}
public function setAudioLevel (val : uint) : void
{
var sndTrans : SoundTransform = new SoundTransform (val);
vStream.soundTransform = sndTrans;
}
private function connectStream () : void
{
vStream = new NetStream (con);
registerStreamEvents ();
video = new Video (vWidth, vHeight);
video.attachNetStream (vStream);
vStream.play (filePath);
startPreloader ();
addChild (video);
trace (vStream.bytesLoaded + "/" + vStream.bytesTotal);
}
private function registerStreamEvents () : void
{
vStream.addEventListener (StatusEvent.STATUS, onNetStatus);
vStream.addEventListener (AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
}
private function unRegisterStreamEvents () : void
{
vStream.removeEventListener (StatusEvent.STATUS, onNetStatus);
vStream.removeEventListener (AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
}
private function onNetStatus (evt : NetStatusEvent) : void
{
switch (evt.info.code)
{
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.Complete":
videoPlayComplete ();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video: " + evt.toString());
break;
}
}
private function onNetSecurity (evt : SecurityErrorEvent) : void
{
trace ("Error occured in connection. " + evt.toString());
}
private function onAsyncError (evt : AsyncErrorEvent) : void
{
// This is mandate handle
}
private function startPreloader () : void
{
var tmr : Timer = new Timer (100);
tmr.addEventListener (TimerEvent.TIMER, checkLoadProgress);
tmr.start ();
}
private function checkLoadProgress (evt : TimerEvent) : void
{
trace (vStream.bytesLoaded + "/" + vStream.bytesTotal);
if (vStream.bytesLoaded == vStream.bytesTotal)
{
trace ("Stoping the timer.");
evt.currentTarget.removeEventListener (TimerEvent.TIMER, checkLoadProgress);
}
}
private function videoPlayComplete () : void
{
// TODO: Dispatch event which will indicate that video is at end.
}
}
}
internal class Singleton {}



