music playing over and over in Actionscript 3 - actionscript-3

Greeting,
I I developed a website using flash with Actionscript 3.
I included a music as a background for the website and the music will loaded when the site loaded.
but the problem that when I click buttons to move between pages(frames) then go and click button_01 the music will play again so I will have music playing more than one time in the background
and the sound_btn will not work any more so even I click sound_btn the music will not stop.
the code I'm using is listed down.
Please advice me what I should modify to not allow the music play more than one time in the background while moving from one page(frame) to another.
Regards,
stop();
//number that is redefined when the pause button is hit
var pausePoint:Number = 0.00;
//a true or false value that is used to check whether the sound is currently playing
var isPlaying:Boolean;
//think of the soundchannel as a speaker system and the sound as an mp3 player
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("music.mp3"));
//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
//mute_btn.addEventListener(MouseEvent.CLICK, clickStop);
sound_btn.addEventListener(MouseEvent.CLICK, clickPlayPause);
soundChannel = sound.play();
isPlaying = true;
myVideo.stop();
function clickPlayPause(evt:MouseEvent) {
if (isPlaying) {
pausePoint = soundChannel.position;
soundChannel.stop();
isPlaying = false;
} else {
soundChannel = sound.play(pausePoint);
isPlaying = true;
}
}
button_01.addEventListener(MouseEvent.CLICK, onClick1);
button_02.addEventListener(MouseEvent.CLICK, onClick2);
button_03.addEventListener(MouseEvent.CLICK, onClick3);
button_04.addEventListener(MouseEvent.CLICK, onClick4);
button_05.addEventListener(MouseEvent.CLICK, onClick5);
button_06.addEventListener(MouseEvent.CLICK, onClick6);
function onClick1(e:MouseEvent):void
{
gotoAndStop(1);
}
function onClick2(event:MouseEvent):void
{
gotoAndStop(2);
}
function onClick3(event:MouseEvent):void
{
gotoAndStop(3);
}
function onClick4(event:MouseEvent):void
{
gotoAndStop(4);
}
function onClick5(event:MouseEvent):void
{
gotoAndStop(5);
}
function onClick6(event:MouseEvent):void
{
gotoAndStop(6);
}

The problem is your code for the sound is initialized on the frame that you send the timeline to when clicking button_01. It will reinitialize each time you do that. Try initializing your sound code one frame earlier so that you do not land on that frame ever again once your page loads.
You also might find that wrapping your pages into movieclips, and using visible = true/false to change sections might be a better approach than advancing the timeline to change sections. That method would not result in the sound code reinitializing each time you changed sections. something like this:
function onClick1(e:MouseEvent):void
{
hideAll();
section_01.visible = true;
}
function onClick2(event:MouseEvent):void
{
hideAll();
section_02.visible = true;
}
function onClick3(event:MouseEvent):void
{
hideAll();
section_03.visible = true;
}
function onClick4(event:MouseEvent):void
{
hideAll();
section_04.visible = true;
}
function onClick5(event:MouseEvent):void
{
hideAll();
section_05.visible = true;
}
function onClick6(event:MouseEvent):void
{
hideAll();
section_06.visible = true;
}
function hideAll():void
{
section_01.visible = false;
section_02.visible = false;
section_03.visible = false;
section_04.visible = false;
section_05.visible = false;
section_06.visible = false;
}
If you wanted tweened transitions you could use a tweening class to handle the transitions by tweening the current section out in the hide function and then tweening the next section in in its respective onCLick function.

Related

How to stop multiple Movieclips at a time by play/pause button in AS3

