Tải bản đầy đủ (.pdf) (104 trang)

whats new in adobe air 3

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.88 MB, 104 trang )

Quickstart Guide for Desktop and Mobile Development
Joseph Labrecque
Adobe AIR 3
What’s New in
www.it-ebooks.info
www.it-ebooks.info
What's New in Adobe AIR 3
Joseph Labrecque
Beijing

Cambridge

Farnham

Köln

Sebastopol

Tokyo
www.it-ebooks.info
What's New in Adobe AIR 3
by Joseph Labrecque
Copyright © 2012 Fractured Vision Media, LLC. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or
Editor: Mary Treseler
Production Editor: Dan Fauxsmith
Proofreader: O'Reilly Publishing Services


Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Revision History for the First Edition:
See for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. The image of the Arched or Whistling Duck and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-31108-7
[LSI]
1323097087
www.it-ebooks.info
Adobe Developer Library, a copublishing partnership between O’Reilly Media Inc.,
and Adobe Systems, Inc., is the authoritative resource for developers using Adobe
technologies. These comprehensive resources offer learning solutions to help devel-
opers create cutting-edge interactive web applications that can reach virtually any-
one on any platform.
With top-quality books and innovative online resources covering the latest tools for
rich-Internet application development, the Adobe Developer Library delivers expert
training straight from the source. Topics include ActionScript, Adobe Flex®, Adobe
Flash®, and Adobe Acrobat®.
Get the latest news about books, online resources, and more at http://adobedeveloper
library.com.
Untitled-1 1 3/3/09 5:37:20 PM

www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Improvements to the MovieClip and Drawing APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Cubic Bezier Curves 1
DisplayObjectContainer.removeChildren() 3
MovieClip.isPlaying 5
2.
External Image Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Enhanced High-Resolution Bitmap Support 9
JPEG-XR Support 11
3. Stage3D: High Performance Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Stage3D Accelerated Graphics Rendering 15
Elements of Stage3D 16
Stage3D Example Using Away3D 18
Stage3D Example Using Starling 20
Tooling Support for Stage3D 24
4. Mobile Advantage: StageText and StageVideo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
StageText Native Text Input UI (Mobile) 27
StageVideo Hardware Acceleration (Mobile) 30
5. Video and Audio Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
H.264/AVC Software Encoding 35
Encoding H.264 within AIR 3 36
Reading an H.264 Stream into AIR 3 38
G.711 Audio Compression for Telephony 40
6. Mobile Device Hardware Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Camera Position API (Mobile) 45
Device Speaker Control (Mobile) 47
vii

www.it-ebooks.info
Background Audio Playback Support on iOS (Mobile) 49
7. Data Transfer Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Native JSON (JavaScript Object Notation) Support 51
JSON.parse() 52
JSON.stringify() 54
Socket Progress Events 57
8. Runtime Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
ActionScript Native Extensions 61
Captive Runtime Support 67
Android Color Depth Setting (Mobile) 70
Garbage Collection Advice 71
9. Adobe AIR Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Encrypted Local Storage (Mobile) 77
Protected HTTP Dynamic Streaming and Flash Access Content Protection
Support for Mobile 80
Secure Random Number Generator 81
Appendix: Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
viii | Table of Contents
www.it-ebooks.info
Preface
Introduction to Adobe AIR 3
This book will detail the various enhancements, new functionalities, and general im-
provements available in this version of the Adobe AIR runtime. Each item is explained
in detail, and when possible, a series of screen captures and a full code example will be
provided, enabling you to both grasp the new feature in a visual way, and integrate the
feature into your own code quickly, based upon example.
AIR, of course, shares many core functionalities with the Adobe Flash Player. During
the development cycle between Flash Player 10 and Flash Player 10.1, Adobe rewrote
much of the underlying code in order to lay a solid foundation that not only benefited

