How to get ByteArray of a Video? - actionscript-3

I am using this example to load a FLV file to my project, it's very simple. It works, but I would like to get the bytes (as ByteArray) after I load the video. Is there any way I can perform this procedure?
For convenience, I was using the File object (Adobe AIR) to load the file, but have not found a way to convert the bytes to a Video object.
Does anyone have any idea how I can load a video file and, after loading, get the ByteArray of this object?

var connection:NetConnection = new NetConnection();
connection.connect(null);
var stream:NetStream = new NetStream(connection);
var client:Object = {};
client.onMetadata = function():void{};
stream.client = client;
var video:Video = new Video();
addChild(video);
video.attachNetStream(stream);
stream.play(null);
stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
var file:File = new File("path/to/your/video.flv");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
// this gives you access to the raw bytes of the file
var bytes:ByteArray = new ByteArray();
fileStream.readBytes(bytes);
// this adds the bytes to the NetStream and begins playback
stream.appendBytes(bytes);

Related

AS3 Load Image Using Path From Text File

i use txt file that contain paths.
after loaded txt file and split to path array
then load img file from array path
but i get err in loading img
please help me
sample code:
var imglst:Array=new Array();
var lodimg:Loader=new Loader();
var lodtxt:URLLoader=new URLLoader();
lodtxt.load(new URLRequest("imglst.txt"));
lodtxt.addEventListener(Event.COMPLETE,onL_C);
function onL_C(e:Event)
{
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
lodimg.load(s);
}
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
function onL_Cimg(e:Event)
{
var i:Bitmap=new Bitmap();
i=Bitmap(lodimg.content);
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}
Are you loading images from a different website from your own? Does the other website(s) have a crossdomain.xml to allow permissions of loading their content? Usually Flash will give you a "security error" as an event but your code isn't listening for such an event so your program is not aware of any such problems... look on google how to handle AS3 errors.
Anyways a workaround is to just load the bytes of file using URLloader and on completion you then only use Loader to decode the bytes into pixel colours.
import flash.utils.ByteArray;
var imglst : Array;
var lodimg : Loader;
//var lodtxt : URLLoader = new URLLoader();
var lodURL:URLLoader = new URLLoader();
var img_bytes : ByteArray = new ByteArray();
lodURL.load(new URLRequest("http://yoursite/imglst.txt"));
lodURL.addEventListener(Event.COMPLETE,onL_C);
function onL_C (e:Event)
{
//# Since load complete no need to keep listening for that
lodURL.removeEventListener(Event.COMPLETE,onL_C);
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
//lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
//lodimg.load(s);
//# Now we know path so we load those file bytes
lodURL.dataFormat = URLLoaderDataFormat.BINARY;
lodURL.load(s); lodURL.addEventListener(Event.COMPLETE, onBytes_C);
}
function onBytes_C (e:Event)
{
//# on complete load of bytes...
trace("got bytes");
img_bytes = lodURL.data;
//trace("Image bytes total : " + img_bytes.length);
img_bytes.position = 0; //avoid "end of file" error
lodimg = new Loader();
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
lodimg.loadBytes(img_bytes); //we already have data so this will be fast
}
function onL_Cimg (e:Event)
{
var i:Bitmap = new Bitmap();
i = lodimg.content as Bitmap;
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}

How to load Subtitle text for Flash Video?

A Video player is created using "Video" in flash action script 3.0. And played video using net stream. The sample code is:
connection = new NetConnection();
connection.connect(null);
on connection success stream and video crested and played.
stream = new NetStream(connection);
video = new Video();
video.width = stage.stageWidth;
video.height = stage.stageHeight;
video.attachNetStream(stream);
stream.play(videoURL);
Video is playing correctly. I want to display subtitle for the video. I have .srt formeted file for the video, any solution in as3 to load the SRT for the Video on flash.
Writing a .srt parser isn't that difficult. Use the CuePoint API provided by AS3 to add cuepoints to your Video instance at runtime. Then listen for the onCuePoint event and display the relevant text in a text field.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var client = {};
client.onCuePoint = function(info:Object):void
{
var key:String;
for (key in info)
{
trace(key + ": " + info[key]);
}
};
ns.client = client;
var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);
ns.play("video.flv");
Instead of tracing the output, you can display text in an on-screen text field.

Load external swf into bytearray with adobe flash

How can I load external swf into bytearray with adobe flash AS3..
Here is my code..
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
my_url1.method = URLRequestMethod.GET;
my_url1.contentType = "application/x-shockwave-flash";
var urlloader:URLLoader = new URLLoader(my_url1);
var myByteArray:ByteArray = new ByteArray();
urlloader.data = myByteArray;
It not works well. But it isn't give any error. How can I fix this problem?
You should listen for COMPLETE event and set the data format to BINARY:
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
var urlloader:URLLoader = new URLLoader(my_url1);
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
urlloader.addEventListener(Event.COMPLETE, function(event:Event):void
{
var myByteArray:ByteArray = URLLoader(event.target).data as ByteArray;
trace(myByteArray.length);
});

Loading an Image with AIR and Displaying It On Stage

I am having trouble loading an image and displaying it on stage using the File API in as3. I am able to successfully load it in but when I put it on stage the image is just noise. I am assuming because I need to decode the PNG/JPG into Bitmap data somehow and I am doing it wrong? Here is my code:
public function browseForIcon(){
var file:File = new File();
file.addEventListener(Event.SELECT, onFileSelected);
file.browseForOpen("Select a an image");
}
private function onFileSelected(event:Event):void {
var stream:FileStream = new FileStream();
stream.open(event.target as File, FileMode.READ);
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes);
var img = new BitmapData(160,160);
img.setPixels(new Rectangle(0,0,160,160),bytes);
this.addChild(new Bitmap(img));
}
}
THANKS!
One option is to use Loader.loadBytes(). If you're using Flex, you could also use an Image with source set to the File.

Object Data Type in AS3 and Flash Builder 4.5.1

I am trying to save a Sprite object as a file on the device I'm working on and it seems to work. the problem I'm having is reading the saved file back and placing it back on stage as a sprite. Below is the code I have so far, could someone tell me what it is I'm doing wrong? I have a suspicion that the saved isn't what I expect it to be since the file sizes have been under a kilobyte.
public function save_album(e:Event):void
{
var outFile:File = File.documentsDirectory; // dest folder is desktop
outFile = outFile.resolvePath("canvas3.bin");
var fs:FileStream = new FileStream();
var bytes:ByteArray = new ByteArray();
//trace (File.documentsDirectory.url + "/canvas2.bin");
fs.open(outFile, FileMode.WRITE);
bytes.writeObject(graffitiContainer) //graffitiContainer is a Sprite
bytes.position = 0;
fs.writeBytes(bytes, 0, bytes.length);
fs.close();
}
public function open_album(e:Event):void
{
var inBytes:ByteArray = new ByteArray();
var inFile:File = File.documentsDirectory;
inFile = inFile.resolvePath("canvas3.bin"); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(inBytes, 0, inBytes.length);
inStream.close();
inBytes.position = 0;
ui.removeChild(graffitiContainer);
var obj:Sprite = inBytes.readObject() as Sprite; //returns a null
graffitiContainer = obj;
ui = new UIComponent();
graffitiContainer.x = 0;
graffitiContainer.y = 100;
ui.addChild(graffitiContainer);
}
Not fully sure I understand what you're trying to accomplish; however, this implementation doesn't do what you're thinking - writeObject could only serialize general public properties, and not the graphics member.
You could render it to a Bitmap.
Saw a blog post about this:
http://jacwright.com/201/serializing-display-objects/