Solved

Flash buton to both start and stop music

Posted on 2010-11-07
7
589 Views
Last Modified: 2012-05-10
Hi,

I currently have a project with two buttons to play and stop a music track, however I wish to have just one button to both start and stop the music but without the problem of playing a new channel over another creating an echo effect, I think this will need a Boolean and an ‘if statement’ to check if the sound is on or off however I have not manage to get any working. N.B. I may be way over complicating things in my second lot of code from an online tutorial.

I have this code for the two buttons:

import flash.events.MouseEvent;
stop();
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest("Welcome_to_the_Jungle.mp3"));

Play1.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
}

Stop1.addEventListener(MouseEvent.CLICK, onClickStop);

function onClickStop(e:MouseEvent):void{
myChannel.stop();
}


This uses a Play1 and Stop1 button but i would like to have just one on/off button.


I have been trying to get this code to work from an online tutorial:

import flash.media.Sound;
import flash.media.SoundChannel;

var soundOn:Boolean = true; //music is ON when we start
var myMusic:TitleMusic = new TitleMusic(new URLRequest("Welcome_to_the_Jungle.mp3"));
var myChannel:SoundChannel = myMusic.play(0,1000); // endless loop, in effect
var myTransform:SoundTransform;

PlayStop.addEventListener(MouseEvent.CLICK,toggleSound);
PlayStop.buttonMode = true;
PlayStop.mouseChildren = false;


function toggleSound(e:MouseEvent)
{
    if(soundOn)
    {
        // turn sound off
        myTransform = new SoundTransform();
        myTransform.volume = 0; // silent
        myChannel.soundTransform = myTransform;
        soundOn = false;
        PlayStop.myButtonText.text = "click to turn sound ON";
    }
    else // sound is off
    {
        // turn sound on
        myTransform = new SoundTransform();
        myTransform.volume = 1; // full volume
        myChannel.soundTransform = myTransform;
        soundOn = true;
        PlayStop.myButtonText.text = "click to turn sound OFF";
    }
   
}

However I keep getting errors such as:

1046: Type was not found or was not a compile-time constant: TitleMusic.


Thank for any help

Karl
0
Comment
Question by:Karl01
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34081178
hI,

You could just create a  switch and control it yourself

var soundplay: String = 0;

then in the button function

if(soundplay==0){
play your sound, set btn txt, etc
end with
soundplay =1;

}
else{
stop your sound, set btn txt, etx
soundplay=0;
}
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34081184
sorry, surround all the strings with " "

var soundplay: String ="0";

then in the button function

if(soundplay=="0"){
play your sound, set btn txt, etc
end with
soundplay ="1";

}
else{
stop your sound, set btn txt, etx
soundplay="0";
}
0
 

Author Comment

by:Karl01
ID: 34083108
Hi gingermoleman,

I have tried your code as follows but I keep getting an error as show below, any ideas on what might be causing this, it does not seam to like the way I am trying to stop the music.


import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
var soundplay: String ="0";
mySound.load(new URLRequest("Welcome_to_the_Jungle.mp3"));

Play1.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void {

      if (soundplay=="0") {
            myChannel = mySound.play(lastPosition);
            soundplay ="1";

      } else {
            myChannel = mySound.stop();
            soundplay="0";
      }
}


Error:
Line 19 –
061: Call to a possibly undefined method stop through a reference with static type flash.media:Sound.

Thank you for your help.

Karl
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Karl01
ID: 34083160
Sorry its error code 1061 and line 19 is:

myChannel = mySound.stop();

Regards
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 500 total points
ID: 34083459
Hi Karl,

The vagaries of Flash, you stop your sound but to resume you play the channel!
You'll also need to update the current play position (lastPosition) in the pause code

function onClickPlay(e:MouseEvent):void {
      if (soundplay=="0") {
                  myChannel = mySound.play(lastPosition);
            soundplay ="1";
                  } else {
                  lastPosition=  myChannel.position;
            myChannel.stop();
                  soundplay="0";
                  }
}

0
 

Author Closing Comment

by:Karl01
ID: 34083956
That code work great, thank you very much.

Karl
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34084212
extra pointer, probably best to set soundplay to values like "playing" and "paused" instead of  "0" and "1". Just makes its easier to read at a later point
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now