14.3. Adding a Video Object at Runtime

Problem

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

Solution

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

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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.