Adobe flash cs6 Output window closes automatically while executing Filesteam operation in .exe file but it works fine in flash debug mode - actionscript-3

I am using adobe flash cs6 for creating a desktop application. In that application iam using flash.filesystem.filestream to save a text file
(I dont want to use FileReference because I don't want show the save dialog box )
When I call the new FileStream() in the exported .swf or .exe file, the app stopped running and it closes the window.
here is my sample code while executing this line
var fileStream : FileStream = new FileStream();
the window closed automatically but this code work fine in Preview mode
( ctrl + Enter) iam using
Target: AIR 2.5;
Script: ActionScript 3.0
in publish settings.
sample.as
package{
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
public class SampleClass {
public function generateReport (text : String) : void
{
var fileMode:String = (FileMode.APPEND);
var fileStream : FileStream = new FileStream();
var file:File = File.desktopDirectory.resolvePath("sample.txt");
fileStream.open (file, fileMode);
fileStream.writeMultiByte (text, File.systemCharset);
fileStream.close ();
}
}
}
Is there any way to solve this problem ?
Thank you very much!
Pravin

I have no idea what content of text is, but from that .writeMultiByte (text, File.systemCharset); I assume you wanted to write non-English alphabetic chars?
Best just use .writeUTFBytes since that handles both English & foreign alphabets.
Anycase... See if this code re-fix SampleClass.as works for you (tested with no crashing .exe) :
package{
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.display.MovieClip;
public class SampleClass extends MovieClip {
public function SampleClass ()
{
generateReport("test Chinese : 你好 世界 ... test Urdu : ہیلو دنیا ... test Russian : Привет мир");
}
public function generateReport (text : String) : void
{
var fileMode:String = "append"; //not... String = (FileMode.APPEND);
var fileStream : FileStream = new FileStream();
var file:File = File.desktopDirectory.resolvePath("sample.txt");
fileStream.open (file, fileMode);
//fileStream.writeMultiByte (text, File.systemCharset); //trying non-English chars??
fileStream.writeUTFBytes(text); //UTF is Unicode so can handle non-English chars
fileStream.close();
//trace("Text Done... check file \"sample.txt\" in Desktop");
}
} //end Class
} //end Package

Related

flash.filesystem.File is the reason my published .exe file cant be open

In my recent project, I need to get a file path where the user can specify/choose the file by using Dialog Box.
This is my code in order to do just that;
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.filesystem.File;
import flash.net.FileFilter;
import xlsxreader.Worksheet;
import xlsxreader.XLSXLoader;
public class MainClass extends MovieClip
{
var fileGet: File = new File();
var excel_loader: XLSXLoader = new XLSXLoader();
public function STRIMSMainClass()
{
var import_btn: MovieClip = MovieClip(root).powerButton_mc.utilitiesButton_mc;
import_btn.addEventListener(MouseEvent.CLICK, selectExcelFile);
fileGet = File.applicationDirectory;
fileGet.addEventListener(Event.CANCEL, fileCancel);
fileGet.addEventListener(Event.SELECT, fileSelect);
excel_loader.addEventListener(Event.COMPLETE, loadingComplete);
}
function selectExcelFile(e: MouseEvent): void
{
var fileFilter = new FileFilter("Select Excel File", "*.xls;*.xlsx;*.xlsm");
fileGet.browseForOpen("Select Excel File", [fileFilter]);
}
function fileCancel(e: Event): void
{}
function fileSelect(e: Event): void
{
excel_loader.load(fileGet.nativePath);
}
function loadingComplete(e: Event): void
{}
}
}
However, the published .exe file can not be open by my computer; it just flash and automatically close itself. I test the code before publish and it work just fine.
When I try to go through the code line by line, by delete this line var fileGet: File = new File() and obviously any line that associate with it, the .exe file can be open just fine.
I need filesystem.File for to get the .nativePath of the file that need to be open, so I can not exclude var fileGet: File = new File(). This also happen in my colleague's PC.
Please help me solve this weird error that happen right now. This is my first time trying to use Adobe Animate to develop software.

Decompiling a Bin File Inside the SWF file

