26. March 2008
Creating a full screen H264 flash player with adobe CS3 / flash 9 / Actionscript 3
Presupun ca intereseaza pe multa lume cum se creeaza un player H264 in flash utilizind streaming dintr-un FMS 3 (Flash Media Server) . Evident ca sint multe modalitati de a face asta. Se poate utiliza componenta care vine cu CS3 FLVPlayback . Merge foarte bine , are suficiente skinuri iar daca nu va place nici unul din skinurile cu care vine se creea un skin custom in care sa va desenati player-ul dupa cum doriti. Pe mine nu m-a interesat componenta gata facuta si am incercat sa reinventez roata. E un bun exercitiu avind in vedere proiectele care urmeaza.
Ce am reusit sa “produc”? Codul de mai jos. Ce face? Se conecteaza la un FMS, si cinta un mp4 predefinit. Evident e doar un skelet minimal peste care se pot adauga facilitati. Ce mi se pare cel mai frumos este ca rezultatul compilarii este un flash mic mic mic. Doar 8K . Playerul care foloseste FLVPlayback are 50k + cit mai are skinul. Prea multa vorba strica. Sa lasam codul sa cinte singur:
import flash.geom.*;
import flash.display.Stage;
var mySound:SoundTransform;
var video:Video;var connect_nc:NetConnection = new NetConnection();
var stream_ns : NetStream ;
connect_nc.addEventListener(NetStatusEvent.NET_STATUS,neta);
connect_nc.connect("rtmp://FMS.tfm.ro/application");
video = new Video();
video.x = 2;
video.y = 2;
fullScreen_mc.addEventListener("click", goFullScreen);
function netStatusHandler(p_evt:NetStatusEvent):void {
/* Add handler here */
}
function onMetaData(p_info:Object):void {
video.width = p_info.width;
video.height = p_info.height;
}
function goFullScreen(p_evt:Object):void {
var scalingRect:Rectangle = new Rectangle(video.x, video.y, video.width, video.height);
stage["fullScreenSourceRect"] = scalingRect;
if(stage.displayState == StageDisplayState.NORMAL)
stage.displayState = StageDisplayState.FULL_SCREEN;
else
stage.displayState = StageDisplayState.NORMAL;
}
function neta(p_evt:NetStatusEvent):void {
if(p_evt.info.code != "NetConnection.Connect.Success")
return;
stream_ns = new NetStream(connect_nc);
stream_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream_ns.client = this;
video.attachNetStream(stream_ns);
stream_ns.play("mp4:my_sample.mp4");
mySound = stream_ns.soundTransform;
mySound.volume = .5;
stream_ns.soundTransform = mySound;
addChild(video);
}
NetConnection.prototype.onBWDone = function(p_bw) {
// do nothing
}