Opening an SWF file in a new window - actionscript-3

I have a button in my desktop flash application, when clicked, it must load an external SWF file and open it in a new window without using the browser.
i've tried
import flash.system.fscommand;
loadButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
fscommand ("exec", "external.swf");
}
But it didn't work. Someone mentioned in a forum that the app must be compiled as a .EXE file for this code to work. is that true?.
i've also tried
loadButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
navigateToURL(new URLRequest("external.swf"), "_blank");
}
But this opens the file in a new browser window.
Any idea how can i do it without using the browser window?

It is simple with the NativeWindow. Despite the fact the NativeWindow is a separate window, your application is still a whole and have a full access to everything, the new window included. You just need to add a Loader with another SWF to the new window's stage.
// This is an example from official docs:
// https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/NativeWindow.html#NativeWindow()
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
windowOptions.type = NativeWindowType.NORMAL;
var newWindow:NativeWindow = new NativeWindow(windowOptions);
newWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
newWindow.stage.align = StageAlign.TOP_LEFT;
newWindow.bounds = new Rectangle(100, 100, 800, 800);
newWindow.activate();
// Now the only thing left is to load the external SWF into it:
var aLoader:Loader = new Loader;
// Load SWF as usual:
aLoader.load(new URLRequest("external.swf"));
// Add it as a child to the new window's stage:
newWindow.stage.addChild(aLoader);

Related

AS3 browse and load video

I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:
private function browseVideo():void {
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, videoFileSelected);
var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");
fileReference.browse([videoTypeFilter]);
}
private function videoFileSelected(e:Event):void {
fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
fileReference.load();
}
function onVideoFileLoaded(e:Event):void {
var fileReferenceTarget:FileReference = e.target as FileReference;
var data:ByteArray = fileReferenceTarget["data"];
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
var videoLoader:Loader = new Loader();
videoLoader.loadBytes(data);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);
trace("video file loaded");
}
function onVideoFileLoadError(e:Event):void {
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
trace("video file load failed");
}
function onVideoLoaderComplete(e:Event):void {
var loadedContent:DisplayObject = e.target.content;
var loader:Loader = e.target.loader as Loader;
scope.addChild(loader);
}
To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.
Take a look on this example :
function browseVideo():void {
var file:File = new File();
file.addEventListener(Event.SELECT, videoFileSelected);
file.browse([videoTypeFilter]);
}
function videoFileSelected(e:Event):void {
playVideo(e.currentTarget.nativePath);
}
function playVideo(video_path:String){
// using a Video + NetStream object
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
var video:Video = new Video();
video.attachNetStream(ns);
addChild(video);
ns.play(video_path);
// using an FLVPlayback component inserted on the Stage
flvplayback.load(video_path);
flvplayback.play();
}
For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).
Hope that can help.

Flash Loader works only inside of Flash

I need to load flex swf file into a flash (a button on witch I need to apply flex skin). I'm using flash Loader() and inside of Flash it works fine (swf is loaded and displayed). But when I try run a compiled flash swf, the Loader does not load the flex swf. There are no errors handled with IOErrorEvent.IO_ERROR and the ProgressEvent.PROGRESS just sais that 0 bytes was loaded. Than nothing else happen.
Here is a part of my code (just display flex button and set a label):
var flexBtn:String = "button.swf"; //swf file is in the same folder as flash source code
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete);
myLoader.load(new URLRequest(flexBtn));
stage.addChild(myLoader);
var timer:Timer;
function loadComplete(event:Event):void
{
timer = new Timer(200);
timer.addEventListener(TimerEvent.TIMER, timeHandler);
timer.start();
}
function timeHandler(event:Event):void
{
timer.stop();
var myClip:MovieClip = myLoader.content as MovieClip;
myClip.x = 10;
myClip.y = 10;
if (myClip.currentFrame == 2){
myClip.application.flexButton.label = "FlexButton";
}
}
I have tried different versions of SDK and different Loaders but it didn't work.
All suggestion will be appreciated :-)
Add SecurityErrorEvent - I guess it's because of a privacy policy error.
And of course you must be sure they are in the exact same folder (correct url).

Flash .fla embed banner add

I need to create a Flash web banner. The .swf file size needs to be >40k so I made a .fla video and tried to stream it into the file however I'm getting no luck.
I can't find anything on the internet so I thought I'd ask here.
Im using Flash CC AS3
When I run the file through the
https://flashval-temp.appspot.com/validator/
the .swf uploads but the .fla doesn't play at all.
The code I was using was:
var vid:Video;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
var customClient:Object = new Object();
customClient.onMetaData = metaDataHandler;
ns.client = customClient;
ns.play("300x250.flv");
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace(event.text);
}
function metaDataHandler(infoObject:Object):void {
vid.width = infoObject.width;
vid.height = infoObject.height;
}
It seems to work fine offline, the files are in the same directory too.
Without knowing the full environment it's hard to tell you what's wrong, but you may want to check these:
(1) upload your video file to a server and grab the full url to the file, then update your code with it:
ns.play("http://www.YOUR_DOMAIN.com/300x250.flv");
(2) add the following as3:
Security.allowDomain("*");
(3) If that still doesn t work, look into crossdomain.xml at the root of your server hosting the video.
But (1) is likely to be the issue.

