Bits & Bytes

Posts Tagged ‘flash’

Playing a Video File in Actionscript 3.0

We begin by creating an Actionscript 3.0 project and setting the stage dimensions to match our video. In this case, the video is 240 by 180 pixels. Then we add this code, compile, and execute. To play the video file, click the screen as shown above.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";
addChild(qInstructions);

// Create a video object with 240x180 pixels
var MyVideo:Video = new Video(240, 180);
addChild(MyVideo);

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

var qNetStream:NetStream = new NetStream(qNetConnection);
MyVideo.attachNetStream(qNetStream);
// Add this line to ignore metadata and cue points
qNetStream.client = new Object();

// Play the video when the screen is clicked
stage.addEventListener(MouseEvent.CLICK, OnClick);
function OnClick(e:MouseEvent): void {
	qNetStream.play("http://xoax.net/public/XoaX.flv");
}

The first part of the program outputs the message telling the user to click the screen. Next, we create a Video object that matches the dimensions of our video. Then we create a NetConnection object and call connect() with the argument value null to signal that we are going to load a sound or video file. Finally, we create a NetStream object using the NetConnection and attach it to the Video.

Once this is done, we can play a video file from anywhere: either our local drive or a remote location on the web. To make the video wait to play until the user clicks, we have wrapped the call to play() in a callback that is called when the stage is clicked.

Editing the Properties of an Object in Adobe Flash CS4

For this post, the layout that I am using is the Animator layout. You can select a layout from the drop-down menu in the top-right corner of the window.

We want to show how to edit the properties of an object. To start, we have a newly created project open with a simple line drawn it (shown above). Make sure that line has is selected, as indicated by the rectangular selection box around the bounds of the line.

If the object is not selected, you can select it by left-clicking the Selection Tool in the Tools toolbar (shown above) and then left-clicking the object that you want to edit. Note that if you do not have the Tools toolbar open, you can open it by left-clicking Windows from the menubar and left-clicking Tools in the submenu.

Once you click, on the object, you should see the properties of the object in the Properties pane (shown above). If you do not see the Properties pane, you can open it by left-clicking Windows from the menubar and left-clicking Properties in the submenu. In the Properties pane, you can edit the properties of the object.

Playing Music or Sounds in Actionscript 3.0

For our first example, we demonstrate how to add a simple sound file to a newly-created project and play it. We start by adding the sound file to the project.

Go to the menubar and select
File->Import->Import to Library…

Then select the sound file that we want to play, something like an .mp3 or .wav file, and left-click the “Open” button. Then right-click the file under the “Library” tab to open the context menu and left-click “Properties…” in the context menu. This opens the “Sound Properties” dialog.

At this point, you might need to left-click the “Advanced” button to open the advanced options inside the “Sound Properties” dialog. In the “Linkage” area, left-click the check box next to “Export for Actionscript.” Then change the name next to the “Class:” box to something appropriate for the class name of your sound. The simplest idea for the class name is to just remove the file extension (.mp3 or .wav, for example). This is what it looks like for my example:

Finally, left-click the “OK” button to create the class. Now, “Export: XoaxTheme” is under “Linkage” in the “Library” tab next to the file name “XoaxTheme.mp3”.

We finish the program, by adding these lines in the “Actions – Frame” tab, where our main program code goes:

var qMySound:Sound = new XoaxTheme();
qMySound.play();

Now, we can compile and execute our code from the menubar by selecting
Debug->Debug Movie

This plays the sound file that we added and finishes our first example. This simple example only uses two lines of code. In our next example, we demonstrate how to loop this music with the code in a separate class file.

 

© 2007–2024 XoaX.net LLC. All rights reserved.