I have 4 Movieclips in timeline.these movieclips have inner animation. they are classically tween in timeline. I have to stop and play these four movieclips at a time by play/pause button. Noted that, I have used the following code for that purposes.
It can stop only one MovieClip which is in the upper layer, and the rest 3 Movieclips can't be stopped by play/ pause button. (I have tried all movie clips are in same instance name and also tried with different instance).
Here is my code:
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
first_sl.play();
this.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
first_sl.stop();
this.stop();
}
You can try like when you are stopping main timeline with stage.stop(); also use mc.stop(); ect. and when u play them use stage.play(); also mc.play();
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
mc.play();
mc2.play();// other 2 mc's too
stage.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
mc.stop();
mc2.stop(); // other mc's
stage.stop();
}
And if you are using last position for timeline also use it for mc's so it will be sync..
hope this on help tho :)
To stop the main timeline you can use stop() or MovieClip(root).stop() and to play it again, you can use play() or MovieClip(root).play() :
var playing: Boolean = false;
btn_play.addEventListener(MouseEvent.CLICK, onPlayClick);
btn_pause.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void
{
if (!playing) {
play(); // you can also use : MovieClip(root).play();
mc1.play();
mc2.play();
mc3.play();
mc4.play();
playing = true;
}
}
function onPauseClick(event: MouseEvent):void
{
if (playing) {
stop(); // you can also use : MovieClip(root).stop();
mc1.stop();
mc2.stop();
mc3.stop();
mc4.stop();
playing = false;
}
}
Hope that can help.

Using a variable to allow only one video to play at a time

