load multiple flv videos using FileReferenceList class - apache-flex

following is my code for loading one video using FileReference class and it works fine
[Event(name="complete",type="flash.events.Event")]
[Event(name="status",type="flash.events.StatusEvent")]
public class LocalFileLoader extends EventDispatcher
{
public function LocalFileLoader()
{}
private var file:FileReference;// = FileReference(event.target);
private var list:FileReferenceList;
public var p2pSharedObject:P2PSharedObject = new P2PSharedObject();
public function browseFileSystem():void {
file = new FileReference();
list = new FileReferenceList();
list.addEventListener(Event.SELECT, selectHandler);
list.browse();
}
protected function selectHandler(event:Event):void {
for each ( file in list.fileList ){
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
writeText(file.name+" | "+file.size);
file.load();
}
}
protected function securityErrorHandler(event:SecurityErrorEvent):void {
writeText("securityError: " + event);
}
protected function completeHandler(event:Event):void {
writeText("completeHandler");
p2pSharedObject = new P2PSharedObject();
p2pSharedObject.size = file.size;
p2pSharedObject.packetLength = Math.floor(file.size/32000)+1;
p2pSharedObject.data = file.data;
p2pSharedObject.chunks = new Object();
var desc:Object = new Object();
desc.totalChunks = p2pSharedObject.packetLength+1;
desc.name = file.name;
p2pSharedObject.chunks[0] = desc;
for(var i:int = 1;i<p2pSharedObject.packetLength;i++){
p2pSharedObject.chunks[i] = new ByteArray();
p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,32000);
}
// +1 last packet
p2pSharedObject.chunks[p2pSharedObject.packetLength] = new ByteArray();
p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,p2pSharedObject.data.bytesAvailable);
p2pSharedObject.packetLength+=1;
writeText("----- p2pSharedObject -----");
writeText("packetLenght: "+(p2pSharedObject.packetLength));
dispatchEvent(new Event(Event.COMPLETE));
}
protected function writeText(str:String):void{
var e:StatusEvent = new StatusEvent(StatusEvent.STATUS,false,false,"status",str);
dispatchEvent(e);
}
}
the sender.mxml code is following which plays the video on the stage
private function init() : void {
fileLoader = new LocalFileLoader();
fileLoader.addEventListener(Event.COMPLETE, fileLoaded);
fileShare = new P2PFileShare();
fileShare.addEventListener(StatusEvent.STATUS,
function(event:StatusEvent):void {
writeText(event.level);
});
fileShare.connect();
}
private function fileLoaded ( event:Event ) : void {
writeText("fileLoaded");
if (fileShare.connected) {
fileShare.p2pSharedObject = fileLoader.p2pSharedObject;
fileShare.p2pSharedObject.lastIndexBegin = 0;
fileShare.p2pSharedObject.lastIndexEnd = fileShare.p2pSharedObject.packetLength-1;
fileShare.updateHaveObjects();
}
setupVideo();
// PLAY
ns.play(null);
ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
ns.appendBytes(fileLoader.p2pSharedObject.data);
video.attachNetStream(ns);
}
private function setupVideo():void{
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = this;
ns.addEventListener(NetStatusEvent.NET_STATUS,
function(event:NetStatusEvent):void{
writeText("stream: "+event.info.code);
});
video = new Video();
videoComp = new UIComponent();
videoComp.addChild(video);
this.addElement(videoComp);
}
then after this, the file.name and file.size goes to mxml page and the video is displayed on stage with name and file size
but the samething i want to do with FileReferenceList class, i am solving this problem from last 2 weeks but cant ,,, plz guide me ,,, i google so many times but no specific answer
Regards
Ammad Khan

You can use the fileList property of the FileReferenceList class to access the files that where selected. Then load() each FileReference in the list separately:
private var list:FileReferenceList;
public function browseFileSystem():void {
list = new FileReferenceList();
list.addEventListener(Event.SELECT, selectHandler);
list.browse();
}
protected function selectHandler(event:Event):void {
for each ( var file:FileReference in list.fileList) {
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
writeText(file.name+" | "+file.size);
file.load();
}
}

