Quick Tip: Working with NetStream

by josefchutka on July 19, 2010

While working on my brand new P2P project I have come to some issues and solutions, that may help you with your projects. First thing, the NetStatusEvent codes. You may expect that codes are dispatched by the instance of the class they beigins with (e.g. “NetStream.Connect.Success” dispatched by NetStream), but in fact, this does not happen (“NetStream.Connect.Success” is dispatched by NetConnection). I bellieve its not a bug but a feature by flash player developer team. To make this work as expected you may do the following:

netConnection.addEventListener(type, onNetStatus);

function onNetConnectionNetStatus(event:NetStatusEvent):void
{
	switch(event.info.code){
		case "NetStream.Connect.Success":
			event.info.stream.dispatchEvent(event);
			break;
		...
	}
}

This way your NetStream dispatches the same NetStatusEvent into itself. You may now wonder why am I doing it? There are 2 reasons for that:

  • This way I can easily handle multiple NetStreams (in multiple objects)
  • This event may be used for attaching camera/microphone into your NetStream (I guess due to peer-assisted security restrictions).
netGroup.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

function onNetStatus(event:NetStatusEvent):void
{
	switch(event.info.code)
	{
		case "NetStream.Connect.Success":
			netStream.attachCamera(camera);
			netStream.attachAudio(microphone);
			netStream.publish("someName");
			break;
		case "NetGroup.Neighbor.Connect":
			subscribers++;
			break;
		case "NetGroup.Neighbor.Disconnect":
			subscribers--;
			break;
	}
}

attaching camera / microphone after NetStream is connected, prevents you from follofing:

var camera:Camera = Camera.getCamera();
var netStream:NetStream = new NetStream(netConnection,
    groupspec.groupspecWithAuthorizations());
stream.attachCamera(cam);

…will result in runtime error:

Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.

NetGroup.Connect.Closed

Getting this annoying, undocumented error code?

class
{
    private function onConnect():void
    {
        var netGroup:NetGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);
    }
    // dispatches NetStatusEvent with code NetGroup.Connect.Closed
}

… workaround:

class
{
    private var var netGroup:NetGroup; // using class variable!

    private function onConnect():void
    {
        netGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);
    }
}

This seems like flash player bug. I guess this bug has something to do with reference to a NetGroup instance. If you use function variable, this get cleared after function finishes even with attached listener (guess), while class variable reference remains.


Interview with Chris Scott (Swiz Framework)

by Tom Krcha on July 14, 2010


Report z letního AUG meetu s Chrisem Scottem

by Viktor Bezděk on July 14, 2010

Vstal jsem o hodinku dříve a těsil se, že napíšu nějaký pěkný report. Už včera mě ale předběhl Nikolaj, který zmínil vše, co bych zmínil já. Přečtěte si tedy jeho postřehy a pokochejte se pár fotkami. Za mě jen v kostce – setkaní bylo opravdu fajn. Chris je sympatický chlapík, který ví co dělá. I [...]

I am just so stuck on this one. Is anyone able to embed font and use it in TextField when targeting Flash player 10.1 using flex 4.1 SDK? My code is very simple:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class Test1 extends Sprite
    {
        [Embed(source="arial.ttf", fontName="Arial", mimeType='application/x-font')]
        public static const FONT_ARIAL:Class;

        public function Test1()
        {
            var tf:TextField = new TextField();
            tf.text = "Hallo";
            tf.embedFonts = true;
            tf.defaultTextFormat = new TextFormat("Arial", 10);
            addChild(tf);
        }
    }
}

font file (.ttf) can be downloaded here. When I try to export the movie I see nothing but white. Actualy it seems like no matter what sdk nor flash player version I target, it still does not render text :-( what is going on flashplayer?

Mistery solved!, you have to set defaultTextFormat before text. Credits goes to Martin.

Where to go from here:

Chris Scott of Swiz is coming to Prague

by Tom Krcha on July 9, 2010


Setkání s Chrisem Scottem, autorem frameworku Swiz

by Viktor Bezděk on July 8, 2010

Adobe User Group CZ pořádá další meeting v úterý 13. července od 18:00 do 20:00 v prostorách Adobe Praha, Radlická 714/113a. Hostem bude Chris Scott, autor frameworku Swiz. Jedná se o jedinečnou šanci potkat se s tímto nezávislým světovým odborníkem na ActionScript. Chris má připravenou prezentaci a pak bude následovat volná diskuze. Chrisovo bio (en): Chris Scott [...]

P2P Chat with NetGroup in Flash Player 10.1

by Tom Krcha on July 8, 2010


Transparentní okna v AIRové aplikaci s Flex SDK 4

by Viktor Bezděk on July 7, 2010

Dnes jsem potřeboval vytvořit AIRovou miniaplikaci, která má vlastní chrome, jenže WindowedApplication (respektive Window/NativeWindow) ve Flex SDK 4 nemá property backgroundAlpha. Jak tedy na transparentní pozadí? Řešení není nijak závratně složité. Nejdříve je třeba nastavit v AIR Application Descriptoru hodnotu systemChrome na none a transparent na true. Tohle se nijak neliší od předchozí verze SDK. [...]

V pátek mi přestala fungovat licence na Flash Builder beta 1. Stáhl jsem tedy beta 2 a zjistil jsem, že SDK beta 1 v něm nefunguje. FB začne vyhazovat fatal errory a zanedlouho padne úplně. Nefunguje ani strom se soubory projektu. Kdo četl mé páteční příspěvky na Twitteru ví, že novou betu SDK považuji za [...]