Skip to main content
Inspiring
July 18, 2011
Answered

On-demand Video Player ArgumentError: Error #2126

  • July 18, 2011
  • 1 reply
  • 1861 views

I have a ERROR message is


ArgumentError: Error #2126: NetConnection object must be connected.
     at flash.net::NetStream/ctor()
     at flash.net::NetStream()
     at videoplayer1_fla::MainTimeline/initVideoPlayer()
     at videoplayer1_fla::MainTimeline/frame1()

Below code is xml playlist included. But i want to play videos through "rtmp server".


My On-demand Video Player Code

// #### VARIABLES

// net connection object for net stream

var ncConnection:NetConnection;

// net stream object

var nsStream:NetStream;

// object holds all meta data

var objInfo:Object;

// shared object holding the player settings (currently only the volume)

var shoVideoPlayerSettings:SharedObject = SharedObject.getLocal("playerSettings");

// url to flv file

var strSource:String                    = root.loaderInfo.parameters.playlist == null ? "playlist.xml" : root.loaderInfo.parameters.playlist;

// timer for updating player (progress, volume...)

var tmrDisplay:Timer;

// loads the xml file

var urlLoader:URLLoader;

// holds the request for the loader

var urlRequest:URLRequest;

// playlist xml

var xmlPlaylist:XML;

// ############# FUNCTIONS

// sets up the player

function initVideoPlayer():void {

    // hide video controls on initialisation

    mcVideoControls.visible = false;

    // hide buttons

    mcVideoControls.btnUnmute.visible         = false;

    mcVideoControls.btnPause.visible            = false;

    mcVideoControls.btnFullscreenOff.visible    = false;

    // set the progress/preload fill width to 1

    mcVideoControls.mcProgressFill.mcFillRed.width  = 1;

    mcVideoControls.mcProgressFill.mcFillGrey.width = 1;

    // set time and duration label

    mcVideoControls.lblTimeDuration.htmlText        = "<font color='#ffffff'>00:00</font> / 00:00";

    // add global event listener when mouse is released

    stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

    // add fullscreen listener

    stage.addEventListener(FullScreenEvent.FULL_SCREEN, onFullscreen);

    // add event listeners to all buttons

    mcVideoControls.btnPause.addEventListener(MouseEvent.CLICK, pauseClicked);

    mcVideoControls.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);

    mcVideoControls.btnStop.addEventListener(MouseEvent.CLICK, stopClicked);

    mcVideoControls.btnMute.addEventListener(MouseEvent.CLICK, muteClicked);

    mcVideoControls.btnUnmute.addEventListener(MouseEvent.CLICK, unmuteClicked);

    mcVideoControls.btnFullscreenOn.addEventListener(MouseEvent.CLICK, fullscreenOnClicked);

    mcVideoControls.btnFullscreenOff.addEventListener(MouseEvent.CLICK, fullscreenOffClicked);

    mcVideoControls.btnVolumeBar.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);

    mcVideoControls.mcVolumeScrubber.btnVolumeScrubber.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);

    mcVideoControls.btnProgressBar.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);

    mcVideoControls.mcProgressScrubber.btnProgressScrubber.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);

    mcVideoControls.mcVideoDescription.btnDescription.addEventListener(MouseEvent.MOUSE_OVER, startDescriptionScroll);

    mcVideoControls.mcVideoDescription.btnDescription.addEventListener(MouseEvent.MOUSE_OUT, stopDescriptionScroll);

    // create timer for updating all visual parts of player and add

    // event listener

    tmrDisplay = new Timer(DISPLAY_TIMER_UPDATE_DELAY);

    tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);

    // create a new net connection, add event listener and connect

    // to null because we don't have a media server

    ncConnection = new NetConnection();

    ncConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

    ncConnection.connect("rtmp://somehost.com/myfolder");

    // create a new netstream with the net connection, add event

    // listener, set client to this for handling meta data and

    // set the buffer time to the value from the constant

    nsStream = new NetStream(ncConnection);

    nsStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

    nsStream.client = this;

    nsStream.bufferTime = BUFFER_TIME;

    // attach net stream to video object on the stage

    vidDisplay.attachNetStream(nsStream);

    // set the smoothing value from the constant

    vidDisplay.smoothing = SMOOTHING;

    // set default volume and get volume from shared object if available

    var tmpVolume:Number = DEFAULT_VOLUME;

    if(shoVideoPlayerSettings.data.playerVolume != undefined) {

        tmpVolume = shoVideoPlayerSettings.data.playerVolume;

        intLastVolume = tmpVolume;

    }

    // update volume bar and set volume

    mcVideoControls.mcVolumeScrubber.x = (53 * tmpVolume) + 318;

    mcVideoControls.mcVolumeFill.mcFillRed.width = mcVideoControls.mcVolumeScrubber.x - 371 + 53;

    setVolume(tmpVolume);

    // create new request for loading the playlist xml, add an event listener

    // and load it

    urlRequest = new URLRequest(strSource);

    urlLoader = new URLLoader();

    urlLoader.addEventListener(Event.COMPLETE, playlistLoaded);

    urlLoader.load(urlRequest);

}

