Hi folks,
I am working on a Flash / AS3 project which has several audio tracks and starts off with an intro track. When the user clicks a button a window opens and the audio track changes, the new audio track continuously loops until the user closes the window. When the window is closed the audio track switches to a default audio track which is similar to the intro track. The problem I am having is that when the user clicks on the button and the window opens, the audio track plays as expected, but when it reaches the end, it doesn’t loop, instead the intro track starts to play instead (and loop I assume).
Here is the code I am working with:
package {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.media.SoundTransform;
import flash.media.SoundMixer;
import com.greensock.TweenMax;
import com.greensock.easing.*;
import com.greensock.events.*;
public class MusicEN extends MovieClip {
//Create tween
public var myTween:TweenMax;
//Create a new sound object
public var music:Sound;
//Set location of sound file – if it is external – using a URLRequest object
public var file:URLRequest;
//Create a new SoundChannel object
public var channel:SoundChannel;
//Boolean checks if the sound is playing
public var musicIsOn:Boolean = true;
//Mute Functions
public var MuteFunc:SoundTransform = new SoundTransform();
public function startMusic():void{
music = new Sound();
file = new URLRequest("audio/track-intro.mp3");
music.load(file);
playMusic(music);
}
public function mainMusic():void {
music = new Sound();
file = new URLRequest("audio/track-main.mp3");
music.load(file);
changeMusic(music);
}
//Sound Transform Actions For "Mute" and "Unmute"
//Mute
function Mute(e:Event):void {
MuteFunc.volume=0;//Close the volume of the music.
SoundMixer.soundTransform=MuteFunc;
MusicToggle.MusicButtonText.text = "turn on";
MusicToggle.addEventListener(MouseEvent.CLICK, UnMute);
MusicToggle.removeEventListener(MouseEvent.CLICK, Mute);
}
//UnMute
function UnMute(e:Event):void {
MuteFunc.volume=1;//Open The volume of the music.
SoundMixer.soundTransform=MuteFunc;
MusicToggle.MusicButtonText.text = "turn off";
MusicToggle.addEventListener(MouseEvent.CLICK, Mute);
MusicToggle.removeEventListener(MouseEvent.CLICK, UnMute);
}
public function playMusic(music:Sound):void{
trace ("init - music: ",music.url);
channel = music.play();
trace ("play - music: ",music.url);
channel.addEventListener(Event.SOUND_COMPLETE, repeat);
}
public function changeMusic(music):void {
trace ("change - music: ",music.url);
myTween = new TweenMax(channel, 1, {volume:0, delay:0.5});
myTween.addEventListener(TweenEvent.COMPLETE, function(evt:Event){switchMusic(evt, music)}, false, 0, true);
}
public function switchMusic(evt:Event, music:Sound) {
trace ("switch - music: ",music.url);
channel.stop();
channel = music.play();
channel.addEventListener(Event.SOUND_COMPLETE, repeat);
myTween.removeEventListener(TweenEvent.COMPLETE, function(evt:Event){switchMusic(evt, music)});
}
public function repeat(evt:Event) {
if(channel != null)
{
trace ("repeat - music: ",music.url);
channel.removeEventListener(Event.SOUND_COMPLETE, repeat);
playMusic(music);
}
}
}
}
In my timeline I change the audio with the following:
// Load the music...
var newMusic:Sound;
var newFile:URLRequest;
newMusic = new Sound();
newFile = new URLRequest("audio/track-button1.mp3");
newMusic.load(newFile);
MovieClip(root).changeMusic(newMusic);
Any ideas what might be causing the difficulties I am having? It’s probably obvious and I am missing it. I am new to packages, classes, etc. so I suspect that might be where the problem lies.
I included trace statements to track what the value of “music” is doing. It appears that when the function repeat() occurs, “music” always seems to default to the intro music “audio/track-intro.mp3”.
Any help would be greatly appreciated.
Glen