This page require Adobe Flash 9.0 (or higher) plug in.

Writing A Musical Loop in ActionScript 3.0

musicloop Sorry for the late posting, I’ve been buzy with my new IPOD lately :3. Today’s tutorial shows how to construct a musical loop in Action Script 3.0. There’s also a mute and un-mute button just in case many of you wondering how to do it.

Completing this tutorial you can some of you presentation animators (those that requires Action Script sound control on presentation loop) can resume your project without much worrying.

Is still almost the same as AS 2.0. The syntax may change a little bit. You can do much more now, they upgraded these sound object stuff.

Here take a look at the sample and scripted code below.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import flash.media.Sound;
import flash.media.SoundChannel;
var musicplayed:int = 0;
//Declare a bgMusic sound object that loads a library sound.
//Please check the library & make sure is attached to actionscript
//ClassName bgMusicLib
var MusicPlayer:Sound = new Music();
var songChannel:SoundChannel;
songChannel = MusicPlayer.play();
songChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
// Placed this in memory so that, to keep track of the button toggle on/off
musicplayed = 1;
this.music_mode.text = "Sound Looped";
function loopMusic(event:Event)
{
	//This function keeps tracks end of music state and plays 
	//the function again thus, making a scripted loop
	songChannel = MusicPlayer.play();
	songChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
}
 
btn_Music.addEventListener(MouseEvent.CLICK,playmute)
function playmute(event:Event){
	if(musicplayed == 1){
		songChannel.stop();
		musicplayed = 0;
		this.music_mode.text = "Sound Stopped";
	}else{
		songChannel = MusicPlayer.play();
		songChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
		musicplayed = 1;
		this.music_mode.text = "Sound Looped";
	}
}

Source file here Download

Other Cool Related Stuff

7 Comments so far »

  1. beedee said

    am November 16 2008 @ 9:33 am

    Great thing this, been looking for tutorials like this for ages… Thanks!!

    One thing though – been trying to get it to work, bit of a novice i’m afraid.. Got a button called ‘loop1_btn’ which I’ve plugged into your script, replacing the ‘btn_music’ – keeps bringing up errors though.. Not sure what I am doing wrong.. Do you have the source files for this to download by any chance? Thanks!!

    B
    (Melbourne, AU)

  2. Mr.GoodStuff said

    am November 16 2008 @ 11:07 am

    you have to import some mp3 / wav file to the library. Use Ctrl+R select your mp3 file into library then, select the file on the library and right click. Export settings export as actionscript called Music.

  3. beedee said

    am November 16 2008 @ 11:13 am

    Awesome! Thanks Mr GoodStuff :)

    One more question please – when the SWF is first opened, it plays straight away without the user clicking… How can I make it NOT play (until the user clicks the button?)

    I have already tried

    var soundOn:Boolean = true; //music is ON when we start

    (I set it to true, but it still played when the SWF opened… Maybe I did the wrong thing here?)

    Any tips would be great, thank you so much!

  4. beedee said

    am November 16 2008 @ 11:17 am

    …Sorry, I set it to FALSE, (not true) but it still played automatically… :(

  5. Mr.GoodStuff said

    am November 17 2008 @ 6:35 am

    That’s because I used that as a boolean to control the onclick event. Notice line 9 is playing the script by default. you will need to add one more line after line 9. songChannel.stop();

    You can refer to AS3: Scripting OnClick Play Sound for more info about how sound works. That’s the core basic function for all sound. This article is mainly on how to establish music loops.

  6. beedee said

    am November 17 2008 @ 9:55 pm

    Thank you again Mr. Goodstuff :) You are AWESOME!! I’ve been trying to get this going for ages!! Lightbulb is burning brightly for the masses :)

  7. Raymond said

    am January 7 2009 @ 12:43 am

    Thanks :)

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment:

CommentLuv Enabled