Solved

Flash buton to both start and stop music

Posted on 2010-11-07
7
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

752 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