Browse > Home > Archive by category 'Flash CS3/AS3'

| Subcribe via RSS

Camera.getCamera()的Bug

九月 8th, 2008 | No Comments , 1,374 views | Posted by flashlizi in Flash CS3/AS3

帮助文档上说:“如果 getCamera() 返回 null,则表明摄像头正由另一个应用程序使用,或者系统上没有安装摄像头。 ”而实际上是,只要系统上安装了摄像头,getCamera()都能返回摄像头,而不是返回null。搜索发现已经有人把这个bug报告给了adobe,不过到了flash player 10 依然没有修正。

另外,帮助文档说使用names.length来确定是否安装了任何摄像头,其实这个也是不能完全信任的。一个明显的bug就是不管你用getCamera()或是names.length都无法在Firefox中准确确定是否安装了摄像头,在安装了摄像头驱动但却没有连上摄像头或临时拔掉摄像头的情况下,上面两种方法都会返回已安装摄像头。不过,在IE下是正常的。

还有,如果用户安装了电视捕捉卡之类的,同样有可能被识别为camera对象,但实际上是无效的camera。

这里还有一些在使用camera的时候值得注意的问题:

Tip 1.

When initiating a connection to the Camera it’s best to do this through the Microphone class. Sounds odd I know! The reason for this is when this code is called :

var camera:Camera = Camera.getCamera();
ns.attachCamera(camera);

there’s a hang just after the allow button is selected on the security dialog panel. This is rationalled in the Adobe Flash help as ‘Scanning the hardware for cameras takes time’. So it’s best to trigger the security panel via

var microphone:Microphone = Microphone.getMicrophone();
ns.attachAudio(microphone);

then listen for the Status Event of UnMuted or later in your application call

var camera:Camera = Camera.getCamera();
ns.attachCamera(camera);

There will always be a delay on this code ns.attachCamera(camera); but at least via activating the Microphone first you won’t get a bug like delay of the security panel not disappearing immediately after the allow button is pressed.

Tip 2.

When recording to a Flash Media Server make sure the camera has activity via the Activity Status Event before publishing the stream. Otherwise you may get a static or black frame at the beginning of the recorded stream.

Tip 3.

To disable/turn off the Camera after a recording is complete do so via :

ns.attachCamera(null);

Though this is actually documented for AS3 it wasn’t in AS2. Once this has been done the Camera will need to be reconnected for a new recording and users will experience this delay again as the Camera starts as mentioned above.

Tip 4.

When embedding your Flash do not change the wmode from default. Otherwise you will get problems on specific browser configurations i.e. Firefox PC . These problems include that the allow button on the security dialog box will not hide on click.

Tip 5.

When detecting wether the user has a web camera you can’t rely on Camera.names.length(). This is because there are scenarios whereby devices will appear in the list which may not be webcameras but devices like TV capture cards and can not be used as a camera. The solution for this is when the camera is attached via

var camera:Camera = Camera.getCamera();
ns.attachCamera(camera);

Then add a time out catch which can be cleared via the camera’s activity event, so that if camera activity occurs within the time out of say 5 seconds then clear the timeout. Otherwise if the timeout happens handle the error scenario.

Tip 6.

Further to the above it’s good at the point of displaying the error message to give the user an option to fix this problem. This is because it may simply be a matter of the user changing the default camera selected. This can be done via adding a button and firing the Camera devices security dialog by the following :

Security.showSettings(SecurityPanel.CAMERA);

It’s interesting to know that when the user is selecting options from the Camera devices list it’s possible to detect wether the option selected is actually a Camera. Once again this is done via camera.activityLevel>0 on the Camera’s activity event. Sadly there is not yet a reliable way to detect when the dialog panel close event occurs. So with this in mind when the user has picked a camera, you should make a noticible visual change somewhere behind the dialog box (and overlay screen) so the user will proceed to the close button – happy days.

慎用正则表达式RegExp

八月 7th, 2008 | No Comments , 770 views | Posted by flashlizi in Flash CS3/AS3

AS3中的正则表达式效率并不高,在很多时候如果用其他方法能实现的话尽量不要用正则,所以String的replace、search、match如果有替换方法的话尽量不要用它们,比如用split&join方法代替一般replace。当然在一些用普通方法不好实现的复杂问题中用正则还是很好的。

比如下面的例子:
var str:String="i love flash-123";
test1(); //replace test1: 330 iloveflash
test2(); //strip test2: 67 iloveflash
function test1():void
{
var time=getTimer();
for(var i = 0; i < 1e4; i++)
{
str.replace(/\W|[0-9]|_/g, "");
}
trace("replace test1:", (getTimer()-time), str.replace(/\W|[0-9]|_/g, ""));
}
function test2():void
{
var time=getTimer();
for(var i = 0; i < 1e4; i++)
{
strip(str);
}
trace("strip test2:", (getTimer()-time), strip(str));
}
function strip(str:String):String
{
var newstr:String = "";
for (var i:int = 0, len:int = str.length; i < len; i++)
{
var char:String = str.charAt(i);
if (char >= “a”; && char <= "z") newstr += char;
}
return newstr;
}