traditional web experiences, but could also be brought over into new areas such as
mobile and television. This foundation has served to make both Flash Player 10.1–10.3
and AIR 2.5–2.7 very stable while allowing Adobe to begin adding small features upon
each incremental release. In contrast to these incremental versions, with Flash Player
11 and AIR 3 we begin to see the rapid evolution of the Flash Platform runtimes into
something not only great at interactive, gaming, media distribution, and enterprise
applications…but into something that pushes these areas way beyond their previous
limitations.
There is no doubt that mobile application development using the Adobe Flash Platform
has become a topic of increased interest in the application developer communities.
While there are a number of solutions to cross-compile applications to a variety of
mobile platforms using any number of technologies, the ability to do this with such a
proven platform is something that most cannot even hope to match. It is very important
that AIR evolves in a way which not only showcases why it is so relevant in this new
ecosystem, but also why it is (in many cases) the ideal technology platform for advanced
interaction on a multitude of devices. With Adobe ramping up the AIR release schedule
along with more iterative tooling support in Flash Professional and Flash Builder, not
to mention a number of new community partnerships in support of the platform from
both independent framework and third-party tooling support, we can expect great
things in future incremental releases of AIR 3 and within the entire platform ecosystem.
ix
www.it-ebooks.info
Who This Book Is For
This book is written for both veteran Flash Platform developers curious about en-
hancements in Adobe AIR 3, as well as those who are entirely new to the Flash Platform.
The reader will acquire a solid overview of new features along with usable code exam-
ples.
Who This Book Is Not For
This book is not an in-depth study of ActionScript or Adobe AIR internals. Neither is
this meant to be an exhaustive overview of complex new features such as Stage3D or

ActionScript Native Extensions (ANE). Entire books will be written which cover such
advanced topics. This book will simply provide the reader with a holistic foundation
to be built upon using other resources.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter-
mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
x | Preface
www.it-ebooks.info
This Book’s Example Files
You can download the example files for this book from this location:
/>All code examples are written using pure ActionScript 3, when possible, and are not
tied to any framework or IDE. This is to allow the reader to implement the code ex-
amples in whichever environment he/she chooses.
The examples are all ActionScript 3 (AS3) class files which can be compiled to AIR,
APK, EXE, BAR, IPA, et cetera, using Flash Professional, Flash Builder, FDT, Flash-
Develop, or any other IDE which can be configured to process and output Flash con-
tent.
For most of the mobile examples with figures, we are setting the <aspectRatio> node

within the <initialWindow> node in the application descriptor file to “landscape” and
the <autoOrients> node within the <initialWindow> node to “false”. This is not re-
quired, but you may wish to do this yourself when using these examples in order to
produce a similar output as is detailed by the figures present in this book.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does not require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “What's New in Adobe AIR 3 by Joseph
Labrecque (O’Reilly). Copyright 2012 Fractured Vision Media, LLC,
978-1-4493-1108-7.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at
How to Use This Book
Development rarely happens in a vacuum. In today’s world, email, Twitter, blog posts,
co-workers, friends, and colleagues all play a vital role in helping you solve development
problems. Consider this book yet another resource at your disposal to help you solve
the development problems you will encounter. The content is arranged in such a way
Preface | xi
www.it-ebooks.info
that solutions should be easy to find and easy to understand. However, this book does
have a big advantage: it is available anytime of the day or night.
Safari® Books Online
Safari Books Online is an on-demand digital library that lets you easily

search over 7,500 technology and creative reference books and videos to
find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online.
Read books on your cell phone and mobile devices. Access new titles before they are
available for print, and get exclusive access to manuscripts in development and post
feedback for the authors. Copy and paste code samples, organize your favorites, down-
load chapters, bookmark key sections, create notes, print out pages, and benefit from
tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from O’Reilly and other pub-
lishers, sign up for free at .
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
/>To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website
at .
Find us on Facebook: />Follow us on Twitter: />Watch us on YouTube: />xii | Preface
www.it-ebooks.info
Acknowledgments
I’d first like to thank my wife, Leslie, and our daughters, Paige and Lily, for being so
understanding of the work that I do. It’s strange stuff, I know.

