Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Flash buton to both start and stop music

Posted on 2010-11-07
7
Medium Priority
?
597 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
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 how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses

722 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