AS3 Writing A Musical Loop

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.

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 Here