Recipe 14.3 Adding a Video Object at Runtime

14.3.1 Problem

You want to add video (and audio) to a Flash movie at runtime.

14.3.2 Solution

Create a movie clip symbol with an embedded Video object and set it to export for ActionScript.

14.3.3 Discussion

Although there is no way to create a Video object at runtime, you can create a movie clip with an embedded video at authoring time. If you set the movie clip symbol to export for ActionScript, you can add instances of the movie clip (including the embedded video) to your Flash movie at runtime with an attachMovie( ) method, as you would with any other exported movie clip. Here are the steps you should follow:

  1. Open the Library and choose New Video from the Library's pop-up Options menu to create a new video symbol.

  2. Create a new movie clip symbol (using New Symbol) and drag an instance of the video symbol into it, positioning the video at (0,0).

  3. Name the instance of the video symbol my_video.

  4. Open the linkage settings for the movie clip symbol (by choosing Linkage from the Library's pop-up Options menu) and select the Export for ActionScript and Export in First Frame checkboxes. Enter a linkage identifier of VideoMcSymbol.

  5. At the point in your ActionScript code where you wish to add a video, use the attachMovie( ) method to attach an instance of VideoMcSymbol, as follows:

    // Create an instance of the video movie clip symbol.
    _root.attachMovie("VideoMcSymbol", "myVideo_mc", 1);
  6. When you want to target the video (to attach a net stream, for example), you should use the path to the wrapper movie clip instance followed by my_video (to target the nested video object), as follows:

    // Invoke the attachVideo(  ) method of the nested video object.

There are occasions when it is appropriate to create all the video instances at authoring time. However, in other cases it may be desirable or even imperative to add video instances dynamically at runtime. For example, to create a video conferencing application that can accept any number of clients, you must add video instances using the technique discussed in this recipe.

14.3.4 See Also

Recipe 7.19. Note that adding the video symbol to the movie doesn't play any content. To play a video, you must add video content, as shown in step 6. For information on adding local video content to a video object, see Recipe 14.4. For information on adding video content from a net stream (content from the FlashCom server), see Recipe 14.7.

    Part I: Local Recipes
    Part II: Remote Recipes