FileReference and registerClassAlias - apache-flex

I'm trying to use the native flex serialization/deserialization process to save/load state of a workspace in flex. In order to save or load a file I have to use my servlet, which just bounces back the bytes from the input stream to the output stream. Here's a basic outline of my Flex code:
Serialized Object Container:
public class MyWorkspace {
public var id : String;
public var url : String;
public var objectCollection : ArrayCollection; // Contains MySubObjects
}
Serialized sub object:
public class MySubObject
{
public var name:String;
public var csv:String;
}
Visual Element Constructor:
public function VisualSandbox(){
registerClassAlias("MyWorkspaceAlias", MyWorkspace);
registerClassAlias("MySubObjectAlias", MySubObject);
}
Visual Element Event Handlers:
public function onSaveButtonClick(event : MouseEvent) : void
{
var ws : MyWorkspace = new MyWorkspace();
ws.id = "ID";
ws.url = "URL";
ws.objectCollection = new ArrayCollection(veObjCollectionAC.source.slice());
var ba : ByteArray = new ByteArray();
ba.writeObject(ws);
ba.position = 0;
var fr : FileReference = new FileReference();
// There's no need for me to put the rest so
...
fr.download(urlRequest, "workspace.ws");
}
public function onLoadButtonClick(event : MouseEvent) : void
{
veFileReference = new FileReference();
veFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, loadCompleteHandler);
// This part works as well so I'm skipping it.
}
public function loadCompleteHandler(event : DataEvent) : void
{
// Pretend I loaded "workspace.ws" by bouncing it off of my servlet byte for byte.
var ba : ByteArray = new ByteArray();
ba.writeObject(event.data);
ba.position = 0;
var obj : * = ba.readObject();
trace(obj is MyWorkspace); // Prints false
}
So my problem here is not with the saving of the workspace. That works great. My problem is with the loading of a workspace. The event.data that I write to the byte array is not reconstituted into a MyWorkspace object even though it is exactly what I wrote to the file.
There has to be a way of making a MyWorkspace object from the data, but I can't figure it out. Has anyone tried to do this before?
Thanks!

I've figured out what the problem here is. It specifically has to do with the line:
ba.writeObject(event.data);
The data field of the event, being a DataEvent, is specifically of type String. ByteArray.writeObject() puts a type code at the beginning of the byte array. This took me many hours of looking at Hexplorer to figure out why, exactly, the byte array had 2-3 extra characters at its beginning.
My current best solution for this is to change that line as follows:
for (var i : int = 0; i < event.data.length; ++i)
{
ba.writeByte(event.data.charCodeAt(i));
}
This ensures that the byte array is exactly the same as what was saved to my local drive and bounced off my servlet.
After doing this, trace(obj is MyWorkspace) prints true.
Hope this helps someone else in the future!

Have you tried casting the value?
var obj : MyWorkspace = ba.readObject() as MyWorkspace;

Related

Save object list in Xamarin Essentials

my first question is here
however since I was advised that questions should not change the original matter I created a new one.
I am saving user settings and I would like to save it in the list, I have had a look on setting by James however I found that that its not possible to save it in the list. So ia have decided to use Xamarin Essentials.
First I tried to save only a string value, which after some struggle I managed to work out and now I am trying to save an object
static void AddToList(SettingField text)
{
var savedList = new List<SettingField>(Preference.SavedList);
savedList.Add(text);
Preference.SavedList = savedList;
}
private void ExecuteMultiPageCommand(bool value)
{
var recognitionProviderSettings = new RecognitionProviderSettings
{SettingFields = new List<SettingField>()};
var set = new SettingField()
{
ProviderSettingId = "test",
Value = "test"
};
AddToList(set);
NotifyPropertyChanged("IsMultiPage");
}
and then the sterilization and des
public static class Preference
{
private static SettingField _settingField;
public static List<SettingField> SavedList
{
get
{
//var savedList = Deserialize<List<string>>(Preferences.Get(nameof(SavedList), "tesr"));
var savedList = Newtonsoft.Json.JsonConvert.DeserializeObject<SettingField>(Preferences.Get(nameof(SavedList), _settingField)) ;
SavedList.Add(savedList);
return SavedList ?? new List<SettingField>();
}
set
{
var serializedList = Serialize(value);
Preferences.Set(nameof(SavedList), serializedList);
}
}
static T Deserialize<T>(string serializedObject) => JsonConvert.DeserializeObject<T>(serializedObject);
static string Serialize<T>(T objectToSerialize) => JsonConvert.SerializeObject(objectToSerialize);
}
}
But Preferences.Get doesn't take object, is there any other way how can I save my setting to a object list? Please advise
I would recommend you to use SecureStorage. You can save your strings only into it. So the place where you have serilized your object as json. Just convert your json to string with .ToString() and save it into secure storage.
You may continue saving your serialized json object as string in Shared preferences but it is recommended to use SecureStorage Instead.