Preload a bit of FLV or F4V with the main project loader

My project has a main loader that load all the assets for the project. I need to load a bit of my video with it.
The video has 16mb, i want to load 3mb to use after the main loader is completed.
I've tried to open a new connection using Netconnection/Netstream to load 3mb and close the connection, but when the project starts and the video is played, a new connection is opened loading it from beginning.
I'm trying to find a way that i can use those 3mb already loaded. Doing this way, the user don't need to wait a main loader and a secondary loader (buffertime).
That's my code, sorry guys.
var loader:Loader = new Loader();
var nc:NetConnection = new NetConnection();
var ns:NetStream = new NetStream(nc);
var client:Object = new Object();
var swfRatio:Number;
var videoRatio:Number;
function init():void
{
nc.connect(null);
client.onCuePoint = cuePointHandler;
client.onMetaData = metaDataHandler;
ns.client = client;
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoader);
addEventListener(Event.ENTER_FRAME, progressTotal);
loader.load(new URLRequest("swf/main.swf"));
ns.play("f4v/main_movie.f4v");
ns.pause();
}
function progressLoader(event:ProgressEvent):void
{
swfRatio = (event.bytesLoaded / event.bytesTotal);
}
function progressTotal():void
{
//Here i get the amount that i want to preload from my video, in this case i want 3mb or 3072000 bytes
videoRatio = (ns.bytesLoaded / 3072000);
//This is a variable that i use to fill my loader asset and verify if my content its totaly loaded.
var frameValue:int = ((videoRatio + swfRatio) / 2) * 100;
if (frameValue >= 100)
{
removeEventListener(Event.ENTER_FRAME, progressTotal);
// Here i close my connection, i suppose that i need to use the same connection in my player.
ns.close();
ns = null;
nc.close();
nc = null;
loaderComplete();
}
}
function loaderComplete():void
{
removeChild(assetLoader);
//Here i add my player to the stage, i want to use the preloaded video with him.
addChild(loader.content);
}
function cuePointHandler(infoObject:Object):void {
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
Then in my player that i've just loaded and added to the stage i'm using OSMF to help me with controls.
To test the "preloaded video" i'm doing this:
private var mediaPlayer:MediaPlayerSprite;
private function _init(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, _init);
mediaPlayer = new MediaPlayerSprite();
addChild(mediaPlayer);
//And here OSMF start to load the entire main_movie.f4v again.
mediaPlayer.resource = new URLResource("f4v/main_movie.f4v");
}
It looks like your strategy would work if you let the video download completely. I'm assuming if the video downloaded completely, the browser may cache it and make it available to the next NetStream that comes along.
Your strategy looks OK otherwise. What you are doing (playing, then pausing the video immediately) is the way to start buffering the video. But since there are two NetStream's being used (one by the main loader, the other by the OSMF player) this won't work.
Perhaps you can devise a scheme where you pass the NetStream from the main loader into the loaded SWF (main.swf). So that it can use the data it's already downloaded. Just a thought, I've never tried this.
Another idea would be to get the OSMF player in your main.swf to do the buffering. That would mean the buffering would start happening only after main.swf is loaded (which may be too late).

How to convert loaded SWF to bitmapdata?

I use the loader class to load some png files to the stage. The function that is called after the file is loaded is something similar to this:
private function IconLoaded(e:Event):void
{
percentLoaded_txt.visible = false;
iconLoader = Loader(e.target.loader);
icone = Bitmap(iconLoader.content);
icone.smoothing = true;
var _icon: imgBox;
_icon = new imgBox(_MAX_WIDTH, _MAX_HEIGHT, icone,0);// new imgHelper(_bitmap);
_icon.x = _main.cena.width/2;
_icon.y = _main.cena.height/2;
addChild(_icon);
}
imgBox is a class that auto resize the bitmap and allow also to add border for example...
My code works fine, to load PNG files. But I want also to be able to load swf files.
How can I do this?
Thanks.
You will need to make a copy of the loaded SWF into a Bitmap.
function copyIt(obj:DisplayObject):Bitmap {
var bd:BitmapData = new BitmapData( obj.width, obj.height );
bd.draw(obj);
return new Bitmap(bd);
}
var bmpCopy:Bitmap = copyIt(mySWF);
addChild(bmpCopy);
You can't load a swf as a bitmap data because they are totally distinct types. What you can do is load a swf and add it as a child of a movie clip to show its content.
This link shows how to load a swf. After loading it, you just need to add this as a child of a MovieClip and add this movieclip to the stage to show its content.