Flash AS3 - SoundChannel error #1009 - actionscript-3

I'm a bit of a Flash newb so apologies if this is a simple issue or fix.
Anyways I'm having problems with some functions. I've noticed I'm getting error 1009 only on functions that attempt to stop a sound channel. Here's some of the code:
This is one the first frame of the first scene, just setting up my channels. All my sounds are done here too but I don't think those are a problem.
import flash.media.Sound;
import flash.media.SoundChannel;
var musicChannel:SoundChannel = new SoundChannel();
var speechChannel:SoundChannel = new SoundChannel();
var sfxChannel:SoundChannel = new SoundChannel();
And here's an example of one of the buttons that causes the 1009 error:
function goHome(e:MouseEvent):void {
speechChannel.stop();
sfxChannel.stop();
musicChannel.stop();
gotoAndPlay(2, "TitleScreen")
}
home.addEventListener(MouseEvent.CLICK, goHome);
Now what really confuses me is that this same code ran perfectly last night on my computer. I went to test my .swf on the computers at my college and my animation froze, so I checked in Flash and this is what I got.
I'm also getting the same 1009 error on some TweenMax stuff. And yes I did import everything necessary and all files that are required for these functions to run are in the root directory of the .fla file. I know this because, as with the other error, this stuff worked on my computer at home perfectly. I am running the exact same .fla file and code file I saved on my computer and I do have the 'com' folder and 'greenstock' swc files in the same directory as my .fla, just like on my PC, yet I'm stilling seeing 1009 with this:
import com.greensock.TweenLite;
import com.greensock.TweenMax;
TweenMax.to(musicChannel, 2, {volume:0});
Again this line is referencing a sound channel, so I'm 99% sure it's the root of these problems. I can swap out 'musicChannel' in this piece of code with 'Music1' (a sound declared in the first frame with the channels) and it stops the music fine, but whenever I try to stop sound channels it craps out on me.
Again sorry if this is really simple to fix but I'm quite confused because it was working fine last night. The Flash Player could be outdated but it's Adobe Flash Player 11 so I don't know if that would be the problem. Right now I'm at college so I don't know what version I have at home.
Thanks for your time.

SoundCahnnel can be null and when they are you can't try to tween their property. An easy way to deal with that is to check whether they are null or not:
if(musicChannel)
{
TweenMax.to(musicChannel, 2, {volume:0});
}
Also SoundChannel objects are generated only when a Sound object play method is called so there's no need to instantiate them:
var musicChannel:SoundChannel = new SoundChannel();//irrelevant piece of code
var musicChannel:SoundChannel;//correct

Related

ActionScript 3 - sound mp3 is not played without error

Help me, I use Adobe Flash CS3 Professional.
// i have try using this import, but still not work
/*import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
*/
// here is the code
var req:URLRequest=new URLRequest("music.mp3");
var snd:Sound=new Sound();
snd.load(req);
var chan:SoundChannel=snd.play();// not work using 'snd.play(1);' too
chan.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event){
trace("Musik Complete");
}
When i run the swf or ctrl+enter, the output Musik Complete without any sound played, but the animation still played. For the animation i just create simple twin motion.
i work on this directory
C:/flash/example.fla
C:/flash/example.swf
C:/flash/music.mp3
I have try to import music.mp3 into the library too.
Note: I get the example from my friend(.swf, .fla, .mp3). I play the .swf that shared by my friend and work, but when i try to create by my self, is not work. So, i think music.mp3 was fine to use for Flash. Here is my friend code:
var Audio:musik = new musik();
var chan:SoundChannel = new SoundChannel();
// play
chan=Audio.play();
// stop
chan.stop();
// i am so confuse i run my friend project and work fine (sound and other), if i write from scrap even i write the same code, there is an error, and this the error:
// 1046: Type was not found or was not a compile-time constant: musik.
// 1180: Call to a possibly undefined method musik.
I just watch on youtube, i missing one step.
In library, open properties of music.mp3 and give it class name.
So, we can call the class using this var Audio:musik=new musik();.
Then, create the Sound Channel.
At least, function to play and stop using this:
//play
chan=Audio.play();
// stop
chan.stop();

What does the message "Test Movie Launch Failed" mean? (Flash Professional CC)

