There are a number of different ways you can play audio through Flash, each has it’s advantages and disadvantages.
PLAYING AUDIO FROM THE LIBRARY
In order to play an audio file from the library, you have to give it a class name. Control or right click on the file name in the library and open the file properties. On the sound properties panel, click the “Advanced” Button in the bottom right of the panel. In the section that opens up, click the “Export for Actionscript” and “Export in Frame 1″ boxes, and give the file a class name. The name of the audio file will automatically get filled in, but you should change this, having the extension in the name will cause problems.
Now you must create a new instance of this sound in your code, using the class name that you set above:
var track:myTrack = new myTrack();
This won’t automatically start the sound playing, however, you have to tell it to play:
If you want to stop the sound, things become a little more complicated, as the stop method is actually part of a different object – the SoundChannel class. So you’ll need an instance of this, and you will need to assign that channel to the playback of your file:
var channel:SoundChannel = new SoundChannel( ); channel = track.play(); channel.stop();
This still isn’t the most ideal way to play audio, however, because the audio file becomes part of the .swf and adds to the file size and download time. Any audio file that is more than a few seconds, you should load into the .swf much in the same way that we did with images.
LOADING EXTERNAL AUDIO FILES
This works similarly to the library example, except that we load the sound into a blank generic sound object, using the URLRequest object to hold the value of the audio file name:
var track:Sound = new Sound(); var request:URLRequest = new URLRequest(filename.mp3); track.load(req);
Adding Video To Your Project
Flash has several options for adding video to your project.
Flash supports it’s own FLV Video formats as well as Quicktime movies that are H264 encoded. There is a list of supported codes here.
There is also a good article about using H264 video here
USING ADOBE MEDIA ENCODER
You can also convert just about any video format to FLV using Adobe Media Encoder, which is included with Flash Professional. Generally speaking, using FLV in your Flash applications is going to be a smaller file, and better looking and functioning experiences for the user.
However, in order to play an FLV video, you need to create a .swf to act as a player. There are two ways you can play video through a .swf, either by importing it to the Timeline / Library, or using a video component or ActionScript to load in an external file.
IMPORTING VIDEO TO THE TIMELINE OR LIBRARY
I can’t stress enough that you should only use very short video that has no audio track with this method. Video will significantly increase the size of your .swf and load times and this method has serious audio sync issues.
Under the File > Import menu is an Import Video option. When you choose this, you will see the following dialog:
Next choose the embedding method. You can choose to place the embedded video directly on a layer of the timeline, or as a movieclip or graphic symbol.
USING VIDEO COMPONENTS TO LOAD VIDEO REMOTELY
Loading video from an external file is a much preferred method, and Flash makes it easy. Choose the “Load external video file with playback component” option from the Import Video panel instead, or you can also drag a FLVPlayback component from the Components (Window > Components) panel directly to the stage:
Under the Video Folder you’ll see several playback component options, as well as the individual elements of the playback component. Drag the component to the stage to add it to the Timeline and your Library.
You can set the appearance and source video of the playback component in the Properties Panel:
Because the FLV video must be embedded in a swf player, it has the same platform limitations as any application that uses the Flash Player (ie – no iOS devices). So, if all you need is video playback within a Web site, consider these options:
- Host the videos on a site like YouTube, Vimeo, or blip.tv and copy and paste their embed code into your html
- Use a third-party player like JWplayer that detects the users operating system and browser and delivers the appropriate player for that configuration (note, this requires having video in multiple formats)
Next week your designs are due, and we will have an all-class work session.