AS3 Component: RichTextField

六月 25th, 2008 | 4 Comments , 1,812 views | Posted by flashlizi in Flash CS3/AS3

Just a very simple demo, welcome any feedback (bug or suggestion).

Demo Update:06-27
like MSN or QQ, you can input any content with smileys (click smileys to input) , then send it to the output window.

Official Flash Player 10(Astro) Documentation

五月 22nd, 2008 | No Comments , 544 views | Posted by flashlizi in Flash CS3/AS3

Download: Official Flash Player 10 Documentation
Extended read:
Flash Player 10 Drawing API

让timer事件不延迟第一次调度

四月 23rd, 2008 | No Comments , 1 views | Posted by flashlizi in Flash CS3/AS3

一般启动Timer对象创建计时器后,都要经过Timer.delay的延迟后才会第一次调度timer事件。不过有些时候,我们想不经过delay就第一次调度timer事件,因此我们可以扩展Timer类来实现:

package {
import flash.utils.Timer;
import flash.events.TimerEvent;
public class MyTimer extends Timer {
private var startDelay:Boolean;
public function MyTimer(delay:Number, repeatCount:int = 0, startDelay:Boolean = true) {
this.startDelay = startDelay;
super(delay, repeatCount);
}
public override function start():void {
if (!startDelay) dispatchEvent(new TimerEvent(TimerEvent.TIMER));
super.start();
}
}
}

使用方法:
//传入startDelay为false即不延迟调度timer事件
var timer:MyTimer = new MyTimer(1000, 5, false);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

需要注意的是,不延迟第一次调度timer事件不会计入到计时器的总运行次数repeatCount的。即调度完第一次timer事件后,currentCount仍为0。

Batch convert files to Flashpaper2

三月 18th, 2008 | No Comments , 578 views | Posted by flashlizi in Flash CS3/AS3

Command:
FlashPrinter.exe {PATH TO DOCUMENT} -o {OUTPUT PATH & FILE with SWF extention}
Example:
FlashPrinter.exe riaidea.doc -o riaidea.swf

AS3 Library: ZipArchive 0.1 Release

三月 5th, 2008 | 7 Comments , 1 views | Posted by flashlizi in Flash CS3/AS3

ZipArchive是一个Zip档案处理类,可读写各种zip格式文件。(update version 0.11)

基本介绍:
1)轻松创建或加载一个zip档案;
2)多种方式读取和删除zip档案中的文件;
3)支持中文文件名;
4)非常容易序列化一个zip档案,如有AIR、PHP等的支持就可以把生成的zip档案保存在本地或服务器上。

更新:此项目已更新至0.2版本,请大家到http://code.google.com/p/as3-ziparchive/去下载最新的源码和示例。
下载:源文件+API文档+示例
查看:API文档

读取zip文件可查看下载包中的example示例。保存zip文件部分可参考我以前发布的AirZip功能演示。

下面是下载包中的example示例的部分代码:

public class Example extends Sprite {
private var zip1:ZipArchive = new ZipArchive();
public function Example() {
//加载一个zip档案
zip1.load(“test.zip”);
zip1.addEventListener(ProgressEvent.PROGRESS, loading);
zip1.addEventListener(ZipEvent.ZIP_INIT, inited);
zip1.addEventListener(ZipEvent.ZIP_FAILED, failed);
zip1.addEventListener(IOErrorEvent.IO_ERROR, ioError);
}
private function inited(evt:ZipEvent):void {
zip1.removeEventListener(ProgressEvent.PROGRESS, loading);
zip1.removeEventListener(ZipEvent.ZIP_INIT, inited);
zip1.removeEventListener(ZipEvent.ZIP_FAILED, failed);
//添加ZIP_CONTENT_LOADED事件侦听器
zip1.addEventListener(ZipEvent.ZIP_CONTENT_LOADED, imgloaded);
trace(“原始zip文件内容\r”, zip1);
//读取zip1中的xml文件
var xmlFile:ZipFile = zip1.getFileByName(“sample.xml”);
var xml:XML = new XML(xmlFile.data);
trace(xml);
//根据字符串内容创建一个新的txt文件
var txtContent:String = “这是一个测试文本文件”;
zip1.addFileFromString(“测试.txt”, txtContent);
//trace(zip1.getFileByName(“测试.txt”).data);
//复制zip1中的girl.jpg为张曼玉.jpg
var zmy:ZipFile = zip1.getFileByName(“girl.jpg”);
zip1.addFileFromBytes(“张曼玉.jpg”, zmy.data);
//加载zip1中的新生成的图片文件的Bitmap对象
zip1.getBitmapByName(“张曼玉.jpg”);
//删除图片文件logo.gif
zip1.removeFileByName(“logo.gif”);
trace(“\r修改后的zip文件内容\r”, zip1);
}
private function imgloaded(evt:ZipEvent):void {
zip1.removeEventListener(ZipEvent.ZIP_CONTENT_LOADED, imgloaded);
var img:Bitmap = evt.content as Bitmap;
addChild(img);
}
private function loading(evt:ProgressEvent):void {
//trace(evt.currentTarget, evt.bytesLoaded, evt.bytesTotal);
}
private function failed(evt:ZipEvent):void {
//trace(evt.content);
}
private function ioError(evt:IOErrorEvent):void {
//trace(evt);
}
}