Flex: how to change the volume of an EMBEDDED sound?

Searching for how to change the volume of sounds, I always get this irritating snd=new Sound(URLRequest), followed by snd.setVolume(val). Oh, fine, but my sound is not an URLRequest, it's an embed.
I've made a lot of random attempts (1) to no avail. How am I supposed to do that instead?
(1) including casting my class to Sound, creating a Sound with the embed class as argument, creating a SoundTransform and setting it to the channel, etc.
Instantiate your embedded class like this:
[Embed(source="MySound.mp3")]
public var soundClass:Class;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var smallSound:Sound = new soundClass() as Sound;
var trans:SoundTransform = new SoundTransform(.01);
smallSound.play(0,0,trans);
}
Update:
In case what you really wanted to know was how to change the volume if the sound was already playing:
[Embed(source="MySound.mp3")]
public var soundClass:Class;
public var smallSound : Sound;
public var vol : Number = 0.01;
public var trans : SoundTransform;
public var chan : SoundChannel = new SoundChannel();
protected function application1_creationCompleteHandler(event:FlexEvent):void {
smallSound = new soundClass() as Sound;
trans = new SoundTransform(vol);
chan = smallSound.play(0,0,trans);
}
protected function volUp_clickHandler(event:MouseEvent):void {
vol += .1;
trans = new SoundTransform(vol);
chan.soundTransform = trans;
}

How to trigger a function only once in case of a mouseEvent