I decompiled a swf file using JPEXS Free Flash Decompiler v.10.0.0 , When i decompile the swf file I only see 2 scripts:
AppLoader
$gamename-web_sec$aba37a......
In the binaryData folder, there's a thing named:
DefineBinaryData(1:gamename-web_sec$aba37a........
Here's the tree of the decompiled swf:
gamename.swf
Headers
BinaryData
DefineBinaryData(1:gamename-websec$aba37a...
Frames
Scripts
AppLoader
$gamename-web_sec$aba37a...
inside the appLoader, here's the script:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
public class AppLoader extends Sprite
{
private var content:Class;
public function AppLoader()
{
content = §gamename-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§;
super();
var loader:Loader = new Loader();
addChild(loader);
var ba:ByteArray = new content();
var bamod:ByteArray = new ByteArray();
bamod.writeBytes(ba,1,ba.length - 1);
loader.loadBytes(bamod,new LoaderContext(false,new ApplicationDomain()));
}
}
}
$gamename-web_sec$aba37a... actionscript:
package
{
import flash.utils.ByteArray;
public class §gamename-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§ extends ByteArray
{
public function §chitchatcity-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§()
{
super();
}
}
}
I see that It's loading the swf game with that BinaryData. When i try to export the bin file and try to load it again using JPEXS Flash Decompiler again. This what it shows,
[2744232]
header
shapes
sprites
binaryData
DefineBinaryData (51763)
DefineBinaryData (11730)
frames
others
ErrorTag
Unknown (ID=377)
Unknown (ID=909)
Unknown (ID=544)
...
Unknown (ID=879)
ErrorTag
Unknown (ID=777)
....
scripts
frame1
DoAction
It show's so many unknown objects in others folder.
Do P-code source: (because the actionscript source is blank)
Jump loc0005
loc0005:
Do you guys have an idea how will i able to decompile the swf file? I have a permission of the owner, and he wants me to decompile it, so we can better secure the game more.

ActionScript Error #2035: URL Not Found

I am receiving an error to suggest that my URL is not found. I am running the below simple script from Flash Builder. As I understand the application launches from bin-debug directory within the main project folder.
I have a image folder also within the main project folder yet it will not find the image. It works when I type the full directory.
The code I have is:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
[SWF(width="550", height="400", backgroundColor="#FFFFFF", frameRate="60")]
public class Kitties extends Sprite
{
//Declare the variables for the background.
public var backgroundURL: URLRequest;
public var backgroundLoader: Loader;
public var background: Sprite;
public function Kitties()
{
backgroundURL = new URLRequest();
backgroundLoader = new Loader();
background = new Sprite();
backgroundURL.url = "..\\images\\background.png";
backgroundLoader.load(backgroundURL);
background.addChild(backgroundLoader);
stage.addChild(background);
}
}
}
I understand I need to add an event listener to capture this error but for now it's not necessary as i know the file exists. Has anyone experienced this before?
Thank you.
You shouldn't use backslashes in actionscript3. Normal slashes work on every OS, even on Windows.
Try this:
backgroundURL.url = "../images/background.png";
If the file exists, it should load correctly.

AS3: How to access and control embedded sounds in an external swf?

I rarely use sounds in AS3/Flash. I am using Flash Pro CS6, but I can't seem to figure out how to access, control (play, stop, etc) sounds embedded in an external SWF loaded into the main SWF.
It's easy to control them when embedded on the main swf. However, on an externally loaded SWR, I get all kinds of errors. For this app, I really need to embed them in the external SWF.
I read several solutions, but none seem to work.
I embed the sound an mp3 file called soundSegment1.mp3 using Flash CS6 import tool and then open the actionscript properties panel on flash to select the class name: SoundSegment1. Then I edit the class code and create a file called SoundSegment1.as and it's saved right next to my document class main.as in the same directory. The code of the SoundSegment1 class looks like this:
package {
import flash.media.*;
public class SoundSegment1 extends Sound
{
public function SoundSegment1 ()
{
// no code in here
}
public function playSound()
{
var soundSegment1:Sound = new SoundSegment1();
var channel:SoundChannel = soundSegment1.play();
}
}
}
Then, in my main.as, I have done several attempts to play this sound such as:
var fileLocation:URLRequest = new URLRequest(SWFToLoad);
loader.load(fileLocation);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
function initListener(e:Event):void // I also placed this code on the completeListener and it didn't work
{
loader.content.soundSegment1.playSound(); // doesn't work.
}
I get:
Line XXX 1061: Call to a possibly undefined method playSound through a reference with static type flash.display:DisplayObject.
or, I also read that I should be able to do something like this anywhere in the Main.as file:
var theClass:Class = Class(loader.content.getDefinitionByName("SoundSegment1"));
var theSound:theClass = new theClass();
theSound.play() //doesn't work either.
I also tried on the completeListener:
var TheClass:Class = e.target.applicationDomain.getDefinition("SoundSegment1") as Class;
var theSound:TheClass = new TheClass();
theSound.play() //doesn't work either.
I get:
ReferenceError: Error #1065: Variable SoundSegment1 is not defined.
at flash.system::ApplicationDomain/getDefinition()
I am stuck and I really need to get this to work. I would be genuinely grateful for your help.
Thank you in advance for any help provided. I really need to get it to work, because I can't simply embed them in the main SWF or load them individually externally one by one.
Thanks again!
user SnickelFritz from Kirupa gave me a fabulous answer with much easier code, which can be very powerful when using multiple sounds. This is just great, because you can use only one preloader per SWF, instead of having multiple loader for each file:
code for the main.as file
http://www.kirupa.com/forum/showthread.php?305098-Playing-a-embedded-sound-in-an-external-swf
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip
{
var swf:MovieClip;
public function Main()
{
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
l.load( new URLRequest("child.swf") );
}
private function swfLoaded(e:Event):void
{
swf = e.target.content as MovieClip;
addChild(swf);
swf.playSound();
}
}
}
Code for the loaded swf "child.as"
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Child extends MovieClip
{
private var s:Sound1;
private var sc:SoundChannel;
public function Child()
{
s = new Sound1();
sc = new SoundChannel();
// All of the following lines are actually optional, if all your graphic elements are already inside the swf prepared in flash pro
var g:Graphics = this.graphics;
g.beginFill(0x666666);
g.drawRect(0,0,550,400);
g.endFill();
}
public function playSound():void
{
sc = s.play();
}
}
}
User "kglad.com" on the Adobe Forums also gave me a good answer:
http://forums.adobe.com/message/5681137#5681137

