CIS 421 Flash > Embedding Video into a .fla/SWF file

load & play an FLV file with ActionScript 3.0

Preparation

  • This version includes play and stop buttons, so you need to encode an FLV file without a skin in the Flash Video Encoder
  • OR import a video into a Flash movie, choosing Progressive Download.
    • When Flash imports the movie it creates an FLV file that is separate from the FLA file and the published SWF file
    • You can deploy this FLV file in other
  • Save it in the same directory as the Flash movie to make it easier to create the path for loading in the SWF file.

Flash file

  • Open a new Flash document and save it in the same directory as your FLV file.
    • The frame rate of the FLA movie does not have to be the same as the frame rate of an external FLV movie
  • Click View>Grid to show the grid on your Flash movie for lining up the buttons.
  • You need 4 buttons:
    • directions on how to create a button in Flash
    • Create 4 buttons, each with its own text.
    • Or create 1 button and use 4 instances of it. If you do that you need to type the text over the button after you insert it on the stage
  • Change the name of the first layer in the timeline to play button
    • Drag a (play) button onto the stage
    • Give it the instance name playBtn in the property inspector
    • Create text on it if you are using one button
  • Insert a new layer and repeat for
    • the stop button, giving it the instance name of stopBtn in the property inspector
    • the pause button, giving it the instance name of pauseBtn in the property inspector
    • the togglePause button, giving it the instance name of togglePauseBtn in the property inspector

Preparing ActionScript 3.0

  • Insert a new layer called ActionScript
  • Click in the first frame of the ActionScript layer
  • Click F9 to open the ActionScript window to insert the following Frame Actionscript
    • Be sure it says Actions-Frame in the uppler left of the ActionScript window.

ActionScript to Load movie

  • This ActionScript loads an FLV dynamically using the NetConnection and NetStream classes
    • The NetConnection class allows playing streaming FLV files from either an HTTP address or a local drive by passing the value null to the connect() method
  • The NetStream class provides 4 main methods for controlling video playback:
    • pause(): Pauses playback of a video stream. When the video is already paused, calling this method does nothing
    • resume(): Resumes playback of a video stream that is paused. If the video is already playing, calling this method does nothing.
    • seek(): Seeks the keyframe closest to the specified location (an offset, in seconds, from the beginning of the stream).
    • togglePause(): Pauses or resumes playback of a stream.
    • There is no stop() method.
      • to stop a stream you must pause playback and seek to the beginning of the video stream.
    • The play() method is used for loading video files and not for playing the movie.
  • Next a NetStream object is created, which
    • takes a NetConnection object as a parameter
    • specifies which FLV file you want to load.
  • Next it creates a new Video object
    • it attaches the previously created NetStream object
    • using the Video class's attachNetStream() method.
    • Then the video object is added to the display list using the addChild() method,.
  • This code is from the Flash ActionScript 3.0 help file.

var nc:NetConnection = new NetConnection();
nc.connect(null);

var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("FIRECAM1.flv");
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}

var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);

pauseBtn.addEventListener(MouseEvent.CLICK, pauseHandler);
playBtn.addEventListener(MouseEvent.CLICK, playHandler);
stopBtn.addEventListener(MouseEvent.CLICK, stopHandler);
togglePauseBtn.addEventListener(MouseEvent.CLICK, togglePauseHandler);

function pauseHandler(event:MouseEvent):void
{
ns.pause();
}
function playHandler(event:MouseEvent):void
{
ns.resume();
}
function stopHandler(event:MouseEvent):void
{
// Pause the stream and move the playhead back to
// the beginning of the stream.
ns.pause();
ns.seek(0);
}
function togglePauseHandler(event:MouseEvent):void
{
ns.togglePause();
}