I am trying to make a simple mp3 player using flash. The songs are loaded using an XML file which contains the song list. I have "play" button with the instance name "PlayBtn". I have an actionscript file named "playctrl", the content of which are listed below:
package classes
{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLRequest;
public class playctrl
{
private var MusicLoading:URLRequest;
private var music:Sound;
private var sc:SoundChannel;
private var currentSound:Sound;
private static var CurrentPos:Number;
private var xml:XML;
private var songlist:XMLList;
private static var currentIndex:Number;
public function playctrl()
{
music = new Sound();
currentSound= music;
CurrentPos = 0;
currentIndex = 0;
}
public function success(e:Event):void
{
xml = new XML(e.target.data);
songlist = xml.song;
MusicLoading = new URLRequest(songlist[0].file);
music.load(MusicLoading);
}
public function playSong(e:Event):void
{
if(sc != null)
sc.stop();
sc = currentSound.play(CurrentPos);
trace("HELLO !!!");
}
}
}
I have a second file named "play.as", the content of which is listed below:
import classes.playctrl;
var obj:playctrl = new playctrl();
var XMLLoader:URLLoader = new URLLoader(); //XML Loader
XMLLoader.addEventListener(Event.COMPLETE, obj.success);
XMLLoader.load(new URLRequest("playlist.xml"));
PlayBtn.addEventListener(MouseEvent.CLICK, obj.playSong);
However on clicking the play button, I notice that the function playSong() is called 7-8 times(check by printing an error msg. inside the function) resulting in overlapped audio output and the player crashing as a result. The function should be called only once when the MouseEvent.CLICK is triggered. Please help ...
interestingly, sound object doesn't have a built-in "isPlaying" boolean property (strange), so you could just create your own.
var isPlaying:Boolean
function playSong():void
{
if(!isPlaying)
sound.play();
}
function stopSong():void
{
if(isPlaying)
{
channel.stop();
isPlaying = false;
}
just a note: by convention, class names are capitalized camel case while instance names are uncapitalized camel case. so your playctrl.as class file should (or could) be PlayCtrl.as, and your PlayBtn instance should (or could) be playBtn.
Edit:
The title of your question is a bit misleading, the answer I gave you is a solution to the question expressed in the title.
Looking at your code, I would look at separating the concerns, on one hand you want to load the song data, on the other hand you want to control the sounds. I would implement separate classes for each concern. If you create a separate class for your player control, you'll be able to dispatch event within that class without the event bubbling all over your app and calling your functions several times.
//Previous answer
You could do this by implementing a Boolean that would be set when the sound is stopped or played.
In any case here's another way to filter unwanted clicks
private function playSong(event:MouseEvent ):void
{
// set up a conditional to identify your button ,
// here's an example...
if( event.currentTarget.name is "PlayBtn" )
{
//do whatever
//then...
event.stopImmediatePropagation();
}
}
This being said, in your case , it sounds like a bit of a quick fix since a MouseEvent shouldn't trigger the play function several times...
It would make sense to debug your code in order to understand why several events are dispatched after a Mouse click
private var _isPlaying:Boolean;
public function playSong(e:Event):void
{
if(sc != null)
{
sc.stop();
_isPlaying = false;
}
if( !_isPlaying )
{
sc = currentSound.play(CurrentPos);
_isPlaying = true;
trace("HELLO !!!");
}
}

Pause and resume download in flex?

Is it possible in an air application to start a download, pause it and after that resume it?
I want to download very big files (1-3Gb) and I need to be sure if the connection is interrupted, then the next time the user tries to download the file it's start from the last position.
Any ideas and source code samples would be appreciated.
Yes, you would want to use the URLStream class (URLLoader doesn't support partial downloads) and the HTTP Range header. Note that there are some onerous security restrictions on the Range header, but it should be fine in an AIR application. Here's some untested code that should give you the general idea.
private var _us:URLStream;
private var _buf:ByteArray;
private var _offs:uint;
private var _paused:Boolean;
private var _intervalId:uint;
...
private function init():void {
_buf = new ByteArray();
_offs = 0;
var ur:URLRequest = new URLRequest( ... uri ... );
_us = new URLStream();
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
...
private function partialLoad():void {
var len:uint = _us.bytesAvailable;
_us.readBytes(_buf, _offs, len);
_offs += len;
if (_paused) {
_us.close();
clearInterval(_intervalId);
}
}
...
private function pause():void {
_paused = true;
}
...
private function resume():void {
var ur:URLRequest = new URLRequest(... uri ...);
ur.requestHeaders = [new URLRequestHeader("Range", "bytes=" + _offs + "-")];
_us.load(ur);
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
if you are targeting mobile devices, maybe you should take a look at this native extension: http://myappsnippet.com/download-manager-air-native-extension/ it supports simultaneous resumable downloads with multi-section chunks to download files as fast as possible.

Is it possible to define a generic type Vector in Actionsctipt 3?

Hi i need to make a VectorIterator, so i need to accept a Vector with any type. I am currently trying to define the type as * like so:
var collection:Vector.<*> = new Vector<*>()
But the compiler is complaining that the type "is not a compile time constant". i know a bug exists with the Vector class where the error reporting, reports the wrong type as missing, for example:
var collection:Vector.<Sprite> = new Vector.<Sprite>()
if Sprite was not imported, the compiler would complain that it cannot find the Vector class. I wonder if this is related?
So it looks like the answer is there is no way to implicitly cast a Vector of a type to valid super type. It must be performed explicitly with the global Vector.<> function.
So my actual problem was a mix of problems :)
It is correct to use Vector. as a generic reference to another Vector, but, it cannot be performed like this:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = spriteList // this will cause a type casting error
The assignment should be performed using the global Vector() function/cast like so:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = Vector.<Object>(spriteList)
It was a simple case of me not reading the documentation.
Below is some test code, I would expect the Vector. to cast implicitly to Vector.<*>.
public class VectorTest extends Sprite
{
public function VectorTest()
{
// works, due to <*> being strictly the same type as the collection in VectorContainer
var collection:Vector.<*> = new Vector.<String>()
// compiler complains about implicit conversion of <String> to <*>
var collection:Vector.<String> = new Vector.<String>()
collection.push("One")
collection.push("Two")
collection.push("Three")
for each (var eachNumber:String in collection)
{
trace("eachNumber: " + eachNumber)
}
var vectorContainer:VectorContainer = new VectorContainer(collection)
while(vectorContainer.hasNext())
{
trace(vectorContainer.next)
}
}
}
public class VectorContainer
{
private var _collection:Vector.<*>
private var _index:int = 0
public function VectorContainer(collection:Vector.<*>)
{
_collection = collection
}
public function hasNext():Boolean
{
return _index < _collection.length
}
public function get next():*
{
return _collection[_index++]
}
}
[Bindable]
public var selectedItems:Vector.<Category>;
public function selectionChange(items:Vector.<Object>):void
{
selectedItems = Vector.<Category>(items);
}
I believe you can refer to an untyped Vector by just calling it Vector (no .<>)
With Apache Flex 4.11.0, you can already do what you want. It might have been there since 4.9.0, but I have not tried that before.
var collection:Vector.<Object> = new Vector.<Object>()
maybe?
But i'm just speculating, haven't tried it.
var collection:Vector.<Object> = new Vector.<Object>()
but only on targeting flash player 10 cs4

Resources