as3 Adobe Air access iPad device camera

Is there a way you can use as3 to access iPad's camera?
I mean start the Camera within the app itself
have ability take a shoot and save the image to byteArray and applying the image to the background or doing some manipulation
I have done some research most of them just showing how to access the android devices.
Thanks for any suggestion or help.
Yes, you can absolutely do this. The beauty of Flash is that the code to do it is the same that you would use on Android or a PC.
Literally, you can do this to connect the camera to a Video object:
var camera:Camera = Camera.getCamera();
var video=new Video();
video.attachCamera(camera);
this.addChild(video); // 'this' would be a Sprite or UIComponent, etc...
There's a lot more to do if you want to do something useful, but it's fairly straight forward once you get started :)
bluebill1049, I'm not certain from the thread if you got what you were looking for, but I did see your request for the whole class. I found the same information (that Jason Sturges posted in his answer) in this post.
take photo using Adobe Builder (flex) for iOS
Unlike his reply here, his reply to that post had had a link to a great tutorial on building a mobile app and it was from that tutorial that this code was lifted/quoted. It requires an event class (event.CameraEvent - only a few lines) that's contained in that project/tutorial so it's important to be able to go back to the source, as it were. That source is located here:
http://devgirl.org/files/RIAUnleashed/
My thanks to Jason. Just so you don't have to dig, here's the event class that's missing from the quote:
package events
{
import flash.events.Event;
import flash.filesystem.File;
public class CameraEvent extends Event
{
public static const FILE_READY:String = "fileReady";
public var file:File;
public function CameraEvent(type:String, file:File=null, bubbles:Boolean = true, cancelable:Boolean = true)
{
super(type, bubbles, cancelable);
this.file = file;
}
}
}
Hope that helps!
Using the loader is not the only way to access the image bytes on iOS. It turns out the data is already in JPEG format to begin with, so encoding it again is not necessary.
Just do a mediaPromise.open() to get at the bytes and save them directly instead.
XpenseIt example code offers this camera implementation:
Class: CameraUtil:
package utils
{
import events.CameraEvent;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MediaEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.media.CameraRoll;
import flash.media.CameraUI;
import flash.media.MediaPromise;
import flash.media.MediaType;
import flash.utils.ByteArray;
import mx.events.DynamicEvent;
import mx.graphics.codec.JPEGEncoder;
[Event(name = "fileReady", type = "events.CameraEvent")]
public class CameraUtil extends EventDispatcher
{
protected var camera:CameraUI;
protected var loader:Loader;
public var file:File;
public function CameraUtil()
{
if (CameraUI.isSupported)
{
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE, mediaEventComplete);
}
}
public function takePicture():void
{
if (camera)
camera.launch(MediaType.IMAGE);
}
protected function mediaEventComplete(event:MediaEvent):void
{
var mediaPromise:MediaPromise = event.data;
if (mediaPromise.file == null)
{
// For iOS we need to load with a Loader first
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleted);
loader.loadFilePromise(mediaPromise);
return;
}
else
{
// Android we can just dispatch the event that it's complete
file = new File(mediaPromise.file.url);
dispatchEvent(new CameraEvent(CameraEvent.FILE_READY, file));
}
}
protected function loaderCompleted(event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
if (CameraRoll.supportsAddBitmapData)
{
var bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height);
bitmapData.draw(loaderInfo.loader);
file = File.applicationStorageDirectory.resolvePath("receipt" + new Date().time + ".jpg");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var j:JPEGEncoder = new JPEGEncoder();
var bytes:ByteArray = j.encode(bitmapData);
stream.writeBytes(bytes, 0, bytes.bytesAvailable);
stream.close();
trace(file.url);
dispatchEvent(new CameraEvent(CameraEvent.FILE_READY, file));
}
}
}
}