views:

317

answers:

1

I'm trying to mask an FLV with a PNG alpha channel. I'm using BitmapData (from a PNG) but it's not working. Is there anything I'm missing? Cut up code below:

var musclesLoader:Loader = new Loader();
var musclesContainer:Sprite = new Sprite();
var musclesImage:Bitmap = new Bitmap();
var musclesBitmapData:BitmapData;

var musclesVideo:Video = new Video(752, 451.2);
var connection:NetConnection = new NetConnection();
var stream:NetStream;

function loadMuscles():void {
    musclesLoader.load(new URLRequest('img/muscles.png'));
    musclesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, musclesComplete);
}

function musclesComplete():void {
    musclesBitmapData = new BitmapData(musclesLoader.content.width, musclesLoader.content.height, true, 0x000000);
    musclesImage.bitmapData = musclesBitmapData;
    musclesImage.smoothing = true;
    musclesContainer.addChild(musclesImage);
    contentContainer.addChild(musclesContainer);
}

function loadMusclesVideo():void {
    connection.connect(null);
    stream = new NetStream(connection);
    stream.client = this;
    musclesVideo.mask = musclesImage; 
    stage.addChild(musclesVideo);
    musclesVideo.attachNetStream(stream);
    stream.bufferTime = 1;
    stream.receiveAudio(true);
    stream.receiveVideo(true);
    stream.play("vid/muscles.flv");
}

Outside this code I have a function that adds the containers to the stage, etc and places the objects in the appropriate spots. It sort of works - the mask applies, but in a square (the size of the boundaries of musclesBitmapData) rather than with the shape of the alpha channel.

Is this the right way to go about this?

+1  A: 

As you can see here:

Alpha channel masking

Alpha channel masking is supported if both the mask and the masked display objects use bitmap caching, as shown here:

// maskShape is a Shape instance which
includes a gradient fill.
mySprite.cacheAsBitmap = true;
maskShape.cacheAsBitmap = true;
mySprite.mask = maskShape;

which means that, unless you can cache your flv as a bitmap (99% sure you cannot), you are out of luck. This doesn't mean you cannot mask, of course, but just that alpha channel masking will not work for you.

Yar
Wow, I was also 99% sure I wouldn't be able to - hence why I asked here. But you can cache FLV as a bitmap! Amazing. Thanks for making me try this. I'll have to check CPU/RAM though...
James Roberts
@James Roberts You can cache FLV as a bitmap? Glad I said 99%, then :)
Yar