How can you stop a sound from playing in as3? - actionscript-3

I have this code in the second frame to play a song, and it works fine. The class of the sound in my library being "MySound".
var snd:MySound = new MySound
snd.play();
Now I need the song to stop playing from a latter frame.

You have to use SoundChannel class for this:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("myFavSong.mp3"));
myChannel = mySound.play();
// ...
myChannel.stop();
Reference: http://www.republicofcode.com/tutorials/flash/as3sound/ (Section 4 - Stopping a Sound)

//*This code for playing the sound after import to the library*
var mySound:Sound = new MenuMusic();
var myChannel:SoundChannel = new SoundChannel();
myChannel = mySound.play();
//*This code for the nextframe button*
btn_easy.addEventListener(MouseEvent.MOUSE_DOWN, btneasy);
function btneasy(event:MouseEvent):void
{
gotoAndPlay(62);
myChannel.stop();
}
Reminder :
You don't have to drag the sound file to the timeline . Just paste the
code to the designated Action Script of the button.
var mySound:Sound = new MenuMusic();
'MenuMusic' can be changed what you want. Just Right click the sound
file in the library then select Properties . Click ActionScript Tab and
check 'Export for ActionScript' and you can now changed the name in
'Class' Input Box..
It works so fine to me :)
Hope it solved the problem ! :D

Related

How can I stop music from playing multiple times when going back to the frame it starts from in as3?

I have a music track that starts on the first frame and plays throughout the entire application. When I go back to the first frame, the current music is still playing but the song starts again over the top of it. How can I make it so it only plays through once even after going back to that frame?
My code:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("Stellardrone - Billions And Billions.mp3"));
myChannel = mySound.play();
SoundMixer.soundTransform = new SoundTransform(0.4);
You could just check to see if the sound (or sound channel) has been created yet or not:
var mySound:Sound;
var myChannel:SoundChannel;
if(!mySound){
mySound = new Sound();
mySound.load(new URLRequest("Stellardrone - Billions And Billions.mp3"));
myChannel = mySound.play();
SoundMixer.soundTransform = new SoundTransform(0.4);
}

How do you keep repeating a song in AS3 FlashCS6?

I would like to know how do you repeat a song again and again in Flash CS6 using AS3?
currently i have this code:
var sound:Sound = new Sound(new URLRequest("YMCA.mp3"))
var soundChannel:SoundChannel = sound.play();
How do I repeat this song when it finishes again and again?
Thanks alot!
Add an event listener to your soundChannel:
soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
function soundCompleteHander(e:Event):void {
sound.play();
}
this will make the sound to loop many times with a flawless and gap-less playback
var sound:Sound = new Sound(new URLRequest("YMCA.mp3"))
var soundChannel:SoundChannel = sound.play(0,999999999);

embed sound so that when publish we do not have to include it in the same folder as the swf file

i have a flash animation with sound that have play,pause,stop,go to start and go to end button.
when i publish it i have to include the mp3 file along with the swf file.
how do i do to get the swf file alone in order to play the whole thing?
i am using flash cs 3 and actionscript 3.0
here is my codes:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
var soundIsPlaying:Boolean = true;
mySound.load(new URLRequest("saloma.mp3"));
myChannel = mySound.play();
all those buttons will go to functions,
go.addEventListener(MouseEvent.CLICK,govid);
function govid(event:MouseEvent):void{
play();
if(!soundIsPlaying){
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}
i am also using scenes to navigate them,
gte.addEventListener(MouseEvent.CLICK,gotoend);
function gotoend(event:MouseEvent):void{
gotoAndStop(1,"ending");
}
thank you :)
I would have written the answer, but this has been answered already...
First, in your library, set the class linkage of a sound file by right clicking, selecting properties and editing the Class field in the Linkage section. In this example it will be Class:FogHorn
import flash.utils.getDefinitionByName;
var SoundClass:Class = getDefinitionByName("FogHorn") as Class;
var newSound:Sound = new SoundClass();
newSound.play()
source: #Allan in Actionscript 3: playing sound from library with name from string
import your mp3 file to your library
select your mp3 file in the library and right click on it
a pop-up window will appear (click on advanced if it is not yet clicked)
under the actionscript linkage, you will see class field, type "sound1" for example (no 5. quotations, this can be any other name aside from sound1)
instead of var mySound:Sound = new Sound(); this code, type
var mySound:Sound = new sound1(); //sound1 is the name of your linkage/class
You don't need this codes anymore. You need to omit your myChannel variable.
mySound.load(new URLRequest("saloma.mp3"));
myChannel = mySound.play();
To play the sound, simply type
mySound.play();

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();
}

How i know if a Sound object is playing?

In AS3 i've created a code that load a sound and execute it in streaming.
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("surrender.mp3"));
myChannel = mySound.play();
Considering that the mp3 file is placed on the server, i need to trace if it's already begun or still receiving initial data from the server. Once it's effectively started i've to call a function that initialize some things.
the problem is, how i can trace if a sound or channel object "is playing" ?
You can check this by listening for the different Events the Sound object dispatches as well as its properties like Sound.isBuffering
Refer to the Sound manpage
you could wait for the mp3 to be loaded completely
var myChannel:SoundChannel;
var soundFactory:Sound = new Sound();
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
soundFactory.load(request);
// ...
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
myChannel = soundFactory.play();
}
or you can use a Timer and check the myChannel.position periodically - if it doesn't change the sound stopped playing...