Related

Looping through an arraylist sourced from an XML file

I am reading in an XML file that is shown in the attached image. I'm reading it in using URLRequest, which works properly. The next thing I'd like to do is to populate an arraylist with all of the "project" nodes. I'm converting the XML to an array, but the source is showing the project as being in the [0] node and the arraylist's length is 1.
What's the proper way to do this so I can loop through all the projects in the arraylist?
private var xmlParameters:XML
private var xmlStoryMap:XMLList;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var params:Object;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlloader_onComplete_Handler);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,IOError_handler);
xmlLoader.load(new URLRequest("myXML.xml"));
}
protected function xmlloader_onComplete_Handler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target)
xmlParameters = new XML(loader.data);
xmlStoryMap = xmlParameters.projects;
initializeMap();
}
protected function initializeMap():void
{
var testlist:ArrayList = new ArrayList();
testlist.source = convertXMLtoArray(xmlStoryMap.project);
}
private function convertXMLtoArray(file:String):Array
{
var xml:XMLDocument = new XMLDocument(file);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder;
var data:Object = decoder.decodeXML(xml);
var array:Array = ArrayUtil.toArray(data);
return array;
}
If you don't want to have a loop issue, use this instead
protected function xmlloader_onComplete_Handler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target)
var xmlString:String = loader.data;
initializeMap(xmlString);
}
protected function initializeMap(xmlString:String):void
{
var testlist:ArrayList = new ArrayList();
testlist.source = convertXMLtoArray(xmlString);
}
private function convertXMLtoArray(xmlString:String):Array
{
var xmlDoc:XMLDocument = new XMLDocument(xmlString);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
var data:Object = decoder.decodeXML(xmlDoc);
return ArrayUtil.toArray(data.storymap.projects.project);
}
For looping through the projects,
for each(var projectXML:XML in xmlParameters.projects.project)
{
// Do operation
}

Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.SharedObject was unable to invoke callback receiveMessag

I am developing simple chat application, my main class is asac.as. When click send button It gives me exception Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.SharedObject was unable to invoke callback receiveMessag, connection is successful, don't know why error is coming. chat.asc is chat file on server. can anybody tell me what is the problem?
public class ASAC
{
protected var so:SharedObject;
protected var chatStr:String;
protected var nc1:NetConnection;
protected var myResponder:Responder = new Responder(onReply);
protected var chatMsgPanel:TextArea;
public var Vg:Group = new Group()
public function ASAC()
{
super();
}
public function addChatPanel():void{
chatMsgPanel = new TextArea();
chatMsgPanel.width = 240;
chatMsgPanel.height = 240;
chatMsgPanel.x = 2;
var msgInput:TextInput = new TextInput()
msgInput.x = 2;
msgInput.y = chatMsgPanel.height+1;
msgInput.width = 165
var msgSendBtn:Button = new Button()
msgSendBtn.x = msgInput.width + 5;
msgSendBtn.y = chatMsgPanel.height+1;
msgSendBtn.label = "Send"
msgSendBtn.addEventListener(MouseEvent.CLICK, msgSend);
var roomLabel:Label = new Label();
roomLabel.text = "Rooms";
Vg.addElement(chatMsgPanel)
Vg.addElement(msgInput)
Vg.addElement(msgSendBtn);
}
public function chatConnection():void{
nc1 = new NetConnection();
nc1.client = this
nc1.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc1.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
nc1.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler)
nc1.objectEncoding = flash.net.ObjectEncoding.AMF0;
nc1.connect("rtmp:/simple_chat");
SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
so = SharedObject.getRemote("message", nc1.uri, false);
so.client = this;
so.addEventListener(SyncEvent.SYNC, soOnSync);
so.connect(nc1);
}
public function asyncErrorHandler(event:AsyncErrorEvent):void
{
Alert.show("========================= asyncErrorHandler"+"\r");
//ignore
}
private function soOnSync(event:SyncEvent):void
{
chatMsgPanel.text += "soOnSync \n";
for (var prop:String in so.data)
{
chatMsgPanel.text += "prop "+prop+" = "+so.data[prop]+"\n";
}
}
public function netStatusHandler(event:NetStatusEvent):void
{
chatMsgPanel.text += "netStatusHandler";
for ( var prop:String in event.info)
{
chatMsgPanel.text += "prop : "+prop+" = "+event.info[prop]+"\n";
}
chatMsgPanel.text += event.info.code+"\n\n";
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
chatMsgPanel.text += "securityErrorHandler \n";
for (var prop:String in event)
{
chatMsgPanel.text += "prop "+prop+" = "+event[prop]+"\n";
}
}
public function msgSend(e:Event):void{
nc1.client = this;
so.client = this;
nc1.call("chat.sendMessage", myResponder, chatMsgPanel.text);
}
private function onReply(result:Object):void
{
if (!result) return;
chatMsgPanel.text += "onReply : resultObj = " + result+"\n";
for ( var prop:String in result)
{
chatMsgPanel.text += "prop : "+prop+" = "+result[prop]+"\n";
}
}
}
}
chat.asc on server
try { var dummy = Chat; } catch ( e ) { // #ifndef Chat
/**
* Chat component class
*/
Chat = function()
{
// Get a non persistent shared object for sending broadcasts
this.message_so = SharedObject.get( "message", false );
}
// send a message to all others participating in the chat session
Chat.prototype.sendMessage = function( mesg )
{
this.message_so.send( "receiveMessage", mesg );
}
chat = new Chat();
} // #endif