The Problem
When I was creating a test program using Flash Professional CC, I got the message "Test Movie Launch Failed" while compiling, and when I tried to open it, nothing happened and it didn't open. However, I got no compiler errors whatsoever, it just said "Test Movie Terminated." in the output.
What Might be Going on
I was thinking it might have been an error in my code, but I can't tell since the error message is extremely vague and no runtime errors appear.
So Here's my code (just in case if it's an error of the code)
Info:
Target: Air 3.6 for Desktop
Script: ActionScript 3.0
Objects:
Input Text Box with instance name of "writeT"
Dynamic Text Box with instance name of "readT"
MovieClip with instance name of "Write" (Too lazy to make buttons)
MovieClip with instance name of "Read"
import flash.filesystem.*;
import flash.events.MouseEvent;
Write.addEventListener(MouseEvent.CLICK, writeToFile);
Read.addEventListener(MouseEvent.CLICK, readFile);
var file:File = File.desktopDirectory.resolvePath("stuff.txt");
var fs:FileStream = new FileStream();
function writeToFile(e:MouseEvent):void{
fs.open(file, FileMode.APPEND);
var text:String = writeT.text;
fs.writeUTF(text);
writeT.text = "";
fs.close();
}
function readFile(e:MouseEvent):void{
fs.open(file, FileMode.READ);
var text:String = fs.readUTF();
readT.text = text;
fs.close();
}
EDIT: I commented all of my code and the error still showed up, so ignore the code
I typically see this if I use a library (.swc or .ane) that the desktop debugger cannot use.
If you using any libraries, perhaps uncouple them to see if the error disappears.
Difficult to say what the exact problem could be. From what I read, seems like a version issue. This thread maybe of some help : https://forums.adobe.com/thread/1007928
On a side note, I noticed that you have your instance named as Write and Read. I would typically avoid such names as they might conflict with reserved keywords, and while naming instances, it's best to name them using a camel case convention.
So, I would name them something Iike writeMc and readMc.
Hope this helps.
if you're publishing a mobile app, test on a mobile. you will get that error when using certain mobile-only code in the test environment. ie, test on an appropriate (ios/android) mobile.

AS3 text to file

I'm making a game in cs3 using as3. I've seen dozens of tutorials, but none of them are working for me.
I found the simplest code I could, and it still gives me an error "1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference."
here's the code I'm using:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")
Are you importing FileReference? If not, you'll need:
import flash.net.FileReference;
..inside your package declaration (or at the top of your block of code if you're coding on the time line)
With that import included, your code works for me.

trouble getting flashdevelop to correctly identify sampling rate of a sound file

I'm trying to put a sound file into a flash program with the following code.
[Embed(source="../lib/DST-Aircord.mp3")]
static var sndtrck:Class; //used to represent the background music file
static var soundtrack:Sound; //used to handle the soundtrack file
soundtrack = new Sound();
soundtrack.load(sndtrck);
soundtrack.play();
I'm not sure that this is correct but it is throwing the following errors.
Error: Unsupported sampling rate (32000Hz)
Error: Unable to transcode ../lib/DST-Aircode.mp3.
Regarding the first error I checked in iTunes and the sampling rate is 44100Hz which I believe is what Flash needs to run. However FlashDevelop is incorrectly determining the sampling rate? I'm guessing that the second error is caused by the first although I'm not entirely sure what it means. Does anyone know what I might be missing in my attempt to embed sound into this SWF?
EDIT: changing the load function to use the path specified in the embed code yields the same error.
the load function of the Sound class only takes a URL of an external sound file. Im not sure what the class your putting into it is, but it probably throws the errors because a Class itself is not a sound file.
This is a link to the load() function of the Sound class, everything about it is there.
While writing this answer I realized that the question is pretty old. ;)
Anyhow, I ran into the same problem a while back and it could be solved
by this: Unsupported sampling flex/actionscript

Loader.load and Loader.loadBytes differences

I'm loading as2 swf into AIR application. It works properly when loaded from file. But when loaded from bytes, it is broken in some way (it reacts to mouse, but some elements are inactive)
var bytes:ByteArray = ... //loaded from resources
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext(false);
context.allowCodeImport = true; //this is neccessary
// Method 1 - blocks some scripts in loaded SWF
//context.applicationDomain = new ApplicationDomain();
// (application domain has no effect with as2 swf)
//context.securityDomain = SecurityDomain.currentDomain; //gives error 2114: securityDomain must be null
loader.loadBytes(bytes, context);
// Method 2 - loads properly
//loader.load(new URLRequest(file.url));
So why not just load it from file? My resources are protected with encryption and I can't dump them to disk - they must still be protected.
What tricks may exist to load from bytes properly?
There is similar question, but in my case as2 causes more problems.
AS2 and AS3 use different runtimes (bytecode is different) so you won't be able to properly execute any AS2 bytecode in the AS3 runtime. you are basically injecting AS2 code into your AS3 application, so it ain't gonna work :/
According the the documentation for LoaderContext you should only use the applicationDomain property only when loading ActionScript 3.0 SWFs. Try dropping that parameter (or setting it to null) and see what happens.
It seems that old SWF movies (AS1 and AS2, which require AVM1) loaded into an AIR app with load get put in their own domains, but those loaded with loadBytes share a domain. So if you have multiple AVM1 SWFs loaded with loadBytes their _global properties will clobber each other. This affects the Flash MX UI components (ca. 2002).
I can't be the only one trying to package ancient Flash files in AIR apps, so I figure this info may be useful to someone.