AS3 Code - Sound not playing - actionscript-3

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.

Related

gotoAndPlay after sound ends as3

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.

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
}

How to loop a sound after I use a soundchannel.stop() in the same sound? AS3 - FlashDevelop

I'm having a hard time to solve a sound loop in a screen that i have created, i would love if you could help me. I have created two screens: a startScreen and a gameScreen.If is the startScreen it plays a "bg sound", if is in the gameScreen the "bg sound" from the startScreen stop(i used the soundchannel.stop()). So far is okay but the soundchannel.stop(); doesn't make the sound be able to play again everytime is in the startScreen. Is there a possible to stop a sound without the soundchannel.stop(); and start again the sound from the begning everytime is in startScreen? heres a part of the code i have ben using:
private var scSound:Sound = new SCSOUND();
private var scSoundChannel:SoundChannel = scSound.play(0, 999);
private function initStartScreen():void
{
startScreen.visible = true;
gameScreen.visible = false;
scSoundChannel;
}
private function initGameScreen():void
{
gameScreen.visible = true;
startScreen.visible = false;
scSoundChannel.stop();
}
I tried to add a addEventListener for the sound but the removeEventListener doesn't wanna remove the sound.

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 + red5/fms: recording audio from soundmixer

i'm wondering if there's a way to record live audio from flash' SoundMixer (NOT from the microphone).
Might be a simple question but i'm a noob with red5/fms technologies and can't find anything online that answers my question :\
TIA for your attention.
There's a lot involved in getting this all working, but the core of the solution is to configure your Flash app to:
Create a NetConnection object
Create a NetStream object, passing the NetConnection to the constructor
Call your NetStream object's attachAudio method, passing it an instance of whatever audio source you want to use.
When you want to stop recording, simply close the NetStream object.
in code, that would look something like:
private var myMic:Microphone;
private var nc:NetConnection;
private var ns:NetStream;
// get connected
private function get_connected():void {
nc = new NetConnection();
nc.connect("rtmp://your.domain.tld:1935");
}
// get audio source
private function init_audio():void {
myMic = Microphone.getMicrophone();
}
// start recording
private function start_recording(fileName:String):void {
ns = new NetStream(nc);
ns.attachAudio(myMic);
ns.publish(fileName, "record");
}
// stop recording
private function stop recording():void {
ns.play(false); // flushes the recording buffer
ns.close();
}
I realize that's a pretty broad overview, but it's a pretty broad topic. Please comment if you have questions on the specifics.