VCASMO

Flash


Before AS3, we can only use substr to abbreviate string characters number for TextField, but we have to guess a extract number to fit our UI design, e.g. only show two lines, we have to guess in this font size, font face, what is maximum numbers of character can be shown. It is a problem which is hard to solve for a long time.

When come to AS3, TextField class have a new method call getLineLength(), it can return each line, number of characters are shown. So I have written this small class to help me limit the number of rows can be shown in TextField

Download MaxRowsTextField class file
Download demo source

Usage
This class is extended from TextField class:

var tf:MaxRowsTextField = new MaxRowsTextField();
tf.width = 290;
tf.multiline = tf.wordWrap = true;

// 0 mean unlimited, default is 0
tf.maxRows = 4;

// change abbreviate symbol, default is "…"
tf.moreStr = "…(more) ";

// if you want to limit rows count, use this, otherwise, you can just use text or htmlText
tf.text2 = "This is a vey long message";

This class is extracted and simplified from my VCASMO 2.0 project.

Thanks Flash Player 10.1, now we can record the sound through microphone directly in browser without the use of Flash Media Server, you can download the wav file converted from ByteArray immediately, you can download mp3 which I send wav file to server and using LAME to encode as mp3. Hope AS3 mp3encoder can come out in one day, then a pure client side microphone recording dream will come true since Flash Player 6!

[Demo]

To learn how to use microphone to record sound, you can check this devnet article.

To save bandwidth, most of servers will practice some hotlink protection, usually using either following script to check whether the HTTP referer is come from the same server, most script are focus on image file, but it can slightly modified for FLV protection. There are two common methods to do that:

Edit in .htaccess

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(flv|mp3)$ - [NC,F,L]

Edit in httpd.conf, first enabled this:

LoadModule setenvif_module modules/mod_setenvif.so

then put it inside <VirtualHost>, just before </VirtualHost>

SetEnvIfNoCase Referer “^http(s)?://(www\.)?yourdomain.com(/|$)” local_ref=1
SetEnvIfNoCase Referer “^$” local_ref=1
<FilesMatch “\.(flv|mp3)”>
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>

However, FLV need to play through Flash Player, different browsers and even minor versions handle plugin HTTP referer differently, so it make such hotlink protection through HTTP referer means impossible :(

Browser sends the URL of the requesting plug-in (i.e. the best situation, url of SWF) does not send a referer sends the embedding page URL
IE X    
Firefox   X  
Safari (MAC)     X
Safari (Win)   X  
<Chrome 3     X
Chrome 3+ X    
<=Opera 9     X
Opera 10+ X    



The hotlink protection cause problem when you let other people embed your video player in their site (like Youtube do), from the above table, you can find the third column will not work, for example in safari (MAC), the video cannot play because Apache think the FLV is linked by third party website and forbid FLV to be accessed.

Detect Flash Player version:

Detect SWF version:

If you use POST method to send parameters, the received result is XML, you should use LoadVars or XML? sendAndLoad from LoadVars cannot handle received XML format, sendAndLoad from XML can use GET method to send parameters only, cannot use POST Method. I have already sticked to write AS3, suddenly switch back to AS1, I forgot how to code, googling (cannot find any useful tutorial). Finally, I find out by myself: LoadVars’ sendAndLoad can assign receive Object, usually assign itself, so never consider it can be others before, that is XML Object, ha!

login_lv = new LoadVars();
login_xml = new XML();
login_xml.ignoreWhite = true;
login_xml.onLoad = function(suc) {
        // parse XML result here
};
login_lv.loginname = loginName_tf.text;
login_lv.password = pwd_tf.text;
login_lv.sendAndLoad("login.php", login_xml, "POST");

Designers always face this problem, inside a MovieClip, there are different symbols, when MovieClip is semi-transparent, each symbol has its own semi-transparent, so the colors are mixed together as the following example:

To solve this problem you can set MovieClip BlendMode to Layer

ActionScript syntax:

mc.blendMode = "layer";

(For Flash 8 AS1/2, too)

Download sample fla(Flash CS3)

When loading XML File and parsing, usually use ChildNode[n].toString() for extract the data, sound like it can get the data and convert to “String” or other variable type. Normally, it has no problem, but when the XML contains special characte, it will get back wrong data, for example special characters like: ‘, &, ". Here is an example, let say being loaded XML File is:

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<data>
  <sentence><![CDATA[Luar's "Friends" & Friends]]> </data>

ActionScript:

var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function() {
  var node:XMLNode = this.firstChild.childNodes[0];
  trace(node.childNodes[0]);
  trace(node.childNodes[0].toString());
  trace(node.childNodes[0].nodeValue);
};
xml.load("sample.xml");

trace result:

Luar&apos;s &quot;Friends&quot; &amp; Friends
Luar&apos;s &quot;Friends&quot; &amp; Friends
Luar’s "Friends" & Friends

So, the safest way to do so should be using nodeValue.

If use AS3 and E4X, no such problem:

var xml:XML = new XML();
var xmlFile:String = "sample.xml";
var urlReq:URLRequest = new URLRequest(xmlFile);
var urlLoader:URLLoader = new URLLoader(urlReq);
urlLoader.addEventListener("complete", xmlLoaded);
function xmlLoaded(event:Event):void {
  xml = XML(urlLoader.data);
  trace(xml.sentence.toString());
}

trace result:

Luar’s "Friends" & Friends

[Download Fla]

Although and, or those ActionScript are deprecated by Adobe, I still love to use it, because it is defaultly support as Keyword in Action panel with color syntax, easy to spot out. Also, the bytecode compiled, it is smaller than &&, ||. You can compare the following two code snippets:

if (a and b) {
        trace(123);
}
// file size: 73 bytes
if (a && b) {
        trace(123);
}
// file size: 79 bytes

When you are working on file size sensitive work, e.g. Flash banner, try to use deprecated ActionScript as one of method to reduce the swf file size.

I have a project need to use FSCommand communicate with JavaScript for Flash Player 7 or below, so I downloaded archived Flash Players and test again. I find FSCommand can work in Firefox (1.5 here), it requires at least Flash Player 6r47, older version cannot work. :(

I have installed the lastest Flash Player 9 in Mac(PPC), but I cannot watch any Flash 9 works, e.g. those entries in Flex Derby, my own Flash 9 works in Mac IE (No problem in Safari, Firefox, Netscape and no problem for Mac IE to view Flash 8 content). It just show a blank white screen and when I “right click”, it said “Movie cannot load”.macie_fp9_01.jpg
macie_fp9_02.jpg
Although I am not a Mac IE user and M$ had stopped Mac IE already, but I just want to make sure Flash 9 is an ideal solution for deploy truely cross platform, cross browser application.

I guess, is it the AC_OETags.js not work in Mac IE?

Older »