Introduction
This article will explain, how to use Aspose.Flash to play a sound with ActionScript. It is recommended to read the previous article Creating Animation Using Aspose.Flash to have some background knowledge of Flash File Format.
Play sound using ActionScript
All those familiar with actionscript might know it already. Below is the actionscript code that plays a sound namely sound.mp3.
var s=new Sound();
s.loadSound( “c:\\sound.mp3” , true);
As a breief explanation, we are creating here a Sound object and then calling its method loadSound. The first parameter passed to is the file path of our sound.mp3 and second parameter specifies that sound starts to play as soon as sufficient bytes are loaded i.e the sound.mp3 needs not to be fully loaded before it starts playing.
How Flash Player executes ActionScript
All the ActionScript code is present in a Flash file inside DoAction tag in compiled form. DoAction instructs Flash Player to perform a list of actions when the current frame is complete. The actions are performed when the ShowFrame tag is encountered, regardless of where in the frame the DoAction tag appears.
Aspose.Flash ActionScript Compiler
Aspose.Flash provides a class namely Aspose.Flash.Actionscript.Compiler that can be used to compile action script into list of byte code actions. We will feed this list of actions into DoAction tag.
Playing Sound
Now we have all the necessary information to play sound. Below are the steps to play a sound file via ActionScript using Aspose.Flash
Steps
- Create a FlashContainer object, set its frame rate and frame size.
- Compile the ActionScript into list of actions using the Compiler class.
- Create DoAction tag and insert above list of actions into it.
- Add the DoAction into the FlashContainer.
- Create and add the ShowFrame tag into the FlashContainer. As soon as ShowFrame tag will be encountered, it will execute all the list of actions inside the DoAction tags before it.
- Write the FlashContainer to disk as a SWF file.
Example Code
C#
//Step 1
FlashContainer flash = new FlashContainer();
flash.FrameRate = 12.0f;
flash.FrameSize = new Rect(0, 0, 10000, 10000);
//Step 2
string source = "var s=new Sound(); s.loadSound(\"c:\\sound.mp3\",true);";
UTF8Encoding encoder = new UTF8Encoding();
MemoryStream ms = new MemoryStream(encoder.GetBytes(source));
Compiler compiler = Compiler.CreateDefaultCompiler(ms);
IList lst = compiler.CompileLines();
//Step 3,4
flash.Add(new DoAction(lst));
//Step 5
flash.Add(new ShowFrame());
//Step 6
flash.Write("c:\\outPlayingSound.swf");
The code above will generate a flash file named outPlayingSound.swf. Running it will play sound.mp3 file in C: drive.
Conclusion
In this article we learnt about the DoAction tag which contains the actionscript in the form of byte codes. To generate the byte codes for it, we used Aspose.Flash Compiler class which compiled our code and returned list of byte code actions. So we were able to feed these list of actions inside the tag. We also learnt that actions inside the DoAction tag are executed as soon as ShowFrame tag is encountered by Flash Player.