gotoAndPlay after sound ends as3 - actionscript-3

I would like my timeline to advance to a certain labeled frame after a sound completes. Here is the code that I have so far:
var n1Channel:SoundChannel = new narratorAlphabetSounds();
n1Channel.addEventListener(Event.SOUND_COMPLETE, audioComplete);
function audioComplete(e:Event):void
{
gotoAndPlay("step2");
}
The sound fails to load and errors occure when trying to test. Any suggestions? Thanks!

You got the implicit coercion error because your narratorAlphabetSounds object is a Sound object and not a SoundChannel one .. then to play your sound and detect when it has finished playing, you should use a Sound and a SoundChannel objects, like this :
var sound:Sound = new narratorAlphabetSounds();
var sound_channel:SoundChannel = sound.play();
sound_channel.addEventListener(Event.SOUND_COMPLETE, audioComplete);
function audioComplete(e:Event):void
{
trace('audio complete');
}
Hope that can help.

Related

Looping sound in AS3

Trying to loop a background soundtrack while my flash program is in use.
So far my code is this:
//turn off sound
btnOff.addEventListener(MouseEvent.CLICK, fl_stopsound);
function fl_stopsound(event:MouseEvent):void
{
SoundMixer.stopAll();
}
//turns sound on
btnOn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound_1);
var fl_SC_1:SoundChannel;
//keeps track of whether the sound should be played or stopped
var fl_ToPlay_1:Boolean = true;
function fl_ClickToPlayStopSound_1(evt:MouseEvent):void
{
var mySound:Sound = new background();
mySound.play();
}
where btnOff turns off the sound and btnOn turns on the sound. My soundtrack is 1:50min long. Is it possible to loop the track within the program with these buttons?
Cheers
Use mySound.play(0, int.MAX_VALUE);

How to loop SWF file loaded with Loader?

I want to loop a swf file that has been loaded with via the Loader class in AS3.
My code looks as following:
public class MyLoader extends MovieClip {
public function MyLoader() {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener("complete", function() {
});
addChild(myLoader);
}
}
From what I understand the Loader has no event for when the embedded movie is finished? Is there a way to figure that out? It must not be a AS3 implementation. I just want a movie that has been exported from Indesign to run in a loop. Thanks in advance
Especially when you have little experience programming you should avoid dirty shortcuts as they'll only get you a lot of trouble. So avoid anonymous function and avoid using string in place of static event variables.
This being said, if your loaded movie has its own timeline then it will be converted into a MovieClip. Also that movie is not embedded but loaded and that's a big difference.
Keep a reference of that movie and the loader:
private var theLoadedMovie:MovieClip;
private var myLoader:Loader;
Listen for the INIT event instead of the COMPLETE event (movies with timeline start to play when their first frame is loaded "INIT", the COMPLETE event fires when the whole movie is loaded).
myLoader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
In your handleInit method keep a reference of that movie:
theLoadedMovie = myLoader.content as MovieClip;
addChild(theLoadedMovie);
theLoadedMovie.addEventListener(Event.ENTERFRAME, handleEnterFrame);
in your handleEnterFrame method check the movie progress to know when it has ended:
if(theLoadedMovie.currentFrame == theLoadedMovie.totalFrames)
{
//movie has reached then end
}

Loop youtube clip on flash As3

I have this code which playing youtube movie in flash As3.
it working fine, but i can't make that movie will play again on ending.
any ideas?
here the code:
Security.allowDomain("www.youtube.com");
var my_player:Object;
var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3&loop=1;"));
my_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
function onLoaderInit(e:Event):void{
addChild(my_loader);
my_player = my_loader.content;
my_player.addEventListener("onReady", onPlayerReady);
}
function onPlayerReady(e:Event):void{
my_player.setSize(300,300);
my_player.loadVideoById("indux8D-SoA&loop=1;",0);
my_player.mute();
}
thanks a lot!
You should use "onStateChange" event listener for your my_player object.
And check when it pass 0 or YT.PlayerState.ENDED.
my_player.addEventListener("onStateChange", onPlayerStateChange);
function onPlayerStateChange(event:Event):void {
if(Object(event).data==0){
//reload, restart, rewind and so on code here;
}
}
Alternatively you could load youtube playlist with one video using getPlaylist()and use
setLoop method to loop playback of this "list"
my_player.setLoop(true);

