By: Tom Green
Page 1 of 2
This past week I picked up an iSight for my Mac. Once I got over the initial thrill of having iChat play "All Tom, All the time", I started wondering: "What can I do with the video feed from the camera." It turns out, if you have Flash MX 2004, the answer is "Quite a bit."
This article actually had its genesis at TODCON. I delivered a session on the subject of "Video and Flash" at that Conference and the next day I was on a plane to Toronto for another speaking engagement at FlashintheCan. Video was at the top of my mind, so I couldn't help but notice Craig Swann wandering around with his Mac TiBook with an iSight camera clamped to the screen.
He was participating in the "Iron Flash" competition and was using his iSight to capture one image every second over the next 24 hours.
The next day, Sunday, Kevin Lynch, Macromedia's Technology "guru", was talking about what was coming down the pipe. He mentioned that the Flash Player 7 had only 30% market penetration to date but studies were showing that number would rise to over 70% (critical mass) by the end of this summer. If this is true, there is going to be a serious amount of Flash video that is going to be hitting the web over the next six months or so.
The day after FlashintheCan, I invited Brendan Dawes -http://www.brendandawes.com- to drop by my College to run a series of seminars for my students. One of the the things he was showing was how to manipulate a video stream in Flash. He too, had an iSight camera attached to his iBook which he was using to demonstrate some really cool stuff he was doing with live video feeds.
Which got me thinking, "How do you run a live video feed from your web cam into FlashMX 2004?
The Flash MX Video Capture Basics
What you don't do is simply plug a web cam into your Firewire or USB port and expect it to play through Flash MX 2004. The steps are:
1. Create a video instance on the stage.
2. Create a camera object by using a Camera class in Actionscript.
3. Feed the video stream into that object.
4. Compile the .swf.
This article will walk you through that process and, if you are a Mac user, point out a major "gotcha" that had me ripping the hair out of my head for a couple of days this week.
Building the Video Capture Application
Before we get started, this tutorial won't turn your computer into a desk-bound production studio. I am strictly going to focus on how to create a viewer that plays the stream from your web camera in a Flash .swf file. I am sure that one of the other partners will take you to the next level. The core skill here is working with Flash's camera object. Once you master that technique, you are only limited by your creativity.
1. Launch Flash MX 2004 and set your stage size to 320 X 240 pixels.
2. Add a new Layer and name it Actions.
3. If it isn't already open, open the Library Panel ( Command-L... Mac or Control-L... PC).
4. When the Library Panel opens, open the Panel Menu in the top left and select New Video from the pop-down menu.
Image 1: Select New Video from the Library Panel's Panel Menu.
This will place a new item in your Library named "Embedded Video". This video symbol is nothing more than a blank video that will playback the video stream at run time.
5. Drag the video symbol from the Library onto the stage in Layer 1 and resize it if necessary. The symbol's default size is 320 X 240. One thing you will notice it the symbol is a box with a big "X" inside it. This just lets you know it is there.
6. Select the video instance on the stage and in the Property inspector, give it an instance name. I used "my_video".
You have now completed the "hard work". The next step is to let Flash know there is a camera attached to your computer and to flow the video into the Video instance on the stage.
7. Select the first frame in the Actions layer. Open the AS Editor- press the F9 key or select Windows > Development Panels > Actions.
8. Click once inside the Editor and enter the following line of code:
my_cam = Camera.get();
This line of code creates the Flash Camera Object and gives it the variable name of "my_cam".
The Camera Object
The Camera Object is rather static. By that I mean there can only be one active camera- a Camera Object- being referred to at any one time by the Flash Player. You can have multiple cameras, such as the situation where the Flash movie is being used in a multimedia installation, but there can only be one video feed into the .swf at any one time. The camera is chosen through the Camera tab of the Flash Player setting dialog box. More about that one later on.
If there are multiple cameras in use, Flash will assign each one a unique identifier number- 0, 1, 2 and so on. In this instance the code that would create a camera object for the camera with the "1" would read:
my_cam = Camera.get(1);
9. Press the return key and enter the following line of code into line 2 of the Actionscript editor:
This single line of code is what feeds the video from the camera object into the video instance on the stage. In plain english it reads: Attach the video feed from the "my_cam" object to the Embedded Video instance named "my_video".
That's all you need to do. These two lines of code, which you have just written, are all you need to get going:
10. Close the Actionscript Editor. Save the file and select File > Publish to create the .swf.