I recently created a project that has four buttons. Each button adds an FLV player to the stage and plays a video. During testing, I noticed that clicking another button before the first function is finished loading the movie, TWO movies will play.
Is there a more efficient way of handling this? I feel like there is a lot of redundant code here, is there a better way to do the following:
Button One Pressed
Load Video One
Ignore all other event listeners until the movie ends, or the user dismisses the video (Clicks to dismiss)
I initially though it would be a good idea to create a two functions, one that removes all event listeners, and another that adds all the event listeners. When a button is pressed, before it loads the FLVPlayback, remove all event listeners. When the movie is COMPLETE, add all the listeners back. That seems taxing, and very inefficient.
I wrote the following code as an example of what I'm trying to do. (This outputs the same string "One Can play" every time though).
var canLaunchOne:Boolean;
var canLaunchTwo:Boolean;
var canLaunchThree:Boolean;
var canLaunchFour:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
trace(canLaunchTwo);
function playMovieOne(event:Event):void {
canLaunchOne = true;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = false;
trace(canLaunchOne);
playTheRightVideo();
}
function playMovieTwo(event:Event):void {
canLaunchOne = false;
canLaunchTwo = true;
canLaunchThree = false;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieThree(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = true;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieFour(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = true;
playTheRightVideo();
}
function playTheRightVideo():void {
if(canLaunchOne = true){
trace("One Can Play"); // do all that video stuff for video one
} else if(canLaunchTwo = true){
trace("Two Can Play"); // do all that video stuff for video one
} else if(canLaunchThree = true){
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("Four Can Play");
}
}
I tried this code, and I got it "working", but every function will always set canLaunchMovie to true...
import flash.events.Event;
var canLaunchMovie:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
function playMovieOne(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("One Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie one) Dave");
}
}
function playMovieTwo(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Two Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie two) Dave");
}
}
function playMovieThree(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie three) Dave");
}
}
function playMovieFour(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Four Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie four) Dave");
}
}
You don't need to have more than one flvplayback.
You only need to change the source of your flvplayback.

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
var sTransform1:SoundTransform = new SoundTransform(1,0);
var Mute1:Boolean = true;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
function toggleMuteBtn1(event:Event) {
if(Mute1 === false) {
Mute1 = true;
setMute1(0);
} else {
Mute1 = false;
setMute1(1);
}
}
From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.
Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Start/stop sound
function startOrStop()
{
if (Mute1 === false)
{
myChannel.stop();
setMute1(0);
}
else
{
setMute1(1);
myChannel = mySound.play();
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
startOrStop()
}
In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.
This example mutes and unmutes the sound. The sound is't stopped, just muted.
Also here the sound must be assigned in the code, and not to the frame.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Toggle mute on/off
function toggleMute()
{
if (Mute1 === false)
{
setMute1(0);
}
else
{
setMute1(1);
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
// if not playing, the sound
if (myChannel.position != 0) {
} else {
myChannel = mySound.play();
}
toggleMute();
}

as3: Mute button and volume slider on one sound channel

I have truly exhausted all my knowledge on this problem so I hope that my peers will be able to help me?
I am building a audio mixer that has five tracks with a volume slider and mute button per track. The reason for a mute button as opposed to a start/stop button per track is so that all the samples will be in sync regardless of when a sample is introduced.
The app has global start, stop and pause buttons which all function normally but I cannot get the volume slider and mute button to work in tandem on an individual sound channel.
The volume slider and the mute button will both work if I comment out the other function but when both are in play then only the volume slider works.
I'm guessing that there is a conflict because I have two separate variables using the soundTransform object/class but maybe you can shed some light on this conundrum?
Here is my code for one track... Any help appricated.
var mySound1:Sound1 = new Sound1();
var myChannel1:SoundChannel = new SoundChannel();
var volumeAdjust1:SoundTransform = new SoundTransform();
volumeAdjust1.volume = 0;
mute_btn1.stop();
mute_btn1.addEventListener(MouseEvent.CLICK,togglemute_btn1);
var Mute1:Boolean = false;
function togglemute_btn1(event:MouseEvent)
{
if (Mute1)
{
mute_btn1.gotoAndStop(1);
volumeAdjust1.volume = 1;
myChannel1.soundTransform = volumeAdjust1;
Mute1 = false;
}
else
{
mute_btn1.gotoAndStop(2)
volumeAdjust1.volume = 0;
myChannel1.soundTransform = volumeAdjust1;
Mute1 = true;
}
}
/*if the section below is commented out then the mute_btn1 works fine
otherwise the volume slider functions are dominent*/
var dragging1:Boolean = false;
var mySliderLength1:uint = 300;
var boundingBox1:Rectangle = new Rectangle(0,0,0,mySliderLength1);
slider_mc1.knob_mc1.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob1);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob1);
slider_mc1.knob_mc1.buttonMode = true;
function dragKnob1(myEvent:Event):void
{
slider_mc1.knob_mc1.startDrag(false, boundingBox1);
dragging1 = true;
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
}
function releaseKnob1(myEvent:Event):void
{
if (dragging1)
{
slider_mc1.knob_mc1.stopDrag();
dragging1 = false;
}
}
function adjustVolume1(myEvent:Event):void
{
var myVolume1:Number = slider_mc1.knob_mc1.y / mySliderLength1;
var myTransform1:SoundTransform = new SoundTransform(myVolume1);
if (myChannel1!=null)
{
myChannel1.soundTransform = myTransform1;
}
}
You should check your Mute1 variable in that listener of yours, and if muted, then volume=0, otherwise volume is calculated. And indeed, do remove your enter frame listener at the point of stopDrag() call.
function dragKnob1(myEvent:Event):void
{
slider_mc1.knob_mc1.startDrag(false, boundingBox1);
dragging1 = true;
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
}
function releaseKnob1(myEvent:Event):void
{
if (dragging1)
{
slider_mc1.knob_mc1.stopDrag();
dragging1 = false;
slider_mc1.knob_mc1.removeEventListener(Event.ENTER_FRAME, adjustVolume1);
// ^ this line added
}
}
function adjustVolume1(myEvent:Event):void
{
if (Mute1) return;
// ^ and this line added
var myVolume1:Number = slider_mc1.knob_mc1.y / mySliderLength1;
var myTransform1:SoundTransform = new SoundTransform(myVolume1);
if (myChannel1!=null)
{
myChannel1.soundTransform = myTransform1;
}
}
I believe your issue is you keep adding the Enter_Frame listener every time the mouse is clicked but it never gets removed. So even after you let go of the knob the adjustVolume1 function is still getting called (which messes up anything the mute function call is doing on the frame after the mute toggle function is called).
So how I think I would deal with this given the current state is move the Enter_Frame listener addition outside of the dragKnob function and in the adjustVolume1 handler just check if dragging1 is true otherwise just return.
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
function dragKnob1(myEvent:Event):void
{
...
}
function adjustVolume1(myEvent:Event):void
{
if(!dragging1)
return;
...
}

AS3: How do I play a sound when the currentframe ==?

My code below unfortunately results in the sound repeating at the framerate of the movie. I just need a simple "play a sound at a specific frame" function but can't figure this one out. Thanks for your help.
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
This seems about right. Are you sure the naturepage.ocean_btns movieclip is not stopped at the second frame or looping very few frames over and over?
Another option is to use a flag to see if you already played the sound:
var alreadyPlayedSound:Boolean = false;
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2 && !alreadyPlayedSound)
{
alreadyPlayedSound = true;
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
Or to remove the event listener once you played the sound:
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
removeEventListener(Event.ENTER_FRAME,soundFunction);
}
}