How to stop sound in the middle of playing

I'm trying to write a bit of code that plays a sound while a buttons pressed however if the button has been pressed and the sound is playing then the sound is paused and played again rather then just playing and overlapping.
this is what I have
var sound:alarm = new alarm();
var isPlaying:Boolean = false;
public function Main()
{
button.addEventListener(MouseEvent.CLICK,playSound);
}
public function playSound(e:Event):void
{
if(isPlaying)sound.stop();
sound.play();
isPlaying=true;
}
at first glance It seemed to have worked but then I saw the following in my output
TypeError: Error #1006: stop is not a function.
at Main/playSound()
TypeError: Error #1006: stop is not a function.
at Main/playSound()
so apparently it works although stop is not a method of the Sound class. what would be the proper way of implementing this? Also I've been wondering if there is a more proper condition I can use, because with this code sound.stop() is called every time the function is entered after the first button click, is there a method that allows me to check in real time whether or not a sound is playing?
In your code, the function playSound(e:Event) should be playSound(e:MouseEvent);Also your right stop() is not a method of the Sound class, however your not using the Sound class, your using the alarm class (unless the alarm class extends the Sound class).On another note, I searched google and this popped up, Flash Play/Pause Sound
Update:
import flash.media.SoundChannel;
// Make sure to import the SoundChannel class
var sc:SoundChannel = new SoundChannel();
var sound:Sound = new alarm();
var isPlaying:Boolean = false;
var pausePos:Number = 0;
public function Main()
{
button.addEventListener(MouseEvent.CLICK,playSound);
}
public function playSound(e:MouseEvent):void
{
if(isPlaying) {
pausePos = sc.position;
sc.stop();
isPlaying = false;
} else {
sc = sound.play(pausePos);
isPlaying = true;
}
}
This code should work, however I have not tested it so if any errors are given or the desired result is not met just let me know and I'll see what I can do.
Short answer...okay, entire answer from me :). Instead of using the sound object, try the SoundChannel object. It offers more options, including volume and balance control, and most prominently, stop.
Documentation should provide enough info for using it. It's relatively common.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html

AS3 Code - Sound not playing

I'm trying to play sound in as3 code using an external mp3 file.
So here's the code I am using:
private function playSound():void
{
trace("loading sound");
var mySound:Sound = new Sound();
mySound.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
mySound.load(new URLRequest("Menu.mp3"));
mySound.play();
trace("playing sound");
}
private function handleIOError(evt:IOErrorEvent):void
{
//handle error if needed
}
The music just doesn't play at all.
The traces "loading sound" and "playing sound" appear so the code is being run.
The mp3 file Menu.mp3 is in the same folder as the .fla file used to run the project. Is this the correct directory? I tried moving it around but still couldnt play the sound.
Any help will be appreciated, thanks!
I have a few suggestions that might help:
Declare mySound as a class level property. The garbage collector might be disposing of the variable prematurely since it is local.
mySound.play() returns a SoundChannel object. Try storing this in a class level property.
Add an event listener to the sound for Event.COMPLETE, right before loading the sound. Try playing the sound after this event occurs. As it is, you might be trying to play the sound before it has loaded.
private var mySound:Sound;
private var mySoundChannel:SoundChannel;
private function playSound():void
{
mySound = new Sound();
mySound.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
mySound.addEventListener(Event.COMPLETE, handleLoadCompletion);
mySound.load(new URLRequest("Menu.mp3"));
}
private function handleLoadCompletion(evt:Event):void
{
mySoundChannel = mySound.play();
}
private function handleIOError(evt:IOErrorEvent):void
{
//handle error if needed
}
Edit:
After reviewing the docs, I think that suggestion 3 isn't necessary.