Thanks also to Rich Tretola, Chris Griffith, Michelle Yaiser, Brian Rinaldi, Richard
Galvan, O’Reilly Media, Adobe Systems, and the Adobe Education Leader and Adobe
Community Professional organizations.
Preface | xiii
www.it-ebooks.info
www.it-ebooks.info
CHAPTER 1
Improvements to the MovieClip and
Drawing APIs
Adobe AIR shares much of its core functionality with the Adobe Flash Player runtime.
Flash Player began life in the mid-1990s as a web-based media animation and display
technology. For much of its history, it has been relied on for graphically intense, func-
tional, and beautiful image rendering and manipulation. With AIR 3, the graphics and
vector drawing technology which is so core to Flash Player and inherited by AIR is
extended and improved upon in some rather useful ways.
Cubic Bezier Curves
We have an addition to the graphics drawing APIs in this release of AIR which allows
simple creation of Cubic Bezier Curves without having to do a lot of complex equations
on your own, each time you want to draw a new curve. The new cubicCurveTo() method
takes six arguments to function correctly: a set of x and y coordinates for the first control
point, a similar set for the second control point, and a set of coordinates for the anchor
point.
Bezier curves are widely used in computer graphics to model smooth
curves through the use of four distinct points: a start point, an end point,
and two anchor points which inform the direction and pull of the drawn
curve.
The curve will begin wherever the current line is – we can use the moveTo() method to
precisely position the start point just as is done on other graphics API calls. The two
control points influence the curve of the line, and the anchor point will be the end of
the drawn curve. This is illustrated visually in Figure 1-1.

1
www.it-ebooks.info
Figure 1-1. How cubic Bezier curves work
In the example below, we create a Sprite within which the new cubicCurveTo() method
is invoked in order to draw a cubic Bezier arc across the stage.
package {
import flash.display.Sprite;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class CubicBezierCurve extends Sprite {
private var drawingHolder:Sprite;
public function CubicBezierCurve() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
drawingHolder = new Sprite();
drawingHolder.graphics.moveTo(20, stage.stageHeight-20);
drawingHolder.graphics.lineStyle(5,0x000000);
drawingHolder.graphics.cubicCurveTo(50, 50, stage.stageWidth-50, 50,
stage.stageWidth-20, stage.stageHeight-20);
addChild(drawingHolder);
}
2 | Chapter 1: Improvements to the MovieClip and Drawing APIs
www.it-ebooks.info
}
}
This will render an application window similar in appearance to Figure 1-2.
Figure 1-2. Cubic Bezier curve
DisplayObjectContainer.removeChildren()
Previous to AIR 3, if a developer wanted to remove all children from a container object,
it was necessary to first determine how many children were present through DisplayOb

jectContainer.numChildren and then loop over each of these child objects, removing
them one at a time.
With the DisplayObjectContainer.removeChildren() method, one simple command
can be used to remove all children of a parent container, making them all available for
garbage collection.
DisplayObjectContainer.removeChildren() | 3
www.it-ebooks.info
You’ll want to be sure to remove any event listeners or other references
to these children before invoking removeChildren, else the garbage col-
lector may not be able to totally free the memory allocated to these
objects.
Figure 1-3. Remove Children
In the following example, we will generate a number of dynamic MovieClip symbols
upon the Stage. We add an event listener to the Stage as well, listening for a simple
MouseEvent.CLICK event – which then invokes a method to remove all of these Movie
Clips with one simple command: stage.removeChildren().
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class RemoveAllChildren extends Sprite {
4 | Chapter 1: Improvements to the MovieClip and Drawing APIs
www.it-ebooks.info
public function RemoveAllChildren() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
for(var i:int=100; i>0; i ){
var childMC:MovieClip = new MovieClip();

var randX:Number = Math.floor(Math.random() * (1+stage.stageWidth-100))
+ 50;
var randY:Number = Math.floor(Math.random() * (1+stage.stageHeight-100))
+ 50;
var randD:Number = Math.floor(Math.random() * 50-10) + 10;
childMC.x = randX;
childMC.y = randY;
childMC.graphics.beginFill(0x000000, 0.5);
childMC.graphics.drawCircle(0, 0, randD);
childMC.graphics.endFill();
this.addChild(childMC);
}
stage.addEventListener(MouseEvent.CLICK, removeAllChildren);
}
protected function removeAllChildren(e:MouseEvent):void {
stage.removeChildren();
}
}
}
MovieClip.isPlaying
It’s actually sort of amazing that we haven’t had this property in older versions of Flash
Player and AIR. MovieClip instances are unique in that they contain their own timeline,
independent from the main timeline. Often, a developer will want to know whether or
not a specific MovieClip instance is actually playing or not, and this has traditionally
involved monitoring the current frame of the MovieClip to determine whether or not it
is changing over time.
Making use of this new functionality is very direct, as MovieClip.isPlaying is simply a
property of every MovieClip instance which, when invoked, returns a Boolean value of
true for playing and false for stopped. In the following example, we create a Movie
Clip add it to the DisplayList, and then write the isPlaying property out onto a Text

