ActionScript 3 - sound mp3 is not played without error - actionscript-3

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

Related

Flash AS3 - SoundChannel error #1009

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

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.

AIR iOS app hangs when clicking button inside imported swf

Been bangin my head against a wall all day, thought I'd see if anyone can shed some light on this -
I have an iOS air app that imports a remote swf. Once imported, an event listener is added to a button inside the imported swf. Clicking the button causes the app to hang. Here's some code -
private function loadRemoteSWF():void{
var urlRequest:URLRequest = new URLRequest("http://www.domain.com/remote.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(urlRequest);
}
private function onLoaded(e:Event):void{
var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
var adPanel:MovieClip = loaderInfo.content as MovieClip;
adPanel.continueButton.addEventListener(TouchEvent.TOUCH_BEGIN, onContinueClicked);
addChild(adPanel)
}
private function onContinueClicked(e:TouchEvent):void{
trace("onContinueClicked");
}
I'm using Flash Builder 4.7 AIR SDK 3.5 ASC 2.0.
This only happens on a release build, debug builds work fine so near impossible to find the cause. It also works fine when using the legacy compiler on the same SDK version.
Dispatching a touch event programatically on the button also works fine. (thought I could do a try/catch to find an error)
adPanel.continueButton.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN));
Touching the button just kills the app, it doesn't even hit the trace.
Anyone got any ideas how to debug this, or why this problem might be happening?
Thanks
Dave
can you do a test: instead of using the the native loader, try using an API, like greensocks api - SWFLoader.
Also, try setting the accepted domains through the security class prior to loading the swf file:
import flash.system.Security;
public class Main extends MovieClip{
public function Main(){
Security.allowDomain("*");
}
}
I would suggest trying to compile with the same setup on a different machine, just to make sure that the Builder install doesn't have some issues.
Also, have you tried on different devices, (could be an issue with the device you are using)?

Android & Flash Professional CS 5 Adobe Air Audio: Stream Error

I have the following code:
import flash.media.Sound;
import flash.net.URLRequest;
function playBase():void
{
var urlRequest:URLRequest = new URLRequest("mybase.mp3");
var sound:Sound = new Sound();
sound.load(urlRequest);
sound.play();
}
playBase()
I have mybase.mp3 into my LIBRARY section, but the DEBUG OUTPUT show me:
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
Why?
URLRequest is used to load an external file, so either
1: save the mp3 file as "mybase.mp3" to the same directory that is running your Flash/AIR App, instead of in your library.
OR
2: In your library right click on the mp3 file > properties > and export for actionscript using a name like MyBaseSound. Once you do that you should be able to reference the sound like a class, something like:
var s:MyBaseSound = new MyBaseSound();
s.play()
EDIT
If going with option 1 you likely will want to go to Publish Settings then to your Android Settings area and under included files add that file. This should add it to your app bundle / package. (In CS6 it is the little wrench icon, but in CS5 I don't recall the exact steps to get there for Android, but I know the iOS settings page has a similar included files option.)

AS3:loading XML is not working after exporting the air file?

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
names();
function names()
{
var url:URLRequest = new URLRequest("http://artbeatmedia.net/application/xmlkpisname.php?username=adel_artbeat&password=ADL1530");
url.contentType = "text/xml";
url.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.load(url);
loader.addEventListener(Event.COMPLETE, onLoadComplete);
}
function onLoadComplete(event:Event):void
{
trace('good');
}
i used adobe air to load xml using as3,this code works only in the .fla file,but when i export the .air file it does not work.
if you export a program, trace messages won't be visible.
you can still use MonsterDebugger, but thats a security hazard.
Vizzy tracer will still capture trace calls from an Air app. Get Vizzy from Google code
I tried your code with no modifications just now and it worked just fine, so I suppose your problem is just a failed trace out. Have a go, Vizzy shows even traces from within Flash IDE, so see what happens. If it still fails, then it might be some connection/permission issue.
Are you including the XML file when you publish the AiR app? Happened to me once, forgot to include the file in the publish dialogue....an hour of my life I'll never get back.