Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

<< Prev 5 | Next 5

MP3Aplyer

/**
* @author: Eric Feminella
* @version 1.1
* @copyright: http://www.ericfeminella.com
*/

package {
        
        import com.dataProviders.XMLDataProvider;
        import flash.display.Sprite;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.net.*;
        import flash.events.*
        import mx.controls.TextArea;
        import mx.controls.DataGrid;
        
        
        public class MP3Handler {
                
                private var mp3:Sound;
                private var song:SoundChannel;
                private var document:XML;
                private var playlistDataProvider:Array;
                private var loader:URLLoader;
                private var mp3LoadEvent:Boolean = false;
                private var isPlaying:Boolean = false;
                private var trackIndex:int = 0;
                private var position:Number;
                private var trackTextArea:TextArea;
                private var playlistTextArea:TextArea;
                
                
                public function MP3Handler(url:String)
                {
                        this.loadDataProvider(url);
                }
                
                
                public function setUI(trackTextArea:TextArea, playlistTextArea:TextArea):void
                {
                        this.trackTextArea = trackTextArea;
                        this.playlistTextArea = playlistTextArea;
                }
                
                
                public function loadDataProvider(url:String):void
                {
                        this.loader = new URLLoader();
                        this.loader.addEventListener("complete", this.parseDataProvider);
                        this.loader.addEventListener("ioError", this.dataProviderLoadError);
                        this.loader.load(new URLRequest(url));
                }
                
                
                public function parseDataProvider(evt:Event):void
                {
                        this.playlistDataProvider = new Array();
                        
                        this.document = new XML(this.loader.data);
                        
                        var counter:int = 1;
                        
                        for each (var prop:XML in this.document.track) {
                                
                                var nodeObj:Object = {};
                                nodeObj.label = prop.@name;
                                nodeObj.data = prop.@src;
                                this.playlistDataProvider.push(nodeObj);
                                
                                this.playlistTextArea.text += "  " + counter + ".) " + nodeObj.label  + "\n";
                                
                                counter++;
                        }
                        
                        this.loadMP3(0);
                }
                
                
                public function loadMP3(i:int):void
                {
                        if (this.trackIndex > this.playlistDataProvider.length || this.trackIndex < 0) {
                                this.trackIndex = 0;
                        }
                        
                        if (!this.mp3LoadEvent) {
                                var url:String = "http://www.ericfeminella.com/portfolio/flash_applications/MP3Player_v1/" + this.playlistDataProvider[i].data;
                                var request:URLRequest = new URLRequest(url);
                                this.mp3 = new Sound();
                                this.mp3.addEventListener(Event.COMPLETE, completeHandler);
                                this.mp3.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                                this.mp3.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                                this.mp3.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
                                this.mp3.load(request);
                                
                                this.mp3LoadEvent = true;
                        }
                }
                
                public function dataProviderLoadError(evt:IOErrorEvent):void
                {
                        this.trackTextArea.text += "Stream I/O Error: " + evt + "\n";
                }
                
                private function completeHandler(event:Event):void
                {
                        this.trackTextArea.htmlText = "<b>Now playing:</b> " + this.playlistDataProvider[this.trackIndex].label;
                        this.mp3LoadEvent = false;
                        this.song = mp3.play();
                        this.isPlaying = true;
                }
                
                private function ioErrorHandler(event:Event):void
                {
                        this.trackTextArea.text += "Stream I/O Error: " + event + "\n";
                }
                
                private function progressHandler(event:ProgressEvent):void
                {
                        this.trackTextArea.htmlText = "<b>Loading:</b> " + this.playlistDataProvider[this.trackIndex].label + " " + Math.round(event.bytesLoaded  / event.bytesTotal * 100) + "%\n";
                }
                
                private function soundCompleteHandler(event:Event):void
                {
                        this.trackIndex++;
                        this.loadMP3(this.trackIndex);
                }
                
                public function stopMP3():void
                {
                        if (!this.mp3LoadEvent) {
                                this.trackTextArea.text = this.playlistDataProvider[this.trackIndex].label + " stopped";
                                this.position = 0;
                                this.song.stop();
                                this.isPlaying = false;
                        }
                }
                
                public function playMP3():void
                {
                        if (!this.mp3LoadEvent && !this.isPlaying) {
                                this.trackTextArea.htmlText = "<b>Now playing:</b> " + this.playlistDataProvider[this.trackIndex].label;
                                this.song = mp3.play(this.position);
                                this.isPlaying = true;
                        }
                }
                
                public function pauseMP3():void
                {
                        if (!this.mp3LoadEvent) {
                                this.trackTextArea.text = this.playlistDataProvider[this.trackIndex].label + " paused";
                                this.position = this.song.position;
                                this.song.stop();
                                this.isPlaying = false;
                        }
                }
                
                public function rrwwMP3():void
                {
                        if (!this.mp3LoadEvent) {
                                this.song.stop();
                                this.trackIndex--;
                                this.loadMP3(this.trackIndex);
                                this.isPlaying = false;
                        }
                }
                
                public function ffwwMP3():void
                {
                        if (!this.mp3LoadEvent)
                        {
                                this.song.stop();
                                this.trackIndex++;
                                this.loadMP3(this.trackIndex);
                                this.isPlaying = false;
                        }
                }
        }
}

