• Home
tudwaythecore.com
blog
  • Recent Posts

    • Detecting focus in flash player
    • BitmapData PerlinNoise
    • Creating bridges in APE part 2
    • Creating bridges in APE
    • SteerWheels 2
    • Creating a simple APE example
    • Design me some levels!
    • Finished Migrating
  • Recent Comments

    • tudway on Creating a simple APE example
    • tudway on creating a platform game in as3 part 4
    • venu on Making a Shooting Game in AS3
    • chris on creating a platform game in as3 part 4
    • Kronoshifter on Creating a simple APE example
  • Categories

    • APE
    • flash
    • game
    • platform
    • sound
    • tutorial
    • Uncategorized
  • Archives

    • May 2009
    • October 2008
    • September 2008
    • August 2008
    • May 2008
    • April 2008
    • March 2008
  •  

    August 2008
    M T W T F S S
    « May   Sep »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • games

    • Game Showcase
Aug 28

Sound in AS3 Part 3

flash, sound, tutorial Add comments

Loading sound from an external source (i.e, not embedded in the .swf file) is really easy to do once you know how. I am going to use the same example as i used for the last tutorial but the sound is not going to be embedded in the .swf it is going to be hosted on my site

Here is the example. I have changed the sound to a whole song to demonstrate how quickly it takes to load.

Important!

Before i started i uploaded a .mp3 file to the internet so that i could call it with a URL request in this code.

You will need to do this for this code to work from the internet, alternatively you can put a sound file in the root classpath of your .fla (this is usually the same folder as the .fla).

Another important note

if you are loading a file on the internet change you publish settings for flash to “access network file only” and if you are loading it from a folder change it to “access local files only”.

To do this it was simple. I replaced one line of the old code for three more lines of code.

I replaced the line:

var sound:Sound = new acoust();

with:

var loadsound:URLRequest = new URLRequest(“http://where my mp3 file is on the internet.mp3″);

var sound:Sound = new Sound();

sound.load(loadsound);

Line 1: a new variable called loadsound which requests the file from the url mentioned inside the brackets and the “”

line 2: a variable like before that holds a blank instance of the sound class instead of the class that we made called acoust.

line 3: loading the sound that we got from the internet via the URL request into the blank instance of the sound class that we named earlier. 

Note: if you want to load the sound locally from a folder you just replace “http://where my mp3 file is on the internet.mp3″ with “sound name.mp3” 

The complete code for ease.

import flash.media.Sound;

var loadsound:URLRequest = new URLRequest(“http://where my mp3 file is on the internet.mp3″);

var sound:Sound = new Sound();

sound.load(loadsound);

var channel:SoundChannel;

var soundisplaying:Boolean = false;

var pausePosition:int = 0;

playbut.addEventListener(MouseEvent.CLICK, playsound);

stopbut.addEventListener(MouseEvent.CLICK, stopsound);

function playsound(event:Event):void {

if (soundisplaying==false) {

channel = sound.play(pausePosition);

soundisplaying = true;

channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);

}

}

function stopsound(event:Event):void {

pausePosition = channel.position;

channel.stop();

soundisplaying =false;

}

function soundComplete(event:Event):void {

channel = sound.play();

channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);

}

Ok this will hopefully help some of you.

i am going away for a week so don’t expect any more posts until i get back!

6 Responses to “Sound in AS3 Part 3”

  1. Stefan Says:
    November 14th, 2008 at 9:30 pm

    Thanks for your help! The pause and play work real well. But the loop glitches when it comes back around… I’m still looking for a solution to this.

    As a test play a sound loop like this:

    var sound = new loopedsound();
    sound.play(0, int.MAX_VALUE);

    and then plug it into the code above and you’ll hear the difference.
    I’ve been using a .wav file, encoded to .mp3 by Flash to avoid the ID3 glitch, but have tryed straight mp3′s as well.
    Seems like pausing a sound loop glitch free should be possible…

    But how??

  2. tudway Says:
    November 17th, 2008 at 11:40 pm

    i can see your problem now that i have looked a a small looping sound. As far as i can tell it is easily fixed by adding a soundisplaying=true; statement to the soundcomplete function.
    eg. function soundComplete(event:Event):void {
    channel=sound.play();
    soundisplaying=true;
    channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
    }

    This fixed the problem with my loops, i don’t know why i missed out the line. And i hope this has worked and helped.

  3. Stefan Says:
    November 18th, 2008 at 9:50 pm

    thanks for the reply – I was excited to try out your suggestion, but… no cigar. Try a tight sound loop and I think you’ll hear it.

    I now believe Event.SOUND_COMPLETE is good in theory, but doesn’t quite translate to real time application…

    I’ve spent way too much time on this… don’t know if you’ve come across this
    http://www.stevensacks.net/2008/08/07/as3-sound-channel-bug/
    but that seems to be the state of pausing loops in as3/Flash for now. Unfortunately.

    Let me know if you come up with some magic…

  4. tudway Says:
    November 18th, 2008 at 10:55 pm

    this is an example of the loop that i used for the test with the new line of code in.

    http://games.tudwaythecore.com/pause%20and%20play.html

    it works ok for me, how about you?

  5. Stefan Says:
    November 19th, 2008 at 12:53 am

    It sounds good, but I think if you tried a more densely populated multi-instrumental sound loop sample you would hear the difference.

    Believe me, I want it to work too. I’ve spent way too much time on this fueled by optimism and faith only to come to the conclusion that Adobe’s Flash/as3 is sorely lacking in the sound department.

    You did a great job with what we were given, my hat’s off…

  6. Gabriel Says:
    February 7th, 2010 at 8:33 pm

    What’s up about progressive “panning” sound by using pure AS3 inside the .fla file?…so that: No by ussing External AS3 Class file but pure AS3 inside the .fla

    I Have this code piece:

    //Starts Left Channel funcion

    function leftBal(e:Event):void
    {
    if(!muted)
    {
    soundVolume = volume_mc.slider_mc.x/100;

    if(sc!=null)
    {
    var trans1:SoundTransform = new SoundTransform(soundVolume,-1); //izquierda
    sc.soundTransform = trans1;
    muted=false;
    }
    }
    }

    But it don’t does progressive but total “pan” change…

    Any idea?

    Cheers!

Leave a Reply

Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml