Toggle Mute sound in Actionscript 3 - actionscript-3

I have several tracks of audio that are in sinc. I would like to have one "TitleMusic" ON from the start, And allow the user to toggle ON and off the other tracks. My code As it stands has the "TitleMusic" playing from the start with all the other tracks playing too. I need to switch "track8" and all the other tracks (not showing) around so they are off at the start.This took me a long time to get to this point, I just need some help turning it around. Thanks
import flash.media.Sound;
import flash.media.SoundChannel;
var soundOn:Boolean = true;//This music is ON when we start
var myMusic:TitleMusic = new TitleMusic();
var myChannel1:SoundChannel = myMusic.play(0,1000);//endless loop, in effect
var soundOn3:Boolean = true; //music is ON when we start
var myMusic3:track8 = new track8();
var myChannel3:SoundChannel = myMusic3.play(0,1000); // endless loop, in effect
var myTransform3:SoundTransform;
mySoundButton3.addEventListener(MouseEvent.CLICK,toggleSound3);
mySoundButton3.buttonMode = true;
mySoundButton3.mouseChildren = false;
function toggleSound3(e:MouseEvent)
{
if(soundOn3)
{
// turn sound off
myTransform3 = new SoundTransform();
myTransform3.volume = 0; // silent
myChannel3.soundTransform = myTransform3;
soundOn3 = false;
mySoundButton3.myButtonText.text = "click to turn sound ON";
}
else // sound is off
{
// turn sound on
myTransform3 = new SoundTransform();
myTransform3.volume = 1; // full volume
myChannel3.soundTransform = myTransform3;
soundOn3 = true;
mySoundButton3.myButtonText.text = "click to turn sound OFF";
}
}

Couldn't you just put this line right after mySoundButton3.mouseChildren = false;:
toggleSound3(null);
Or, to be more efficient with memory, you could do this:
Take this line:
var myChannel3:SoundChannel = myMusic3.play(0,1000);
and change it to:
var myChannel3:SoundChannel;
This makes it so you're not actually starting the sound right away, but just creating the pointer for it (var)
Then, in your turn on block right after // turn sound on:
if(!myChannel13){
myChannel3 = myMusic3.play(0,1000);
}
This checks to see if you've started the sound yet, if not, it creates/starts the sound
You'll also want to change this line in your sound off block:
myChannel3.soundTransform = myTransform3;
to this
if(myChannel13){
myChannel3.soundTransform = myTransform3;
}
That way, if the off button is clicked before the on button, it won't throw an error.

Related

CROSSFADE SOUND AS 3.0

I'm an AS 3.0 project where I'm getting into an array sounds to compose a particular set of phrases. The problem is that it sounds too much shock and wanted to make a crossfade effect to better attach a word to each other.
My problem is that I can not join them because every sound played one after another, is there any way to merge that can reach the end of a sound with the beginning of the next?
Thank you very much.
The code I'm working with is something like this:
for (iii = 0; iii < numpalabras; iii ++)
{
if (abuscar2 = abuscarArray[iii])
{
vocaliza(abuscar2, iii);
}
}
iii = 0;
localSound = lossonidosArray[iii];
var soundTrans:SoundTransform = new SoundTransform;
soundTrans=SoundMixer.soundTransform;
soundTrans.volume=1;
soundTrans.pan=0;
elcanal.soundTransform = soundTrans;
elcanal = localSound.play(85, 0, soundTrans);
elcanal.addEventListener(Event.SOUND_COMPLETE, locutapalabra);
}
function locutapalabra(event:Event)
{
if (iii < (ii))
{
iii=iii+1;
localSound = lossonidosArray[iii];
var soundTrans:SoundTransform = new SoundTransform;
soundTrans=SoundMixer.soundTransform;
soundTrans.volume=1;
soundTrans.pan=0;
elcanal.soundTransform = soundTrans;
elcanal = localSound.play(85, 0, soundTrans);
elcanal.addEventListener(Event.SOUND_COMPLETE, locutapalabra);
}
function vocaliza(abuscar2, iii)
{
if (datosXML.palabras.(palabra == abuscar2).palabra == abuscar2)
{
ii++;
elfic = "mp3/" + datosXML.palabras.(palabra == abuscar2).fichero;
var elsonido :Sound = new Sound();
elsonido.addEventListener(IOErrorEvent.IO_ERROR, errorprogreso);
var laurl:URLRequest = new URLRequest(elfic);
elsonido.load(laurl);
lossonidosArray[ii] = elsonido;
}
}
I am new to AS 3.0 programming and I do not get clear my code to make the words come together with each other, because I get to build such phrases of several words.
Thank you very much.
For cross-fading, you can use a tween library (like greensock) to tween the volume of the sound channel:
var sound:Sound = new Sound(...);
// start volume at 0
var soundChannel = sound.play(0, 0, new SoundTransform(0));
// tween volume to 1
TweenMax.to(soundChannel, 1, { volume: 1 } );
// half a second before the sound is complete, tween volume to 0
TweenMax.to(soundChannel, .5, {volume: 0, delay:(sound.length/1000)-.5});
var timer:Timer = new Timer(3000) //set to how long to wait
timer.addEventListener(TimerEvent.TIMER, nextSound);
function nextSound(e:Event):void
{
listenForNextSound(); //this is where you play your next sound
}
timer is a timer variable, and when 3 seconds pass (or however long you want) the next sound will start playing.
To make your timer variable a bit more accurate, you can make it the duration of the sound by listening for the COMPLETE Event:
yourTimer.addEventListener(Event.COMPLETE, function() {
timer.delay = yourTimer.length - howMuchTimeBefore;
});

Text stops showing unless not playing

So i finally got my flash application to work and have it running on a website but after a while the dynamic text stops showing up in the fields. The file is set to loop every 5 seconds and it is supposed to update to show the staff on air and what is currently playing. Link to the website is http://mischieffm.com/
var xmlData:XML = new XML();
var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
var theURL_ur:URLRequest = new URLRequest ("/stream/shout.xml?rnd=" + Math.random());
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
show_txt.text = xmlData.SERVERTITLE;
song_txt.text = xmlData.SONGTITLE;
}
This is all done in cs6 flash pro and actionscript 3
Try this:
function fileLoaded(e:Event):void
{
loader_ul.removeEventListener("complete", fileLoaded);
xmlData = new XML(loader_ul.data);
if (xmlData.SONGTITLE && xmlData.SERVERTITLE)
{
show_txt.text = xmlData.SERVERTITLE;
song_txt.text = xmlData.SONGTITLE;
}
else
{
show_txt.text = "Untitled artist";
song_txt.text = "Untitled song";
}
}
Note: In order to prevent memory leaks, always call removeEventListener("someEvent", someMethod); after your event gets dispatched. There are some exclusions, but generally you should do it all the time.

as3: Mute button and volume slider on one sound channel

I have truly exhausted all my knowledge on this problem so I hope that my peers will be able to help me?
I am building a audio mixer that has five tracks with a volume slider and mute button per track. The reason for a mute button as opposed to a start/stop button per track is so that all the samples will be in sync regardless of when a sample is introduced.
The app has global start, stop and pause buttons which all function normally but I cannot get the volume slider and mute button to work in tandem on an individual sound channel.
The volume slider and the mute button will both work if I comment out the other function but when both are in play then only the volume slider works.
I'm guessing that there is a conflict because I have two separate variables using the soundTransform object/class but maybe you can shed some light on this conundrum?
Here is my code for one track... Any help appricated.
var mySound1:Sound1 = new Sound1();
var myChannel1:SoundChannel = new SoundChannel();
var volumeAdjust1:SoundTransform = new SoundTransform();
volumeAdjust1.volume = 0;
mute_btn1.stop();
mute_btn1.addEventListener(MouseEvent.CLICK,togglemute_btn1);
var Mute1:Boolean = false;
function togglemute_btn1(event:MouseEvent)
{
if (Mute1)
{
mute_btn1.gotoAndStop(1);
volumeAdjust1.volume = 1;
myChannel1.soundTransform = volumeAdjust1;
Mute1 = false;
}
else
{
mute_btn1.gotoAndStop(2)
volumeAdjust1.volume = 0;
myChannel1.soundTransform = volumeAdjust1;
Mute1 = true;
}
}
/*if the section below is commented out then the mute_btn1 works fine
otherwise the volume slider functions are dominent*/
var dragging1:Boolean = false;
var mySliderLength1:uint = 300;
var boundingBox1:Rectangle = new Rectangle(0,0,0,mySliderLength1);
slider_mc1.knob_mc1.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob1);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob1);
slider_mc1.knob_mc1.buttonMode = true;
function dragKnob1(myEvent:Event):void
{
slider_mc1.knob_mc1.startDrag(false, boundingBox1);
dragging1 = true;
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
}
function releaseKnob1(myEvent:Event):void
{
if (dragging1)
{
slider_mc1.knob_mc1.stopDrag();
dragging1 = false;
}
}
function adjustVolume1(myEvent:Event):void
{
var myVolume1:Number = slider_mc1.knob_mc1.y / mySliderLength1;
var myTransform1:SoundTransform = new SoundTransform(myVolume1);
if (myChannel1!=null)
{
myChannel1.soundTransform = myTransform1;
}
}
You should check your Mute1 variable in that listener of yours, and if muted, then volume=0, otherwise volume is calculated. And indeed, do remove your enter frame listener at the point of stopDrag() call.
function dragKnob1(myEvent:Event):void
{
slider_mc1.knob_mc1.startDrag(false, boundingBox1);
dragging1 = true;
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
}
function releaseKnob1(myEvent:Event):void
{
if (dragging1)
{
slider_mc1.knob_mc1.stopDrag();
dragging1 = false;
slider_mc1.knob_mc1.removeEventListener(Event.ENTER_FRAME, adjustVolume1);
// ^ this line added
}
}
function adjustVolume1(myEvent:Event):void
{
if (Mute1) return;
// ^ and this line added
var myVolume1:Number = slider_mc1.knob_mc1.y / mySliderLength1;
var myTransform1:SoundTransform = new SoundTransform(myVolume1);
if (myChannel1!=null)
{
myChannel1.soundTransform = myTransform1;
}
}
I believe your issue is you keep adding the Enter_Frame listener every time the mouse is clicked but it never gets removed. So even after you let go of the knob the adjustVolume1 function is still getting called (which messes up anything the mute function call is doing on the frame after the mute toggle function is called).
So how I think I would deal with this given the current state is move the Enter_Frame listener addition outside of the dragKnob function and in the adjustVolume1 handler just check if dragging1 is true otherwise just return.
slider_mc1.knob_mc1.addEventListener(Event.ENTER_FRAME, adjustVolume1);
function dragKnob1(myEvent:Event):void
{
...
}
function adjustVolume1(myEvent:Event):void
{
if(!dragging1)
return;
...
}

