1
votes

I am trying to making a dynamic RTMP vedio player like jwplayer. my player collect stream veriable from swfobect and then play it via netstream in as3. I attached netstream to my stage. But i am facing some weird problem. When i am resizing my stage or making full screen of my player the vedio got stretched. i tried stage.scaleMode but its not working. please check my main as3 below and give me a solution so that my vedio can obtain aspect ration at any size of my player/stage. see the screenshot fullscreen mode screenshot

normal screen Mode

Main ActionScript Code

package com.WindchimeVedioPlayer {
    import flash.media.*;
    import flash.system.Security;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.display.*;
    import flash.display.DisplayObject;
    import com.WindchimeVedioPlayer.RTMPStream;
    Security.allowDomain("*");

    public class main extends RTMPStream {

        /* the constructor. */
        public function main():void {

            trace("Downstream object has been created."); // debug trace..
            //stage.scaleMode=StageScaleMode.NO_BORDER;
            this.oVideo = new Video(640, 480);
            this.oConnection = new NetConnection();
            this.oConnection.addEventListener(NetStatusEvent.NET_STATUS, eNetStatus, false, 0, true);
            this.oConnection.connect(this.sMediaServerURL);
            addChild(logo);
            //logo.x=440;
            }

        /* triggered when a net status event is received. */
        private function eNetStatus(oEvent1:NetStatusEvent) {

            trace("NetStatusEvent: " + oEvent1.info.code); // debug trace..

            switch (oEvent1.info.code) {
            case "NetConnection.Connect.Success":

                // create a stream for the connection..
                this.oNetStream = new NetStream(oConnection);
                this.oNetStream.addEventListener(NetStatusEvent.NET_STATUS, eNetStatus, false, 0, true);
                this.oNetStream.bufferTime = 5; // set this to whatever is comfortable..

                // listen for meta data..
                this.oMetaData.onMetaData = eMetaDataReceived;
                this.oNetStream.client = this.oMetaData;

                // attach the stream to the stage..
                this.oVideo.attachNetStream(oNetStream);
                this.oNetStream.play(sStreamName);
                this.addChildAt(this.oVideo, 0);

                trace("Connected to the RTMP server."); // debug trace..
                break;

            case "NetConnection.Connect.Closed":

                trace("Disconnected from the RTMP server."); // debug trace..
                break;

            case "NetStream.Play.StreamNotFound":

                trace("This stream is currently unavailable."); // debug trace..
                break;
            }

        }

    }

}

Please see the action script code and please suggest me how to fix it.

1
Does this really need everything to be called this.?VC.One

1 Answers

3
votes

Well this.oNetStream.client = this.oMetaData; needs to be backed up by a Metadata handling function. In there you want to read the original video width/height & use that to adjust the container dimensions to fit accordingly (maintain aspect ratio). I also suggest using Sprite as container of video instead of just pasting it on Stage, easier to work with later on if you want to get interesting..)

public var oMetaData : Object; //will keep a database of meta info

// listen for meta data..
oMetaData = new Object();
oMetaData.onMetaData = received_Meta;
oNetStream.client = oMetaData;


Then include this function to process

function received_Meta (data:Object):void
{       
    trace("Detected Video Width  (pixels) : " + data.width);
    trace("Detected Video Height (pixels) : " + data.height);

    var _stageW:int = stage.stageWidth;
    var _stageH:int = stage.stageHeight;

    var _videoW:int;
    var _videoH:int;
    var _aspectH:int; 

    var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
    Aspect_num = data.width / data.height;

    //Aspect ratio calculated here..
    _videoW = _stageW;
    _videoH = _videoW / Aspect_num; //or try: _videoW * Aspect_num; 
    _aspectH = (_stageH - _videoH) / 2;

    oVideo.x = 0;
    oVideo.y = _aspectH;
    oVideo.width = _videoW;
    oVideo.height = _videoH;
}


Update: To explain how you will then read the metadata...

This line function received_Meta (data:Object):void is using data as the reference name to the metaData holder (type :Object).

In my posted code I have (data:Object) so this line works Aspect_num = data.height

If you change it to something like:
(oObject:Object) now the line works as Aspect_num = oObject.height