How to Convert String into ArrayCollection in Flex?

Actually my Flex Application ..Sample code
private var selectedDays:String = null;
protected function selectRepeatedDays(event:MouseEvent):void
{
selectedDays = new String();
if(MON.selected==true)
{
selectedDays += "MONDAY,";
Alert.show("Monday :"+selectedDays);
}
if(TUE.selected==true)
{
selectedDays += "TUESDAY,";
}
if(WED.selected==true)
{
selectedDays += "WEDNESDAY,";
Alert.show("Monday :"+selectedDays);
}
if(THU.selected==true)
{
selectedDays += "THURSDAY,";
}
}
var arr:ArrayCollection = new ArrayCollection();
arr = selectedDays.substr(0, selectedDays.length-1).toString();
Alert.show(arr.lenth)
But it is not convert... the Alert Statement Not Prompt ..
So How to Convert This String into ArrayCollection...
Use the method split to convert the String to Array:
var array:Array = selectedDays.split(",");
Then (if needed yet) add each item of Array to the ArrayCollection:
var arr:ArrayCollection = new ArrayCollection();
for each (var str:String in array) {
arr.addItem(str);
}
use below code snippet to convert String to ArrayCollection
Convert String to Array using split method
var array:Array = selectedDays.split(",");
Convert Array to ArrayCollection
var selectedDaysArr:ArrayCollection = new ArrayCollection(array);

readUTFBytes from remote file

Is it possible to readUTFBytes from a remote file without prompting the user with the download screen?
Thank you.
Sure, use URLLoader.
private var loader:URLLoader;
private var req:URLRequest;
private function init():void {
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
req = new URLRequest("..."); //url
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(req);
}
private function onComplete(event:Event):void {
ByteArray(loader.data).readUTFBytes(100);
}

(flex mobile)how to add element in the my custom layout

this is my code:
public class my_Layout extends LayoutBase
{
public function my_Layout(){
super();
var b:Button = new Button;
b.label="my button"
addChild(b)
}
}
but it show error , i the method addChild is undefined ,
so waht can i do , thanks
private function get_list():List{
var list:List = target.parent.parent.parent.parent as List;
return list;
}
private function get_document():View{
var list:List = get_list();
return list.document as View;
}
var list:List = get_list();
var document:View = get_document();
var b:Button = new Button;
b.label = 'wwwwwwwwwwwwwwwwwwwwww';
document.addElement(b);

Resources