Field.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
MovieClip.isPlaying | 5
www.it-ebooks.info
public class CheckPlaying extends Sprite {
private var face:MovieClip;
private var traceField:TextField;
public function CheckPlaying() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
face = new AngryFace() as MovieClip;
face.x = stage.stageWidth/2;
face.y = stage.stageHeight/2;
face.stop();
face.addEventListener(MouseEvent.CLICK, toggleFacePlaying);
addChild(face);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 26;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;

traceField.alpha = 0.7;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
stage.addEventListener(Event.ENTER_FRAME, checkPlaying);
}
protected function toggleFacePlaying(e:MouseEvent):void {
if(face.isPlaying){
face.stop();
}else{
face.play();
}
}
protected function checkPlaying(e:Event):void {
traceField.text = "MovieClip is playing? => " + face.isPlaying;
}
}
}
6 | Chapter 1: Improvements to the MovieClip and Drawing APIs
www.it-ebooks.info
Figure 1-4. MovieClip.isPlaying
The result of this code can be seen fully rendered in Figure 1-4. When clicking upon
the MovieClip, its playback is toggled and the isPlaying Boolean is measured and writ-
ten onto the screen.
MovieClip.isPlaying | 7
www.it-ebooks.info
Figure 1-5. Export SWC from Flash Professional
Note that in this example, we are employing a MovieClip object that was
animated in Flash Professional CS5.5, exported as part of a SWC, and

linked into Flash Builder 4.5. There are other ways of doing this, but
this method is very direct if you are not working within Flash Profes-
sional already.
8 | Chapter 1: Improvements to the MovieClip and Drawing APIs
www.it-ebooks.info
CHAPTER 2
External Image Capabilities
Taking into account the close relationship between AIR and Flash Player, along with
Flash Player’s focused ability to readily handle vector drawing objects, it is often over-
looked how capable the Flash Platform is at utilizing bitmap data through embedded
or external image files. Whether using PNG, JPG, GIF, or the new JPEG-XR filetype,
there is no denying that this imaging technology is extended and improved upon in
some rather spectacular ways.
Enhanced High-Resolution Bitmap Support
Loaded BitmapData objects have historically been limited to 8,191 total pixels along any
side with a total supported resolution of 16,777,215 pixels…which isn’t a whole lot
when dealing with high resolution images. With the megapixel count of consumer
digital cameras breaking well past 10, the need for greater resolution is easily apparent.
With AIR 3, these restrictions have been lifted, making this is a feature that can be
leveraged through a multitude of project types.
1 megapixel is equal to 1,000,000 pixels.
AIR 2 supports up to 16.777 megapixels.
AIR 3 incudes no such restrictions.
9
www.it-ebooks.info
Figure 2-1. High-resolution bitmap
We don’t actually need to do anything to enable support for this behavior, as it is built
into the AIR runtime itself. In the following example, we’ll use the Loader class to bring
a high-resolution image into an AIR project:
package {

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.ProgressEvent;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class HighRes extends Sprite {
private var imageLoader:Loader;
private var traceField:TextField;
10 | Chapter 2: External Image Capabilities
www.it-ebooks.info
public function HighRes() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
imageProgress);
imageLoader.load(new URLRequest("assets/highres.jpg"));
addChild(imageLoader);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 22;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.6;

traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function imageProgress(e:ProgressEvent):void {
traceField.appendText(e.bytesLoaded + " / " + e.bytesTotal + " bytes
loaded \n");
}
protected function imageLoaded(e:Event):void {
imageLoader.height = stage.stageHeight;
imageLoader.scaleX = imageLoader.scaleY;
traceField.text = "Loaded image is " + e.target.width + " x " +
e.target.height + " pixels =>\nThat's " + e.target.width*e.target.height + " total
pixels!\n\n" + traceField.text;
}
}
}
JPEG-XR Support
AIR 3 includes expanded support for still image file formats. Previous versions of AIR
include support for the following image file formats: GIF, JPEG, and PNG – with any
other files relying upon external code libraries for interpretation. The recent addition
of JPEG-XR (International Standard ISO/IEC 29199-2) brings a new image file format
to AIR which boasts more efficient compression than JPG, along with both lossy and
JPEG-XR Support | 11
www.it-ebooks.info

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×