Posted by: sarapeter on: May 3, 2009
In ActionScript 3.0, SWF files are loaded using the Loader class. To load an external SWF file, your ActionScript needs to do four things:
load() method, passing the URLRequest instance as a parameter.addChild() method on a display object container (such as the main timeline of a Flash document) to add the Loader instance to the display list.Ultimately, the code looks like this:
var request:URLRequest = new URLRequest("http://www.[yourdomain].com/externalSwf.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
This same code can be used to load an external image file such as a JPEG, GIF, or PNG image, by specifying the image file’s url rather than a SWF file’s url. A SWF file, unlike an image file, may contain ActionScript. Thus, although the process of loading a SWF file may be identical to loading an image, when loading an external SWF file both the SWF file doing the loading and the SWF file being loaded must reside in the same security sandbox if you plan to use ActionScript to communicate in any way to the external SWF file. Additionally, if the external SWF file contains classes that share the same namespace as classes in the loading SWF file, you may need to create a new application domain for the loaded SWF file in order to avoid namespace conflicts. For more information on security and application domain considerations, see Using the ApplicationDomain class and Loading SWF files and images.
When the external SWF file is successfully loaded, it can be accessed through the Loader.content property. If the external SWF file is published for ActionScript 3.0, this will be either a movie clip or a sprite, depending on which class it extends.
If the external SWF file has been published with an older version of ActionScript, there are important limitations to consider. Unlike an ActionScript 3.0 SWF file that runs in AVM2 (ActionScript Virtual Machine 2), a SWF file published for ActionScript 1.0 or 2.0 runs in AVM1 (ActionScript Virtual Machine 1).
When an AVM1 SWF file is successfully loaded, the loaded object (the Loader.content property) will be an AVM1Movie object. An AVM1Movie instance is not the same as a MovieClip instance. It is a display object, but unlike a movie clip, it does not include timeline-related methods or properties. The parent AVM2 SWF file will not have access to the properties, methods, or objects of the loaded AVM1Movie object.
There are additional restrictions on an AVM1 SWF file loaded by an AVM2 SWF file. For details, see the AVM1Movie class listing in the ActionScript 3.0 Language and Components Reference.
Posted by: sarapeter on: May 3, 2009
Here’s a simple example of loading a .swf in AS3. I had a typo before but now it’s fixed – sorry about that. Also, I updated it so you don’t have to strictly use it in an Class setting.
Happy Coding…
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad()
{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(“MouseActions.swf”);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event)
{
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
Posted by: sarapeter on: May 1, 2009
I just discovered that Actionscript 3 has the ability to read the data that some .mp3 files have in them. To demonstrate I created a little movie below that will output all this data and dump it in a TextArea Component. In addition, I created a little analyzer demo for the right and left channels.
First I needed a mask for the analyzer, so I used my Rectangle.as class.
Here is the document class
Posted by: sarapeter on: May 1, 2009
Loading and playing external sounds in AS3 is not far off from its AS2 counterpart. The code, as usual in AS3, is a little different because of the new event model, but really boils down to the same thing.
Let’s take a look at the AS2 version:
And here is how to do that in AS3:
As you may have noticed, there is a strange work-around in AS3 for what used to be the onSoundComplete event. I did a bit of Googling before posting this and found an interesting post by Andre Michelle on the topic. That didn’t really solve my problem though, as I wanted to demonstrate how to do it in AS3, so I looked at the Flex reference for the AS2 to AS3 conversion of the onSoundComplete event and saw that it was replaced by, according to this, the soundComplete event in flash.media.SoundChannel. I went ahead and tried it out and it seemed to fire off fine on my machine, so here you have it.
Initially I had thought that the following lines of code would do the trick, but they didn’t:
Hence why I Googled and tried what I did above, which, as I’ve already stated, seemed to fix the problem. I’m no expert on sound in Flash but if it works, why question it? ![]()
NOTE: I’ve obviously removed the “music.mp3″ file from the download so just grab any .mp3 you have, put it in the same folder that the example files are in, and rename it to music.mp3.
Posted by: sarapeter on: May 1, 2009
Here goes Flash Action Script 3 MP3 Player – Part 1. Need Your comments and support on the articles.
I am writing this first version of Flash Action Script 3 MP3 for beginners so that they can have an idea of Flash Action Script 3 Sound Classes. Also this code uses memory optimization techniques to reduce memory leaks.
To Learn More About Memory Optimization read my previous post
http://actionscript-blog.imaginationdev.com/Memory-Optimization-Flash-Action-Script-3
Flash Action Script 3 MP3 Player can be developed in 2 ways. 1- Songs are played once they are completely downloaded on the local machine, Part 1 of my Flash Action Script 3 MP3 Player uses this techniques. this approach is suitable when developing a desktop application. 2- Songs are buffered and streamed from the server and played simultaneously. This type of approach is common for Online Flash Action Script 3 MP3 Player. Part 2 of this article series will cover the 2nd methodology.
Building a Flash Action Script 3 MP3 Player is really easy using Flash Action Script 3 classes. Once you have the concepts of Sound class, SoundChannel class and Timer class in Flash Action Script 3 you are ready to develop a Media Player in Flash Action Script 3. In case you need guidance and help in the above mentioned classes you are welcome to leave comments and i will respond as soon as possible.
This might not be the best Flash Action Script 3 MP3 Player. This sure will guide you how to develop a Flash Action Script 3 MP3 Player using flash action script. There are number of flash media players available on the internet but very few i saw focussed on the fact that action script sound class has some problems when optimizing memory space. So i thought to write one sample of my own.
This flash MP3 player is easily customizable and reusable. In coming future i plan to write a Flash MP3 Player Component using Action Script3. Hopefully that article will also cover the basis of creating a flash component.
You can use the flash action script 3 Mp3 player code to build your own customized Flash Action Script 3 MP3 Players. Basic idea will always remain the same.
Flash Action Script 3 MP3 Player development starts with the code for loading XML and inserting data in the play list.
///////////////////////////////////////////////////////////////// //Loading XML /////////////////////////////////////////////////////////////////
var xmlData:XML; var xmlLoader:URLLoader = new URLLoader();xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("playlist.xml"));
function LoadXML(e:Event):void {
trace(LoadXML); xmlData = new XML(e.target.data);function getValues(input:XML) {var xmlCategoryList:XMLList = input.children(); var item:XML;var intCount:int = 0; for each (item in xmlCategoryList) {playList.addItem({label:item.title, data:item.url}); intCount = intCount + 1;}}//end of get Values Function getValues(xmlData);
}// end of Load XML
After that we will wait until user clicks on any song in play list. For handling play list event we will write the following code.
///////////////////////////////////////////////////////////////// //Play List Handler Function /////////////////////////////////////////////////////////////////
playList.addEventListener(Event.CHANGE, playListChangeHandler);
function playListChangeHandler(event:Event) {
if (flagSoundChannel==1) {soundChannel.stop();} else if (flagSoundChannel==0) {flagSoundChannel=1;} playSound(event.target.selectedItem.data);timeSlider.value=0;
}// end of change Handler
Above code calls another important function playSound. This method loads the new sound in the sound Object.
///////////////////////////////////////////////////////////////// //Play Sound Function /////////////////////////////////////////////////////////////////
function playSound(path) {
songPath.url = path;if(playerObject.sound!=null)delete playerObject.sound;playerObject.sound = new Sound(); addSoundEvents(); playerObject.sound.load(songPath);
}
Above code requires sound events to be handled before a new sound object is created. If you focus on the delete playerObject.sound; statement, this deletes the previous allocation of memory for the sound object. If we don’t do this a lot of memory will leak and eventually application will crash.
///////////////////////////////////////////////////////////////// //Sound Loading Event Handler Function /////////////////////////////////////////////////////////////////
function addSoundEvents():void
{
playerObject.sound.addEventListener(Event.COMPLETE, funcSoundLoaded);
playerObject.sound.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
playerObject.sound.addEventListener(ProgressEvent.PROGRESS,funcOnLoadProgress);
}
function funcSoundLoaded(event:Event) {
var soundObject:Sound = event.target as Sound;
var totalSoundDuration:Number;
soundChannel=soundObject.play();
totalSoundDuration=(soundObject.length/1000)/(60);
totalSoundDuration=Math.floor((totalSoundDuration*100));
totalSoundDuration=totalSoundDuration/100;
timeSlider.maximum=(soundObject.length/1000);
sliderDivisionValue=totalSoundDuration;
fileTimeInSec = Math.floor(playerObject.sound.length/1000);
secTimer.start();
}//end of Function Loaded
function funcOnLoadProgress(event:ProgressEvent):void {
var loadedPct:uint =Math.round(100 * (event.bytesLoaded / event.bytesTotal));
trace("The playerObject.sound is " + loadedPct + "% loaded.");
}
function onIOError(e:IOErrorEvent) {
trace("The playerObject.sound can not be Loaded"+ e.text);
}
Above code implementation of sound event handlers. Lastly we will add Timers and GUI element event handlers. ///////////////////////////////////////////////////////////////// //Control Handlers UIDesign /////////////////////////////////////////////////////////////////
btnStop.addEventListener(MouseEvent.CLICK,funcStop);
btnPause.addEventListener(MouseEvent.CLICK,funcPause);
btnPlay.addEventListener(MouseEvent.CLICK,funcPlay);
btnForward.addEventListener(MouseEvent.CLICK,funcForward);
btnBack.addEventListener(MouseEvent.CLICK,funcReverse);
soundChannel.addEventListener(Event.SOUND_COMPLETE, funcComplete);
function funcComplete(event:Event) {
soundChannel.stop();
timeSlider.value=0;
currPosition=0;
}
function funcStop(event:Event) {
secTimer.reset();
timeSlider.value=0;
currPosition=0;
soundChannel.stop();
flagSoundChannel=0;
}
function funcPause(event:Event) {
secTimer.stop();
currPosition= soundChannel.position;
soundChannel.stop();
btnPlay.visible=true;
btnPause.visible=false;
}
function funcPlay(event:Event) {
soundChannel.stop();
soundChannel=playerObject.sound.play(currPosition);
btnPlay.visible=false;
btnPause.visible=true;
secTimer.start();
}
function funcForward(event:Event) {
if (soundChannel.position+forwardDuration<=playerObject.sound.bytesTotal) {
timeSlider.value+=forwardDuration/1000;
soundChannel.stop();
soundChannel=playerObject.sound.play(soundChannel.position+forwardDuration);
}
else
{
timeSlider.value=0;
currPosition=0;
soundChannel.stop();
flagSoundChannel=0;
}
}
function funcReverse(event:Event) {
if (soundChannel.position-forwardDuration>0) {
timeSlider.value-=forwardDuration/1000;
soundChannel.stop();
soundChannel=playerObject.sound.play(soundChannel.position-forwardDuration);
}
else
{
timeSlider.value=0;
currPosition=0;
soundChannel.stop();
flagSoundChannel=0;
}
}
///////////////////////////////////////////////////////////////// //Control Handlers Sliders /////////////////////////////////////////////////////////////////
timeSlider.addEventListener(SliderEvent.THUMB_PRESS,sliderPressed);
timeSlider.addEventListener(SliderEvent.THUMB_RELEASE,sliderReleased);
timeSlider.addEventListener(SliderEvent.CHANGE,sliderChangeHandler);
timeSlider.liveDragging =true;
function sliderChangeHandler(event:SliderEvent) {
secTimer.stop();
soundChannel.stop();
soundChannel=playerObject.sound.play(event.target.value*1000);
secTimer.start();
}
function sliderPressed(event:SliderEvent) {
}
function sliderReleased(event:SliderEvent) {
secTimer.stop();
soundChannel.stop();
soundChannel=playerObject.sound.play(event.target.value*1000);
secTimer.start();
}
///////////////////////////////////////////////////////////////// //Control Handlers Timers /////////////////////////////////////////////////////////////////
secTimer.addEventListener(TimerEvent.TIMER, onTick);
secTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
function onTick(event:TimerEvent):void {
trace("Timer is running");
timeSlider.value+=1;
}
function onTimerComplete(event:TimerEvent):void {
trace("Time's Up!");
}
For Further details on voice control and sound buffering you may contact me. I will soon be writing part 2 of the flash MP3 Player series with voice control and Sound Buffering.
Kindly Let me know about your remarks if this article is being helpful in solving your problem.
Download Sample Project : Flash MP3 Player Using Action Script 3
Posted by: sarapeter on: May 1, 2009
Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!