• 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
    • Ellen on Creating a simple APE example
    • tudway on Detecting focus in flash player
    • Markel on Detecting focus in flash player
    • Shawn on Sound in flash cs3 part 2 – pause
  • Categories

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

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

    March 2010
    M T W T F S S
    « May    
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  
  • games

    • Game Showcase
Aug 28

Sound in AS3 Part 3

flash, sound, tutorial 5 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!

Apr 09

Sound in flash cs3 part 2 – pause

flash, sound 4 Comments »

Ok, starting off with an example, heres an example where it loops infinitely and you are able to pause and resume it.

The code hasn’t changed much but there are some subtle differences that have a lot of effect on the outcome.
When the pause button is pressed a new variable is set which tells the play button where to replay the sound from. Lets look at the code.

import flash.media.Sound;
var sound:Sound = new acoust();
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, the new pause position variable is the first new thing, it is set at 0 when the movie starts, this is the start of the sound.
The next change is within the soundplay function. Where instead of the channel = sound.play(0, 99) with channel.play(pausePosition), so to recap, this is to play the sound from pauseposition, which i set earlier.
Next i add the event listener for when the sound finishes, so i can loop it. ( i can’t loop it the way i did last time as this interferes with the pause function)
The next change is the most important one, where i update the pausePosition when the stopbut button is pressed. This sets the place from which you can resume the play back when you press play.
The last thing i changed was to make a function which loops the music, it literally checks to see if the sound has finished and then replays the sound if it is.
Ok thats the basic sound tutorials over.
Apr 08

Sound in Flash CS3

flash, sound 1 Comment »
In flash, sometimes you might want to add a sound using actionscript, for example:- background music that repeats for ever, or a sound that plays only when you shoot a gun. I’m going to show you how to make this happen!   

1st, find a sound or some music that you have in your computer, when you have found one remember its file path.
Next open up flash cs3 and click File:Import:Import to Library. and then choose your sounds file path from the menu that pops up. 

Then, press control + L to open the library and something like this should appear. 
Obviously the sound name will be different, but it will look mostly the same as this.
Next right click on the sound name (highlighted) and choose linkage from the menu that appears.

 

A box should pop up that looks something like this, but without the “Export for ActionScript” checkbox ticked. 
Check this and then write a class name in the box that is highlighted on the screenshot. But you have to remember this, as we will be using it a bit later. I have called mine “acoust”, so if you have problems with the script i suggest you use that name.
Now we are ready to make the sound play.
just 3 lines of actionscript are required to make the sound play.
import flash.media.Sound;
var sound:Sound = new acoust();
sound.play(0, 99);
I am not going to make a demo, as it will be quite CPU intensive and also it will interfere with the later experiments.
Line 1: importing the necessary classes for the sound to play.
Line 2: Setting up a new variable “sound”, which is a copy of the class “acoust” that we set up earlier.
Line 3: Setting the variable “sound” to play 99 times starting at 0 miliseconds.
Now lets make some buttons that can stop and play the sound.
This script is a lot more complicated than the 1st script that i showed you this is because i have had  to play and stop the sound on command. For this i had to add 2 more variables, one to see if the sound was playing and another one that is a soundchannel variable. some code:-

import flash.media.Sound;
var sound:Sound = new acoust();
var channel:SoundChannel;
var soundisplaying:Boolean = false;
playbut.addEventListener(MouseEvent.CLICK, playsound);
stopbut.addEventListener(MouseEvent.CLICK, stopsound);
function playsound(event:Event):void {
if (soundisplaying==false) {
channel = sound.play(0, 99);
soundisplaying = true;
}
}
function stopsound(event:Event):void {
channel.stop();
soundisplaying =false;
}

The first thing that you will notice is the new soundChannel variable “channel”. This sets up a new channel which i will use to hold the sound.play() function.
Next, you will notice the “soundisplaying” boolean variable, this quite literally tells us if the sound is playing. I set this up to stop multiple versions of the sound playing when you pressed the play button. it is set to false to start with.
The next 2 lines are adding the event listeners for when the buttons are pressed. i have explained buttons in more detail in this tutorial.
You can see that i have added 2 new functions for controlling the sound. 
The first one plays the sound, but only is the boolean soundisplaying is set to false, that is what the if statement is for. The channel = sound.play is the same function as before, then i set the soundisplaying variable to true, so the user can’t play the sound more than once.
The next function is the one that stops the sound playing. You cannot directly stop the sound, so you have to stop the soundChannel that it is attached to, so the code is channel.stop().
I also set the soundisplaying boolean to false, so that if you click the play button you can restart the sound.
thats it so far, next time i will be focusing on pausing and then replaying the sound from the pause point.
Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml