Starting and stopping sound AS3 - actionscript-3

I'm trying to get a sound thats on the timeline to stop and play when i push the buttons. And there is also a animation on that timeline that should stop and play to. I cant get the code that im using to work. I get errors
Del1, Layer 'actions', Frame 1, Line 16 1061: Call to a possibly undefined method play through a reference with static type flash.media:SoundChannel.
Del1, Layer 'actions', Frame 1, Line 10 1120: Access of undefined property fireworks.
I'm new to this I have no idea how to fix the errors and get the buttons to work.
here is the entire code
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
PlayButton.addEventListener(MouseEvent.CLICK, playbutton);
NextButton.addEventListener(MouseEvent.CLICK, nextbutton);
StopButton.addEventListener(MouseEvent.CLICK, stopbutton);
var mySound:Sound = new fireworks.wav();
var myChannel:SoundChannel = new SoundChannel();
myChannel = mySound.play();
function playbutton(event:MouseEvent):void
{
myChannel.play();
play();
}
function stopbutton(event:MouseEvent):void
{
stop();
myChannel.stop();
}
function nextbutton(event:MouseEvent):void
{
gotoAndPlay(1, "Del2");
}

Your first error: You cannot call play() on a SoundChannel
Although you can always stop() your myChannel object, you can not as simply play it. See the section titled Pausing a Sound in this tutorial for information on the functionality that you are trying to build. Essentially, when you pause your song, you need to remember that location in the song, and then call myChannel = mySound.play(thatLocation); whenever you want to resume.
Your second error: The syntax on adding your song file needs to be changed a bit
If the song file is somewhere outside of Flash, like in a folder -
var mySound:Sound = new Sound();
mySound.load(new URLRequest("fireworks.wav"));
mySound.play();
If the song file is loaded into your Flash library -
var mySound:Sound = new CustomSongNameHere();
mySound.play();
But for this case, you need to find the song in your Flash library, right-click it, select Linkage, check Export for ActionScript and change the Class field to CustomSongNameHere or whatever you use in your code.

Related

Unable to Load an external Swf

I am trying to learn Action Script (self study) and therefore I took a project for myself.So this question might be way too simple or idiotic. If it is i apologise.
The goal is simple. I have 2 swf to embed within my swf. when my swf will run, it will load 1st swf by default. when you click a button, it will load the second swf.you can return back to the first swf using a different button.
After researching I came up with the action script mentioned below. The buttons work and the 1st swf work. But the second swf does not load for some reason. No compilation error found (but got an output error "TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event#2e1785d9 to OpenEvent." but i think that is comming for the working swf because of the xml it is trying to load) . wondering why the second swf is not loading even though I used a similar code as the first and how to rectify it.
url to my swf : http://itnotes.in/RLC/swf/Radio/muses-1.2/radio-tv.swf
my fla file (flash cs6 as3) : itnotes.in/RLC/swf/Radio/muses-1.2/radio-tv.fla
Any help deeply appreciated
Security.allowDomain("avastarentertainment.com")
Security.allowDomain("itnotes.in")
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
var Xpos:Number = 110;
var Ypos:Number = 180;
var swf:MovieClip;
var loader:Loader=new Loader();
loader.load(new URLRequest('http://itnotes.in/RLC/swf/Radio/muses-1.2/muses.swf?url=http://listen.181fm.com:8002&lang=auto&codec=mp3&tracking=true&volume=65&autoplay=true&buffering=5&skin=http://itnotes.in/RLC/swf/Radio/muses-1.2/simple-gray/ffmp3-simple-gray.xml&title=Vishara%20Designs'));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
/////////////////////////////////////////////////////////////////////////////
//Radio Function
radio.addEventListener(MouseEvent.CLICK, RadioBtnClick);
function RadioBtnClick(event:MouseEvent):void{
removeChild(loader);
SoundMixer.stopAll(); //stop all sounds...
loader.load(new URLRequest('http://itnotes.in/RLC/swf/Radio/muses-1.2/muses.swf?url=http://listen.181fm.com:8002&lang=auto&codec=mp3&tracking=true&volume=65&autoplay=true&buffering=5&skin=http://itnotes.in/RLC/swf/Radio/muses-1.2/simple-gray/ffmp3-simple-gray.xml&title=Vishara%20Designs'));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
}
/////////////////////////////////////////////////////////////////////////////
//TV Function
tv.addEventListener(MouseEvent.CLICK, TvBtnClick);
function TvBtnClick(event:MouseEvent):void{
removeChild(loader);
SoundMixer.stopAll(); //stop all sounds...
loader.load(new URLRequest("http://avastarentertainment.com/avanced2avan/AVAncedPlayer_TX_DeSiRe_TGZ_MS_vww861102_181powerTop40_4_29_16rev11EpCc_SSER.swf"));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
}
Your codes don't have any problems, test your project's output on your browser {in maximized window mode}.
Note:
The file
AVAncedPlayer_TX_DeSiRe_TGZ_MS_vww861102_181powerTop40_4_29_16rev11EpCc_SSER.swf
doesn't work in another domain. so it must load within http://avastarentertainment.com/
domain (another contents required for loading this file, which are accessible only on that domain {copyright} )

FLASH AS3 Sound Overlap

Alright, I'm a total noob in flash as3 so this must be very easy to solve I guess. I'm making a soundboard with recorded voices in flash cs6, very simple: 1 frame, ten buttons, each button makes a different sound. The problem is the overlapping of these sounds, so what I need is that when I press one button the other sounds stop playing. anyone please?
See the play() method documentation in the Sound class, it returns a SoundChannel object, which has a stop() method.
So you could do it like that (schematically) :
var currentChannel:SoundChannel;
button1.addEventListener(MouseEvent.CLICK, onButtonClick);
button2.addEventListener(MouseEvent.CLICK, onButtonClick);
button3.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(event:MouseEvent):void
{
/* provided you have implemented selectSoundByButton func somewhere */
const sound:Sound = selectSoundByButton(event.currentTarget);
if (currentChannel) {
currentChannel.stop();
}
currentChannel = sound.play();
}
More detailed description:
Let's say you want to create yet another fart button application in flash. That's what you have to do:
Create a button symbol, add it to stage and give it an instance name in properties tab. Let's call it myButton
Add the sound to library with file->import
Export this sound to actionscript. Right click on a sound in library, check "Export for actionscript", "export in frame 1" on "actionscript tab". Fill "Class" input with a desired class name for a sound (e.g. MySound)
Then you have to trigger the sound playback on your button click. So you should put the following code to the first frame of your flash clip:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var currentChannel:SoundChannel;
const mySound:Sound = new MySound();
function onClick(e:MouseEvent):void {
if (currentChannel) {
currentChannel.stop();
}
currentChannel = mySound.play();
}
myButton.addEventListener(MouseEvent.CLICK, onClick);
Add this to your code on each button before playing a sound:
SoundMixer.stopAll();
If you're adding actions directly from the timeline in Adobe Flash, there's no need to import the class. If you're working from an IDE like FlashDevelop or FlashBuilder, add this code to the beginning (after Package {):
import flash.media.SoundMixer;
Happy coding!
Edit: More info on the SoundMixer class

Flash CS6 Error #1009 - Button disappears

I'm starting to use Flash CS6 for the first time to try and make Scaleform UI's for UDK. I'm following this simple tutorial: http://goo.gl/yedMU. I've followed it to the letter but can't seem to get it to work. I even have tried it again in a new project but it ends up with the same error. I've triple checked each name and instance but it just refuses to work. Here is the really simple code of the two frames in the file:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
var cursor:cursor_mc = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
stop();
function subMenu(event:MouseEvent):void
{
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
and
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
gotoAndStop('Main Menu');
}
I used the debugger and the code breaks at
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
Any ideas? The whole thing works until I used the 'Back' button to go back to the first frame, when the 'Exit' button is gone and I get that error. The 'Submenu' button remains however and the menu is still operable.
This is the error using the debugger:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Menu_fla::MainTimeline/frame1()[Menu_fla.MainTimeline::frame1:6]
at flash.display::MovieClip/gotoAndStop()
at Menu_fla::MainTimeline/backBtn()[Menu_fla.MainTimeline::frame2:10]
Okay, so I might have been wrong in what I said in the comments above, the solution is this:
For your first frame's AS3:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
var cursor:cursor_mc = new cursor_mc();
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
stop();
function subMenu(event:MouseEvent):void
{
subMenu_btn.removeEventListener(MouseEvent.CLICK, subMenu);
exit_btn.removeEventListener(MouseEvent.CLICK, exitGame);
removeChild(cursor);
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
For your second frame's AS3:
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
cursor = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
removeChild(cursor);
gotoAndStop('Main Menu');
}
You were instantiating the cursor each time you hit frame 1, which I believe would create a namespace problem. The solution was to remove the cursor from the stage, and add it back in for each frame. There may be a more elegant solution, but as I never use multiple frames with AS (for this very reason), this is the best I could do. I also hid the mouse cursor to give your cursor_mc more focus.
Let me know if you have any other questions.
Happy coding!
Another solution would be to create a new layer, and put the cursor code on that layer only. That layer would have one keyframe, and extend to cover the entire timeline, so it is always alive.
Or, create a menu movie clip that exists on frame 1. And inside that movie clip, have your different frames for each part of the menu (options, etc.). And the cursor would exist at the same level as the menu movie clip. So it always exists and is only initialized once.

as3 Sound Class Unloading Sound

I have tried this for HOURS. There must be a simple solution to stop the sound and unload it in as3.
This is not all of my code, but in short I am loading random sounds. I need certian vars to be outside of the function so I can reference them with other functions for a progress bar.
How do I unload a sound so I can load a new one using the same names without getting an error the second time its called?
I have two buttons in this test. A play Sound and a Stop Sound Button.
here is my code:
var TheSound:Sound = new Sound();
var mySoundChannel:SoundChannel = new SoundChannel();
PlayButton.addEventListener(MouseEvent.CLICK, PlaySound);
StopButton.addEventListener(MouseEvent.CLICK, Stopsound);
function PlaySound(e:MouseEvent)
{
TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3"));
mySoundChannel = TheSound.play(0);
}
function StopSound(e:MouseEvent)
{
delete TheSound;
}
HERE IS THE ERROR I GET:
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at Untitled_fla::MainTimeline/PlaySound()[Untitled_fla.MainTimeline::frame1:21]
UPDATE.... I TRIED STOPING THE SOUND AND THEN UNLOADING IT AS FOLLOWS
mySoundChannel.stop();
TheSound.close();
BUT NOW I GET THIS ERROR:
Error: Error #2029: This URLStream object does not have a stream opened.
at flash.media::Sound/close()
at Untitled_fla::MainTimeline/shut1()[Untitled_fla.MainTimeline::frame1:35]
I believe I am closer. Thanks so much for your help so far.
In order to stop the sound from playing, you first have to tell the SoundChannel instance to stop like so :
mySoundChannel.stop();
Once you did that, you can close the stream used by the sound instance by invoking the close method like so :
TheSound.close();
Also, the delete keyword is rarely used in as3 and you should not it while some methods are trying to access the variable you are deleting. If you want to dispose of the instance that is currently assigned to your TheSound variable, you should set its value to null. This way, flash will properly garbage collect the old Sound instance that is no longer used when it finds the appropriate time to do so.
You can initialise the variable outside of the function, but define it as a new Sound object each time the function is called. That way it has global scope, and you can load a new URL at any time.
var TheSound:Sound;
var mySoundChannel:SoundChannel = new SoundChannel();
PlayButton.addEventListener(MouseEvent.CLICK, PlaySound);
StopButton.addEventListener(MouseEvent.CLICK, StopSound);
function PlaySound(e:MouseEvent)
{
TheSound = new Sound();
TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3"));
mySoundChannel = TheSound.play(0);
}
function StopSound(e:MouseEvent)
{
mySoundChannel.stop();
TheSound.close()
}

AS3 Loader keeps reloading same swf over and over

I have a flash animation made on the main timeline of the SWF with a couple of layers, some functions and some keyframe labels. For example, i have a movieclip of a star that come across the screen and then triggers a dispatchEvent for the main timeline to go to frame label "next".
Here is a sample of the actionscript used on the main timeline :
Stars.addEventListener("fadeInTitle",_fadeInTitle);
function _fadeInTitle(e:Event):void {
Title.gotoAndPlay("fadeIn");
Stars.removeEventListener("fadeInTitle",_fadeInTitle);
}
stop();
That SWF alone works perfectly. Problem comes when I try to load this SWF into another one.
What happens is the loader keeps reloading the SWF over and over, overlapping them and the actionscript that's on the main timeline of the loaded SWF is ignored, the timeline plays continuously. Here is the code i use to load the SWF :
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad(){
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("Fly.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.target.content);
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
}
startLoad();
There's nothing special there. Just a simple loader.
I have found a workaround by putting the entire animation inside one main movieclip and put that movieclip on the main timeline (one keyframe, one layer, no actionscript) and then load it. That way it works fine but it feel more like a patch than a solution. I would really like to know why it's bugging when you try to load an external SWF that use the main timeline with multiple layers, keyframes and actionscript.
Any help/hint will be greatly appreciated.
Thanks a lot for reading.
m
First take the loader instantiation out of a function, it is not necessary. Second, kill your listeners in the onComplete function. Third, if this loader code is on the timeline of your loader shell, make sure that there is not more than one frame, or if there is (not sure why there would be though) place a stop on the frame containing the loader code. Or even better, use a Document class to contain your loader code rather than putting it on the timeline. My best guess is you were calling startLoad() over again somehow. Also, make sure you have the flash debug player installed so you are getting proper error reporting when viewing this in a browser.
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("Fly.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
function onCompleteHandler(loadEvent:Event)
{
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
addChild(loadEvent.target.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
}