Posted by: Eric Feminella | website http://www.ericfeminella.com
Prototype: MovieClip.addSound
// Credit to Pedro Aguilar (xperiments.com) for the basis of this MovieClip
// prototype.  It is similar to his XSound object, but I simplified the code
// and made it part of the movieClip prototype.  There is no need to define
// an empty movieClip at the beginning of your code, now, and you can control
// on which level the movieClip container is created.

MovieClip.prototype.addSound = function ( linkageID, levelID ) {
        this.createEmptyMovieClip( linkageID , levelID );
        obj = this[linkageID];
        obj.sound = new Sound(obj);
        obj.sound.attachSound(linkageID);
}

// Usage:
//        MovieClip.addSound("linkID", "levelID")
//
// Description:
//        This method will attach a sound to a newly created movieClip
//        whose instance name will match the linkage ID of the sound
//        clip.  This allows for an easier control of the sound clip,
//        more control with organization of your sound clips, and it
//        also allows the setVolume() method to act on the individual
//        clips, rather than globally.
//
// Parameters:
//        linkID = the linkage name of the Sound you wish to add
//        levelID = the Level on which you want the movieclip created
//
// Example:
//        CONSTRUCTOR:
//        _root.addSound("testSound", 100);
//              -or-
//        myMovieClip.addSound("testSound", 100);
//
//        TO CALL SOUND METHODS:
//        testSound.sound.method();
//              -or-
//        myMovieClip.testSound.sound.method();

Posted by: J Tomasino | website http://www.aloneone.com
Put a looping audio track in my movie as well as toggle it on and off
Probably the best way to add an audio track to your movie would be to load the music file as a seperate swf file. By doing this you reduce the download time of the main movie considerably. The following example uses the sound object:
1) Open up a new swf file. On frame 1 add the script:
mySound = new Sound();
mySound.attachSound("liabrarySound");
This action attaches your music track from the liabrary. To attach a file from the library: a) Select your file in the liabrary, click the options button then choose 'Linkage'.
b) In the linkage panel choose 'export this symbol' and type the name you want to give your file. I opted for' liabrarySound'.
2) Place two buttons on your stage, on and off. On the button to start your music:
on (release) {
        mySound.start(1,100);
}
This action will start your music after 1 second and loop it 100 times. 3) Button to stop your music:
on (release) {
        mySound.stop();
}
Save this movie to the same folder as your main movie. To load this movie into your main movie use the load movie action:
on (release) {
        loadMovieNum ("musicFile.swf", 5);
}
This action will load your audio track into level 5.
Posted by: No name | website http://
queueSounds();
[as]
/**********************************************************
QUEUE SOUNDS
this isn't really a sound object prototype but rather a
movieclip prototype that loads and queues sounds
within a movieclip and is fairly usefull. add sound linkages
to the arguments and the sounds will be played one after
the other untill the inputed list has finished
**********************************************************/
MovieClip.prototype.queueSounds = function() {
        this.nameArray = (this.nameArray == undefined) ? [] : this.nameArray;
        var nArL = this.nameArray.length;
        for (var i = 0; i<arguments.length; i++) {
                var name = arguments[i].split(" ").join("_");
                this.nameArray[(nArl+i)] = name+nArL;
                this["sound_"+name+nArL] = new Sound();
                this["sound_"+name+nArL].attachSound(arguments[0][i]);
                this["sound_"+name+nArL].t = this;
                this["sound_"+name+nArL].onSoundComplete = function() {
                        this.t.nameArray.shift();
                        if (this.t.nameArray.length>0) {
                                this.t["sound_"+this.t.nameArray[0]].start();
                        } else {
                                this.t.playing = false;
                        }
                };
        }
        if (!this.playin) {
                this["sound_"+this.nameArray[0]].start();
                this.playin = true;
        }
};
/**********************************************************/
//USAGE
_root.createEmptyMovieClip("soundHolder", 1)
_root.soundHolder.queueSounds("sound_linkageID_1","sound_linkageID_2","sound_linkageID_2")
// basically even if the sounds are still playing you can still
// add new sounds to the list and they will be added to
// and played accordingly
/**********************************************************/
[/as]

Posted by: buggedcom (aka Oliver Lillie) | website http://www.buggedcom.co.uk
Random Loop Sound
//this AS plays a random sound when movie start

var mySongs = new Array("s1", "s2", "s3", "s4", "s5);
var thisSong = mySongs[Math.ceil(Math.random() * mySongs.length-1)];
var mySound = new Sound(this);
mySound.attachSound(thisSong);
mySound.start();
mySound.onSoundComplete = function(){
mySound.attachSound(thisSong);
var thisSong = mySongs[Math.ceil(Math.random() * mySongs.length-1)];
mySound.start();

Posted by: anonymous | website http://www.nourl.com

<< Prev 5 | Next 5

Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.