31 August 2009
This article assumes that you are familiar with Flash Professional CS4 and have a basic understanding of delivering video through Flash Player and Flash Media Server. You should also have a basic understanding of XML and ActionScript 3.
Note: In response to reader requests, the extra sample file contains the code you need to add descriptions to the scrolling tile list in the dynamic video playlist (which now shows just titles). You simply add a field to the custom cell renderer class.
Intermediate
If you've published video on the web yourself, without the help of a portal like YouTube, you know how easy it is to import video into Adobe Flash and publish a SWF file that contains the video component and plays your movie file. (If you don't, the Video Learning Guide for Flash has a whole section on adding web video to your website.) More often than not, however, you want to publish multiple videos on the same web page—without authoring a new SWF for each one. What you want is a player that's flexible and shows any number of videos you point it to.
This article explains how to do just that. It provides a template with instructions for both streaming and progressive delivery of your video to a customizable player. The sample project you create includes a dynamic playlist using XML and Flash CS4 components that you can customize and extend.
If this video template looks familiar, it's because I wrote a tutorial about it back when Flash first featured a video workflow (Flash MX Professional 2004). In this thoroughly revised article, I've incorporated some of the feedback that readers have sent me over the years about developing playlists, answered many common questions, and demonstrated some of the most requested features.
Note: This tutorial describes how to create a dynamic playlist for both progressive and streaming delivery of video to Adobe Flash Player 9 and later. To find out how to build a similar playlist targeting older versions of Flash Player, see my past articles, Creating a dynamic playlist for progressive Flash video and Creating a dynamic playlist for streaming Flash video.
The code in this article should be clear enough to follow, even if you're just getting started with ActionScript 3—so don't be intimidated! Sample code is provided, along with some sample videos (vintage cartoons courtesy of the Prelinger Archives), so you can test your application right away.
The basic framework of the playlist application consists of the following (see Figure 1):
Comparing the pros and cons of streaming and progressive video is beyond the scope of this article. (Read the Video Learning Guide for Flash for an overview of Flash video delivery options.) Even so, you should decide which method of delivery to use before you begin. As a quick review, let's outline the major benefits and pitfalls of each.
Before beginning the development of your dynamic playlist application, you should understand how to deploy it to the web. The setup differs for progressive and streaming delivery, but only slightly. The biggest difference is where you place your video files. Take a closer look (see Figure 2).
No matter which delivery method you choose, you'll have to upload your application SWF and associated files to your standard web server. All of these files are required:
You'll also need to upload your video files. The location you place them depends on your delivery method.
For progressive delivery, just create a new directory called videos in the same location as the files above and copy all of your files there. After doing so, you're ready to test your application.
Note: You can download the free Flash Media Development Server 3.5 for local testing or sign up for an account with a Flash Video Streaming Service (FVSS) provider if you don't yet own a Flash Media Server license.
For streaming delivery, you will need to upload your videos to Flash Media Server (FMS). The setup is quite simple. This article assumes that you are running FMS locally, but the application setup would be the same if you were using a remote server. Follow these steps to set up your FMS application:
That's all the setup required on the server side for streaming delivery. You'll then be ready to connect to the application and test your streaming video playlist (once you create it, of course—I'm getting to that!)
The ActionScript for your playlist is identical for either deployment method. The only difference is in the source path of your videos in the playlist.xml file. I cover this in more detail in the next section.
This section describes the structure of the XML data files, playlist.xml, and playlist-streaming.xml, all of which are included in the sample files you downloaded on the first page of this article.
To add new videos to your playlist dynamically, simply edit the appropriate file: playlist.xml for progressive delivery or playlist-streaming.xml for streaming. Here's the code in playlist.xml:
<?xml version="1.0" encoding="UTF-8"?>
<playlist id="Adobe Developer Center - Dynamic Video Playlist in AS3" >
<vid desc="Popeye for President, Title and Credits"
src="videos/Popeye_forPresiden256K_flash_popeye.mp4"
thumb="thumbs/Popeye_forPresiden768K.jpg" />
<vid desc="Vote for Popeye"
src="videos/Popeye_forPresiden768K_001.flv"
thumb="thumbs/Popeye_forPresiden768K_001.jpg" />
<vid desc="Vote for Bluto"
src="videos/Popeye_forPresiden768K_002.flv"
thumb="thumbs/Popeye_forPresiden768K_002.jpg" />
<vid desc="Bluto's stolen all of Popeye's voters"
src="videos/Popeye_forPresiden768K_003.flv"
thumb="thumbs/Popeye_forPresiden768K_003.jpg" />
<vid desc="Popeye getting creative"
src="videos/Popeye_forPresiden256kb_flash_004_sm.mp4"
thumb="thumbs/Popeye_forPresiden768K_004.jpg" />
<vid desc="The fight for Olive Oyl's vote"
src="videos/Popeye_forPresiden768K_005.flv"
thumb="thumbs/Popeye_forPresiden768K_005.jpg" />
</playlist>
Notice that both sample files have a very basic XML structure, holding two main elements:
The src attribute must point to your video files.
For progressive videos (shown above), you update the src attribute with a relative path to the video from the SWF file; in this example, you've copied all the files into a folder aptly named videos.
For streaming video, just append your Flash Media Server address and application name to your video filenames, as shown in this playlist-streaming.xml example:
<?xml version="1.0" encoding="UTF-8"?>
<playlist id="Adobe Developer Center - Dynamic Video Playlist in AS3" >
<vid desc="Popeye for President, Title and Credits"
src="rtmp://localhost/videoplaylist/mp4:Popeye_forPresiden256K_flash_popeye.mp4"
thumb="thumbs/Popeye_forPresiden768K.jpg" />
<vid desc="Vote for Popeye"
src="rtmp://localhost/videoplaylist/Popeye_forPresiden768K_001.flv"
thumb="thumbs/Popeye_forPresiden768K_001.jpg" />
<vid desc="Vote for Bluto"
src="rtmp://localhost/videoplaylist/Popeye_forPresiden768K_002.flv"
thumb="thumbs/Popeye_forPresiden768K_002.jpg" />
<vid desc="Bluto's stolen all of Popeye's voters"
src="rtmp://localhost/videoplaylist/Popeye_forPresiden768K_003.flv"
thumb="thumbs/Popeye_forPresiden768K_003.jpg" />
<vid desc="Popeye getting creative"
src="rtmp://localhost/videoplaylist/mp4:Popeye_forPresiden256kb_flash_004_sm.mp4"
thumb="thumbs/Popeye_forPresiden768K_004.jpg" />
<vid desc="The fight for Olive Oyl's vote"
src="rtmp://localhost/videoplaylist/Popeye_forPresiden768K_005.flv"
thumb="thumbs/Popeye_forPresiden768K_005.jpg" />
</playlist>
This playlist-streaming.xml example file assumes that you are working with Flash Media Server running locally. If you are running a remote FMS server or using an FVSS provider, include your server address here instead of "localhost."
Go ahead and edit the XML file that corresponds with your delivery mode to play your own videos, or proceed using the provided sample videos for now.
Note: When you are streaming MPEG4 videos (MOV, M4V, F4V, MP4, etc.) with Flash Media Server, you'll need to append MP4: to the beginning of your filename to make your video play.
The src attribute for a streaming MPEG4 video looks like this:
src="rtmp://localhost/videoplaylist/mp4:Popeye_forPresiden256K_flash_popeye.mp4"
You do not need to add the "MP4:" prefix when using progressive delivery.
Now that you've established the data source for your playlist, launch Flash CS4 Professional and start working on the application interface.
Here I explain the structure of the finished player and walk you through the process of creating the interface in the Flash authoring environment.
The application has a relatively simple interface, consisting of an FLVPlayback component for the video display and a TileList component for the playlist (see Figure 3).
The TileList component is populated automatically from the contents of your XML file. The TileList component is perfect here because it allows you to easily include a small thumbnail preview of each video. Each item in this list links to the appropriate video file, which plays in the FLVPlayback component when clicked.
Create the interface by placing and configuring the two components. Open Flash and follow these steps:
autoPlay to false, and scaleMode to noScale. Since the dimensions of your videos will be automatically detected by the FLVPlayback component, the scaleMode setting will determine what to do if the videos are different sizes:noScale retains the original size of the video and resizes the playback skinmaintainAspectRatio resizes your video to fit the current dimensions of the component but does not stretch the videoexactFit stretches the video horizontally and vertically to match the dimensions of the component
The component automatically changes its placement on the page, depending on the setting for the align property. By default, the component remains centered on its current registration point. If you were to reset the registration point to align topLeft instead, the placement of the video would remain anchored at the top left corner and would shrink or grow from that point. (I've encoded one of the sample video files with smaller dimensions than the others to demonstrate this alignment behavior. See if you can find which one it is!)
At this point, your Flash file should look something like Figure 6.
That's all there is to creating the functional interface. Save this file; you'll create the separate ActionScript files next, and then open VideoPlaylist.fla again later to skin the playlist.
Are you ready for the fun part? The next section gets into the meat of the application—the client-side ActionScript.
This section explains how to read in, or parse, the XML document, create the dynamic playlist, and play selected videos. (I think you'll be surprised how remarkably simple it is to do all of this in Flash!)
As I mentioned previously, all of the ActionScript used in this project is contained in external class files. Begin by creating an ActionScript file called VideoPlaylist.as in Flash, Flex Builder, Dreamweaver, or in your favorite text editor. Save it in the same folder as VideoPlaylist_CS4.fla. I'll walk you through the code here, so you understand how it works.
Import the classes that are needed later in the script:
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import fl.controls.listClasses.CellRenderer;
import fl.controls.ScrollBarDirection;
Set up the class constructor and main VideoPlaylist method:
public class VideoPlaylist extends MovieClip {
private var xmlLoader:URLLoader;
public function VideoPlaylist():void {
// Load the playlist file, then initialize the media player.
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, initMediaPlayer);
xmlLoader.load(new URLRequest("playlist.xml"));
// Format the tileList, specify its cellRenderer class.
tileList.setSize(200, 240);
tileList.columnWidth = 180;
tileList.rowHeight = 60;
tileList.direction = ScrollBarDirection.VERTICAL;
tileList.setStyle("cellRenderer", Thumb);
}
The VideoPlaylist method loads the XML data. Once it is successfully loaded, it triggers the initMediaPlayer method. ActionScript 3 has given us some powerful XML handling features, so this part is amazingly easy.
The overall format of the TileList component is set up in this first method as well, specifying the size, row height, and scrollbar behavior—and assigning the class that will control what the playlist looks like. (I describe that class in the next section.)
Here's the initMediaPlayer method:
public function initMediaPlayer(event:Event):void {
var myXML:XML = new XML(xmlLoader.data);
var item:XML;
for each(item in myXML.vid) { // populate playlist.
// Get thumbnail value and assign to cellrenderer.
var thumb:String;
if(item.hasOwnProperty("@thumb")>0) thumb = item.@thumb;
// Send data to tileList.
tileList.addItem({label:item.attribute("desc").toXMLString(),
data:item.attribute("src").toXMLString(),
source:thumb});;
}
// Listen for item selection.
tileList.addEventListener(Event.CHANGE, listListener);
// Select the first video.
tileList.selectedIndex = 0;
// And automatically load it into myVid.
myVid.source = tileList.selectedItem.data;
// Pause video until selected or played.
myVid.pause();
}
The initMediaPlayer method accomplishes the following:
myXML object.Finally, set up the listListener function to handle change events fired by the TileList component:
// Detect when new video is selected, and play it
function listListener(event:Event):void {
myVid.play(event.target.selectedItem.data);
}
}
}
You're almost there. Now you have a video player that reads in a list of videos from your external XML data source and loads the first video in the list into the FLVPlayback component. Before you go ahead and test the app, however, you still need to set up the TileList component to accept the thumbnail of the video file. Just to get a bit fancy, I'll cover the process of customizing its skin as well—it's easier than you might think.
At this point, the application is clean and functional. However, the functionality for displaying the video thumbnails is not yet implemented.
To make the TileList component load your thumbnails, you need to set up a custom CellRenderer. While you're at it, why not customize the look of your list as well? Specify the skin for the mouseover and selected states of the video items.
Note: If you are integrating this code into your own custom file, it's important to note that the TileList will not populate correctly if you place it on a layer that is masked.
If you've worked with components in older versions of Flash, you're going to love how easy this is to do now. As you may remember, you've already assigned a CellRenderer in the main constructor of the VideoPlaylist.as class:
tileList.setStyle("cellRenderer", Thumb);
So, you now need to actually create this cellRenderer class. In the code editor of your choice, create a new ActionScript file and name it Thumb.as. Save this file in the same location as your VideoPlaylist.as file.
Import the classes that are needed later in the script:
package {
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ImageCell;
import fl.controls.TileList;
import flash.text.*;
Set up the main constructor:
public class Thumb extends ImageCell implements ICellRenderer {
private var desc:TextField;
private var textStyle:TextFormat;
public function Thumb() {
super();
loader.scaleContent = false;
useHandCursor = true;
// set skins
setStyle("upSkin", ThumbCellBg);
setStyle("downSkin", ThumbCellBg);
setStyle("overSkin", ThumbCellBgOver);
setStyle("selectedUpSkin", ThumbCellBgSelected);
setStyle("selectedDownSkin", ThumbCellBgSelected);
setStyle("selectedOverSkin", ThumbCellBgSelected);
// Create and format desc
desc = new TextField();
desc.autoSize = TextFieldAutoSize.LEFT;
desc.x = 75;
desc.width = 110;
desc.multiline = true;
desc.wordWrap = true;
addChild(desc);
textStyle = new TextFormat();
textStyle.font = "Tahoma";
textStyle.color = 0×000000;
textStyle.size = 11;
}
This main constructor accomplishes the following:
After you've added this code, save the Thumb.as file.
That's all that's required to create the custom CellRenderer. There's just one final step before you can go ahead and test your application—create some simple movie clips that will be used to customize the look, or "skin" of your playlist. You specified these movie clips in the Thumb.as code:
// set skins
setStyle("upSkin", ThumbCellBg);
setStyle("downSkin", ThumbCellBg);
setStyle("overSkin", ThumbCellBgOver);
setStyle("selectedUpSkin", ThumbCellBgSelected);
setStyle("selectedDownSkin",
ThumbCellBgSelected);
setStyle("selectedOverSkin", ThumbCellBgSelected);
Note: This step is optional. If you are happy with the default mouseover skins, you can remove the six setStyle lines from Thumb.as, then skip ahead to the section called "Publishing your video playlist."
Open your VideoPlaylist.fla in Flash. You'll be creating three new movie clips: ThumbCellBg, ThumbCellBgOver, and ThumbCellBgSelected. Follow these steps to use the movie clips as custom rollover states for your TileList component, and feel free to use other colors if you like:
That's it! You've just skinned your playlist.
With all of your ActionScript and playlist skins in place, you are now ready to publish the SWF. Select File > Publish or Control > Test Movie. The XML data will load into your playlist, and your first video should be cued up to play. Just click the play button or select an item in the playlist to start playing a video.
Note: If you're using progressive delivery, your videos can be previewed locally with no Internet connection required. If you are streaming your videos using Flash Media Server, be sure that your videos are copied into your Flash Media Server directory as outlined earlier, and that your server is running (either running locally or accessible via the Internet) in order to test your application.
One feature that you may want to add is support for full-screen video playback. This is remarkably easy to do. In fact, all of the video examples provided (and the code you've been working on so far) supports full-screen playback—and you didn't even know it!
All you need to do is specify the correct HTML template and publish your project. Select File > Publish Settings, check the box for HTML, and select the HTML tab. From the Template drop-down menu, choose "Flash Only – Allow Full Screen" (see Figure 9).
Click Publish, then OK. That's it! Now when you launch the HTML page in your browser, you can click the full-screen icon in your FLVPlayback skin (be sure to chose a skin that includes one) and voilà—full-screen video goodness!
The final step describes the process of deploying your files to the web for all the world to see.
The beginning of this article outlined the location of the video files for both progressive and streaming delivery. Now that you've developed the sample application, let's review. You will need to place your SWF file and other associated files in your web-accessible folder. And you'll need to copy your videos to either your web server (for progressive delivery) or Flash Media Server (for streaming).
The following files should be in your web-accessible folder on your web server:
The last step is to be sure your videos are uploaded in the proper place for your method of delivery. If you're unclear about where they belong, refer back to the section of this article entitled "Understanding the files and servers."
Once all of your files are in place on your server(s), navigate to VideoPlaylist.html in your favorite browser to test your dynamic video playlist, complete with thumbnails and a custom TileList skin.
Preview the dynamic video playlist template
Now that you've explored the elements that comprise a simple video playlist navigation using XML, you can extend this basic framework with other features as well:
In this tutorial, you created a flexible XML-based video playlist application that you can easily update, reuse, and reskin to your heart's content. Like most Flash developers, however, I'll bet you've got some custom features you'd like to add, don't you? Yeah, I thought so.
The framework I provided for you was meant to be streamlined and easy to follow, but you can easily extend it. In fact, there were some additional features that had been requested from my past articles that I've included for you. In the provided sample files, there are a few other variations you can play with:
VideoEvent.COMPLETE and trigger the next video in the playlist to play.Some other, very useful features that you could add include bandwidth detection (client-based for progressive delivery, or server-based using Flash Media Server), custom metadata, dynamic cue points, or video preloaders. Take this basic framework and be creative.
The following resources can help you get up to speed with video in Flash, Flash Media Server, and customizing the components:
Be sure also to play with the other video templates in the Flash Developer Center.
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |