Solved

Flash buton to both start and stop music

Posted on 2010-11-07
7
588 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Karl01
Comment Utility
Sorry its error code 1061 and line 19 is:

myChannel = mySound.stop();

Regards
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 500 total points
Comment Utility
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
Comment Utility
That code work great, thank you very much.

Karl
0
 
LVL 5

Expert Comment

by:gingermoleman
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to count occurrences of each item in an array.
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

772 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

9 Experts available now in Live!

Get 1:1 Help Now