一个命名空间namespace应用演示

二月 20th, 2008 | No Comments , 588 views | Posted by flashlizi in Flash CS3/AS3

这是一个用命名空间来控制名称相同的方法的不同实现的例子。三个命名空间string、number、array下均有一个名为print的方法,它们分别实现打印字符串、数字、数组的具体方法。有时候用命名空间也是实现方法重载的一种不错的办法。

演示代码如下:

  1. package {
  2. import flash.display.Sprite;
  3. public class NameSpaceExam extends Sprite{
  4. //打印字符串的NS
  5. private namespace string;
  6. //打印数字的NS
  7. private namespace number;
  8. //打印数组的NS
  9. private namespace array;
  10. //构造函数
  11. public function NameSpaceExam() {
  12. var str:String = "www.riaidea.com";
  13. var num:Number = 200802201507;
  14. var arr:Array = ["flash cs3", "flex3.0", "sliverlight 1.0", 2008, true];
  15. trace(string::print(str));
  16. trace(number::print(num,","));
  17. trace(array::print(arr));
  18. }
  19. //打印字符串
  20. string function print(str:String):String {
  21. return "String: " + str;
  22. }
  23. //打印数字,3节分段计数形式
  24. number function print(num:Number,sep:String=","):String {
  25. var str:String=String(num);
  26. var new_str:String="";
  27. var len:int=str.length;
  28. while(len>3){
  29. var tmp:String=str.slice(len-3);
  30. str=str.substring(0,len-3);
  31. new_str=sep+tmp+new_str;
  32. len=str.length;
  33. }
  34. return "Number: "+str+new_str;
  35. }
  36. //打印数组,枚举每个数组元素
  37. array function print(arr:Array):String {
  38. var str:String = "Array: \r";
  39. for (var i:int = 0, l:int = arr.length; i < l; i++) {
  40. str += "["+i+"]-> "+arr[i].toString()+"\r";
  41. }
  42. return str;
  43. }
  44. }
  45. }

输出结果为:

  1. String: www.riaidea.com
  2. Number: 200,802,201,507
  3. Array:
  4. [0]-> flash cs3
  5. [1]-> flex3.0
  6. [2]-> sliverlight 1.0
  7. [3]-> 2008
  8. [4]-> true

网页中flash对象ID命名错误造成ExternalInterface失效

二月 20th, 2008 | No Comments , 542 views | Posted by flashlizi in Flash CS3/AS3

今天做个小测试的时候,在flash cs3中按F12预览,发现ExternalInterface在IE下失效,而Firefox却很正常。以前做过类似的都没出现过问题。

经过排查,终于发现罪魁祸首是id="Untitled-1",把id中的连字符号(hyphen)去掉就OK了。大家都知道flash默认文件名为:Untitled-X.swf,测试也就没去修改。这样看来IE中的命名对连字符号"-"也是敏感的,最好避免在命名中使用到它。

IE7/Firefox阻止navigateToURL打开新窗口的解决方法

二月 13th, 2008 | No Comments , 931 views | Posted by flashlizi in Flash CS3/AS3

新年第一篇文章,首先给大家拜个晚年:)。今天上班终于想起要解决这个问题了。IE7和Firefox(我使用的版本是2.0.0.11)会阻止用navigateToURL方法打开新窗口,而AS2中的getURL方法则不会,让人很不爽。既然项目选择了AS3开发,就只能想办法来解决。

首先当然想到的是ExternalInterface了,测试发现还是会被blocked。后来想添加wmode会不会有所帮助,于是在页面中添加wmode属性为opaque,果然OK了。

现提供AS3中的getURL方法:

使用方法跟AS2中的getURL一样。另外,我只测试了IE6/7,Firefox2,并未对Safari等其他浏览器做测试。最后,最最重要的就是在html中把flash对象设置wmode属性为opaque或transparent。因为wmode属性默认为window,这表明此Flash应用程序与HTML层没有任何交互。