function playClicked(e:MouseEvent):void {

    // check's, if the flv has already begun

    // to download. if so, resume playback, else

    // load the file

    if(!bolLoaded) {

        nsStream.play(strSource);

        bolLoaded = true;

    }

    else{

        nsStream.resume();

    }

    vidDisplay.visible = true;

    // switch play/pause visibility

    mcVideoControls.btnPause.visible    = true;

    mcVideoControls.btnPlay.visible  = false;

}

nsStream.seek(Math.round(mcVideoControls.mcProgressScrubber.x * objInfo.duration / 472))

    else

        mcVideoControls.mcProgressScrubber.x = nsStream.time * 472 / objInfo.duration;

    // set time and duration label

    mcVideoControls.lblTimeDuration.htmlText        = "<font color='#ffffff'>" + formatTime(nsStream.time) + "</font> / " + formatTime(objInfo.duration);

    // update the width from the progress bar. the grey one displays

    // the loading progress

    mcVideoControls.mcProgressFill.mcFillRed.width  = mcVideoControls.mcProgressScrubber.x + 5;

    mcVideoControls.mcProgressFill.mcFillGrey.width = nsStream.bytesLoaded * 478 / nsStream.bytesTotal;

function onMetaData(info:Object):void {

    // stores meta data in a object

    objInfo = info;

    // now we can start the timer because

    // we have all the neccesary data

    if(!tmrDisplay.running)

        tmrDisplay.start();

}

function netStatusHandler(event:NetStatusEvent):void {

    // handles net status events

    switch (event.info.code) {

        // trace a messeage when the stream is not found

        case "NetStream.Play.StreamNotFound":

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

        break;

        // when the video reaches its end, we check if there are

        // more video left or stop the player

        case "NetStream.Play.Stop":

            if(intActiveVid + 1 < xmlPlaylist..vid.length())

                playNext();

            else

                stopVideoPlayer();

        break;

    }

}

function stopVideoPlayer():void {

    // pause netstream, set time position to zero

    nsStream.pause();

    nsStream.seek(0);

    // in order to clear the display, we need to

    // set the visibility to false since the clear

    // function has a bug

    vidDisplay.visible          = false;

    // switch play/pause button visibility

    mcVideoControls.btnPause.visible    = false;

    mcVideoControls.btnPlay.visible  = true;

}

function setVolume(intVolume:Number = 0):void {

    // create soundtransform object with the volume from

    // the parameter

    var sndTransform        = new SoundTransform(intVolume);

    // assign object to netstream sound transform object

    nsStream.soundTransform = sndTransform;

    // hides/shows mute and unmute button according to the

    // volume

    if(intVolume > 0) {

        mcVideoControls.btnMute.visible  = true;

        mcVideoControls.btnUnmute.visible   = false;

    } else {

        mcVideoControls.btnMute.visible  = false;

        mcVideoControls.btnUnmute.visible   = true;

    }

    // store the volume in the flash cookie

    shoVideoPlayerSettings.data.playerVolume = intVolume;

    shoVideoPlayerSettings.flush();

}

function formatTime(t:int):String {

    // returns the minutes and seconds with leading zeros

    // for example: 70 returns 01:10

    var s:int = Math.round(t);

    var m:int = 0;

    if (s > 0) {

        while (s > 59) {

            m++; s -= 60;

        }

        return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);

    } else {

        return "00:00";

    }

}

function playlistLoaded(e:Event):void {

    // create new xml with loaded data from loader

    xmlPlaylist = new XML(urlLoader.data);

    // set source of the first video but don't play it

    playVid(0, false)

    // show controls

    mcVideoControls.visible = true;

}

function playVid(intVid:int = 0, bolPlay = true):void {

    if(bolPlay) {

        // stop timer

        tmrDisplay.stop();

        // play requested video

        nsStream.play("myvideo.flv");

        // switch button visibility

        mcVideoControls.btnPause.visible    = true;

        mcVideoControls.btnPlay.visible  = false;

    } else {

        strSource = xmlPlaylist..vid[intVid].@src;

    }

    // show video display

/*  vidDisplay.visible                    = true;

    // reset description label position and assign new description

    mcVideoControls.mcVideoDescription.lblDescription.x = 0;

    mcVideoControls.mcVideoDescription.lblDescription.htmlText = (intVid + 1) + ". <font color='#ffffff'>" + String(xmlPlaylist..vid[intVid].@desc) + "</font>";*/

    // update active video number

    intActiveVid = intVid;

}

// ###############################

This topic has been closed for replies.
Correct answer VenkatBaddi

Do you hear sound?

Is it the same video that you play from http?

Post rtmp url to the video  - I will test it in my player.


Finally i got video Andrei,

problem with file extension, if i remove ".flv" when video is playing.)


Thank you for your help.

1 reply

Inspiring
July 18, 2011

With rtmp streaming you have to wait until NetConnection is successful by listening for NetStatusEvent.NET_STATUS event when it arrives with NetConnection.Connect.Success. Only after that you can establish NetStream.

Read documentation:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetConnection.html#includeExamplesSummary

Inspiring
July 19, 2011

please send me detailed explination, because iam as3 beginner.

where location i change

Inspiring
July 19, 2011

Did you read documentation? Example there shows how to use NetConnection.