actionscript 3.0 function calling using button click

im creating a simple flash playlist using buttons, in my stage i have 4 buttons which is button for song1,song2, stop and play. i have a working code for this one, but i decided to revise it because my previous code is like, per song they have each stop and play button,so i created this one to have a dynamic stop and play, i created a function for each song, the function will change the filename of the song to be loaded,
heres the catch, so i first pick a song, (either song1 or song2) then i click stop, then when i select a new song this error appears
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at playlist_fla::MainTimeline/songSelect1()
i think its not calling the second function because i cant see the trace i put inside it,anyway heres my code, sorry for the long post,
THANKS IN ADVANCE
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songSelect1);
function songSelect1(e:MouseEvent):void{
song = "<filenameofthe1stsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(1);
}
song2.addEventListener(MouseEvent.CLICK,songSelect2);
function songSelect2(e:MouseEvent):void{
song = "<filenameofthe2ndsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(2);
}
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
function onClickStop(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
}
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}
Correction:
According to Adobe:
Once load() is called on a Sound object, you can't later load a
different sound file into that Sound object. To load a different sound
file, create a new Sound object.
So, creating a new sound object would be the only way to fix it.
- - - Original Post - - -
If you enable debugging in your flash settings, it'd be easier to determine exactly what line is causing issues. That said, your code doesn't look incorrect apart from defining your sound transform twice. Try this:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songHandler);
song2.addEventListener(MouseEvent.CLICK,songHandler);
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function songHandler(e:MouseEvent):void {
switch (e.currentTarget.name) {
case "song1":
songSelect("<filenameofthe1stsong>")
break;
case "song2":
songSelect("<filenameofthe2ndsong>")
break;
}
}
function songSelect(songPath:String):void {
mySound.load(new URLRequest(songPath));
myChannel.soundTransform.volume = 0.5;
lastPosition = 0;
trace("Loading " + songPath);
}
function onClickStop(e:MouseEvent):void {
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void {
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}

AS3 play sounds in an array in a sequence

This is being programmed in Flash CS5.5:
I want to push a button, and play through the entire array one sound at a time. When the first sound stops, the second begins, etc. all the way until the last sound plays. When the last sound finishes, all sound should stop, and if you push the play button again, it should start over at the beginning, and play through all sounds again.
Currently, to advance to the next sound, you have to push the button again. I'm thinking the SOUND_COMPLETE needs to be used... I'm just not sure how, hence the empty function. I only want to have to push play one time to hear the entire array in a sequence. Any ideas?
var count;
var songList:Array = new Array("test1.mp3","test2.mp3","test3.mp3");
count = songList.length;
myTI.text = count;
var currentSongId:Number = 0;
playBtn.addEventListener(MouseEvent.CLICK, playSound);
function playSound(e:MouseEvent):void{
if(currentSongId < songList.length)
{
var mySoundURL:URLRequest = new URLRequest(songList[currentSongId]);
var mySound:Sound = new Sound();
mySound.load(mySoundURL);
var mySoundChannel:SoundChannel = new SoundChannel();
mySoundChannel = mySound.play();
currentSongId++;
mySoundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete)
}
if(currentSongId == songList.length)
{
currentSongId = 0;
}
}
function handleSoundComplete(event:Event){
}
You should use functions to modulate what you do, this will make your code more readable.
private Array songList = new Array("test1.mp3", "test2.mp3");
public function onPlayBtnPressed(){
currentSongIndex = 0;
PlaySongFromIndex(currentSongIndex);
}
public function PlaySongFromIndex(songIndex:int){
//do what ever here to simply play a song.
var song:Sound = new Sound(songList[songIndex]).Play()
//Addevent listener so you know when the song is complete
song.addEventListener(Event.Complete, songFinished);
currentSongIndex++;
}
public function songFinished(e:Event){
//check if all the songs where played, if so resets the song index back to the start.
if(currentSongIndex < listSong.Length){
PlaySongFromIndex(currentSongIndex);
} else {
currentSongIndex=0;
}
}
This wont compile its just to show an exemple, hope this helps.