How to unmute the sounds in as3? - actionscript-3

Sorry for my English..I have a code for two buttons to mute and unmute sounds..when I click to mute the sounds, it works but when I click to the other button to make the sounds to play it doesn't worked..any help?
import flash.media.SoundMixer;
speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound);
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound);
var channel2:SoundChannel = new SoundChannel();
var clicktoPlay:Boolean = true;
function speaker2sound(e:MouseEvent):void{
if (clicktoPlay==true){
var snd2:Sound = new Sound;
snd2.load(new URLRequest("shakeup.wav"));
clicktoPlay=false;
channel2 = snd2.play();
speakerb1.visible=true;
speakerb2.visible=false;
SoundMixer.soundTransform = new SoundTransform(1);
}
if (clicktoPlay==false){
clicktoPlay=true;
speakerb2.visible=true;
speakerb1.visible=false;
SoundMixer.soundTransform = new SoundTransform(0);
}
clicktoPlay =!clicktoPlay;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}

I got your code working with a few small changes:
speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound);
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound);
var channel2:SoundChannel = new SoundChannel();
var clicktoPlay:Boolean = true;
var snd2:Sound;
function speaker2sound(e:MouseEvent):void{
if (clicktoPlay==true){
if(!snd2){
snd2 = new Sound();
snd2.load(new URLRequest("Kalimba.mp3"));
channel2 = snd2.play();
}
speakerb1.visible=true;
speakerb2.visible=false;
SoundMixer.soundTransform = new SoundTransform(1);
}
else {
speakerb2.visible=true;
speakerb1.visible=false;
SoundMixer.soundTransform = new SoundTransform(0);
}
clicktoPlay =!clicktoPlay;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}

As Marcela says, try taking out the line "clicktoPlay =!clicktoPlay;" and see if it works.
The way your code is now, until "soundfin" is called, clicktoPlay will remain true no matter how often speaker2sound is called.

Related

Can't restart music as3

I have a button that turns on and off the music. When I turn it off it works, but when I try to play again the music it doesn't work! help please
var sound=true;
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("Track.mp3"));
myChannel = mySound.play();
function changeSound(event:MouseEvent):void {
if(sound==true){
myChannel.stop();
sound=false;
}
if(sound==false){
myChannel.reset();
sound=true;
}
}
SoundChannel doesn't have a reset method. Call play instead of reset.
if(sound==false){
myChannel = mySound.play();
sound=true;
}
Also, you should place else between two if.
if(sound==true){
myChannel.stop();
sound=false;
}
else if(sound==false){
myChannel = mySound.play();
sound=true;
}

Volume control after loop as3

I have a problem with my code. The code works great with one exception, if I have pressed the mute-button and the animation loops, it's always back to volume = 1. How can it loop without always going back to volume = 1?
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
volumeAdjust.volume = 1;
mySound.load(songURL);
channel1.soundTransform = volumeAdjust;
channel1 = mySound.play();
function muteLyd(e:MouseEvent):void {
if(volumeAdjust.volume == 1){
volumeAdjust.volume = 0;
}
else {
volumeAdjust.volume = 1;
}
channel1.soundTransform = volumeAdjust;
}
Just found out this code solves the problem:
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
unmute1.addEventListener(MouseEvent.CLICK, unMuteLyd);
mySound.load(songURL);
channel1 = mySound.play();
function muteLyd(evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(0);
unmute1.visible = true;
mute1.visible = false;
}
function unMuteLyd (evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(1);
unmute1.visible = false;
mute1.visible = true;
}

Refresh stream content

When pres play it connects to stream and its all ok!
When I click pause btn it stops sound, but the problem
is when you click play btn after them it starts playing stream from the same place where you stop it, doing this all time nothing happens it always start stream from the same place when do it 1st time...
play_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
pause_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
spiner_mc.visible=true;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
spiner_mc.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}
everything was easy!
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}
The s:Sound, req, context and s.load cant be placed outside and thats all...

AS3 - my sound player seems to work well but the sound is not there

I've been doing a soundplayer for my assignment.Came across some issues. No compile error neither runtime error is appearing and my sound player functions well but I can't hear the song playing.
var mySound:Sound = new Sound ();
var myURL:URLRequest = new URLRequest ("playList.xml");
var mySoundChannel:SoundChannel = new SoundChannel;
var playing:Boolean = true;
var resumeTime:Number = 0;
var loadedXML:XML;
var mySongs:XMLList;
var myTotal:Number;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(myURL);
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void {
loadedXML = XML(e.target.data);
mySongs = loadedXML.SONG;
myTotal = mySongs.length();
}
btnPlayPause.buttonMode =true;
btnStop.buttonMode =true;
btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
function stopSound (f:MouseEvent):void {
mySoundChannel.stop();
}
Help is appreciated very much.
It seems like you trying to play sound only if it already playing, while stoping sound if it is NOT playing:
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
I see this code like this:
function playSound(){
if(playing){
mySound.play();
} else {
mySoundChannel.stop();
}
}
Maybe this is the root of problem

actionscript 3 external swf loads above the close button

I'm an as3 newbie, i'm loading external swf files by clicking different buttons but using only one function which works fine.
//main
mainCloseBtn.visible = false;
movieloaderbg.visible = false;
M1L1Btn1.visible = false;
//Load activities
var Xpos:Number = 0;
var Ypos:Number = 0;
var swf:MovieClip;
var loader:Loader = new Loader();
loader.x = Xpos;
loader.y = Ypos;
addChild(loader);
// Btns Universal function
function btnClick(event:MouseEvent):void {
removeChild(loader);
var newSWFRequest:URLRequest = new URLRequest("activities/" + event.target.name + ".swf");
loader.load(newSWFRequest);
loader.x = Xpos;
loader.y = Ypos;
addChildAt(loader,3);
mainCloseBtn.visible = true;
}
// Btn listeners
wrong.addEventListener(MouseEvent.CLICK, btnClick);
// Back btn
backBtn.addEventListener(MouseEvent.CLICK, backBtnClick);
function backBtnClick(evt:MouseEvent):void {
gotoAndStop("M1Lessons");
}
// Video Button
videoBtn.addEventListener(MouseEvent.CLICK, videoBtnClick);
function videoBtnClick(evt:MouseEvent):void {
mainCloseBtn.visible = true;
M1L1Btn1.visible = true;
M1L1Btn1.gotoAndPlay(2);
}
//Main Close Button
mainCloseBtn.addEventListener(MouseEvent.CLICK, mainCloseBtnClick);
function mainCloseBtnClick(evt:MouseEvent):void {
mainCloseBtn.visible = false;
loader.unload();
M1L1Btn1.visible = false;
M1L1Btn1.stop();
}
I'm trying to unload the swfs by clicking on the close button, but the problem is when the swf is loaded i can no longer see the close btn, I've tried to use swap depth but isn't working.
I would really appreciate it if someone could help me!
Thanks
try this:
replace
addChild(loader);
with
addChildAt(loader,0);