Accessibility

Table of Contents

NYC Traffic: Best Practices for Building Flash Lite Dynamic Content

Connecting to Remote Content

Before you can download content to your mobile phone, one additional step has to occur—the phone must establish an active GPRS connection. Generally, this means that if your SWF file is currently not connected, the user must select an access point through an OS-level menu that will appear when the connection has to be established (see Figure 3). As part of this process, however, you have better information about the state of your user's network status than any other version of Flash.

When you call a function that loads data, you will be prompted to choose your access point.

Figure 3: When you call a function that loads data, you will be prompted to choose your access point.

Because each phone has different capabilities, you may want to check the phone's ability to make a network connection. You can do this by using the new _capLoadData property. If this property has a value of 1, then the phone has a network connection. Otherwise you will need to figure out how you want your program to degrade gracefully and deal with the fact that it will not be able to connect to the Internet. Unfortunately, this usually involves displaying a message to users that they are currently unable to connect to the Internet.

At any time you can also get more detailed information about the last request with fscommand2 getNetworkRequestStatus(). The returned status can be anywhere from -1 to 10, with every value representing a different network state or result. For the full list of status codes and sample syntax, see pages 41 and 42 in the Flash Lite 1.1 Authoring Guidelines available in PDF format in the Flash Lite CDK.

When building a connected application, keep the following connection issues in mind:

  • Idle connections time out.

    Your connection to the Internet will shut down after several seconds if the SWF file is not communicating with the Internet. If you need to reconnect after the connection times out, the user must select his or her access point again.

  • Limit the number of simultaneous connections.

    If you try to open more than one connection in the same frame, only the first connection is made. Also, if you have too many connections open at once (even at just one connection per frame), you outstrip the phone's ability to make connections.

  • Remember that there are many data plans in the world.

    GPRS data access can be very expensive. You should do your best to optimize the data you are transferring, and only connect when necessary. If you are creating a high-bandwidth application, consider at one or more points reminding the user that this application may transfer a considerable amount of data.

Loading Text

The connection process described above happens automatically any time you try to access remote data from the Internet whether it is variable or movie data. To access remote text you can use either loadVariables or loadVariablesNum depending on whether you want the loaded data to be stored in a timeline you specify or in a level. You can read about the syntax for these commands in the documentation in the Flash IDE or look at the examples provided in the Flash Lite CDK.

The text on the server must be URLencoded in order to be read by loadVariables. URL encoding is a way of serializing data where keys and values are separated by equal signs and each key/value pair is separated by an ampersand (&) character. Also, some special characters are converted to a percent sign followed by a two-character code. This is a very common form of encoding and is seen often in very long URLs that pass a lot of information in them. (For more details refer to this Technote 14143: URL Encoding: Reading special characters from a text file.)

Below is the content of http://justin.everett-church.com/mobile/variables.txt. This file contains four variables and their escaped values.

"name=Justin+Everett-Church"site=http%3A%2F%2Fjustin%2Eeverett%2Dchurch%2Ecom"company=Yahoo%21"done=1

In an FLA file add the following code to Frame 1 in the root Timeline:

loadVariables(http://justin.everett-church.com/mobile/variables.txt, "/")

When you try this yourself, make sure that you also have text fields in your root to show the loaded variables. Feel free to test this within Flash, but you really should try this on your phone to see how the loading process works.

To verify that your data has loaded, you have two alternatives. You can check getNetworkRequestStatus() using fscommand2 and verify the status, or you can include a variable at the end of your stream of variables and then check the right timeline for that value, which I find is far simpler than the other alternative. For example, I like to use the variables done=1 or eof=1. This way, when I check the value of the variable, I know the data is loaded if it equals 1. If it doesn't, I might go check the status to make sure that there was no issue with finding the file.

To see an example of variables being loaded from the server, check out the "Connection Detection" example in the Flash Lite CDK.

Loading SWF Files

The process for loading an external SWF file is very similar to loading variables. The commands for loading a SWF file are loadMovie and loadMovieNum, once again making the distinction between loading into a timeline versus loading into a level, respectively. The only requirement for loading the SWF file is that it must be formatted as a Flash Lite, Flash 4, or older file.

If the file that you try to load is either a newer SWF file or a completely incorrect file type, Flash will not handle this silently like it does on the desktop. An attempt to load invalid data is reported to the user in an OS-level pop-up alert. Keep in mind that some features described for loadMovie in the Flash IDE help are for higher versions of Flash Player and the Flash Lite player. For example, you cannot load JPEG files directly using Flash Lite. Attempts to load a JPEG file would trigger the invalid content warning (see Figure 4).

Loading a SWF file exported for Flash Player 7 will give you a type 3 error

Figure 4: Loading a SWF file exported for Flash Player 7 will give you a type 3 error.

To see this error on your phone, add the following line of code to the root Timeline of an FLA file:

loadMovieNum("http://justin.everett-church.com/mobile/invalid.swf", 1)

Keep in mind that the content loads correctly when viewed on your desktop computer. Loading fails only when you load the file from your phone.

Be careful that your target for the loadMovie command is always on the Stage when the load is complete. If you load a SWF file into a movie clip that only exists on certain frames and the server responds when the target movie clip does not exist, an error will occur. To prevent this error, consider keeping your target on the Stage at all times but using the _visible property to make it invisible as needed.

To verify that a SWF file is loaded in Flash Lite, you can always use the network request status. However, I prefer to use the existing properties _framesloaded and _totatframes. When the two values are equal, your file is loaded.

To demonstrate dynamic loading of a SWF file in Flash Lite take a look at the following sample code that I have taken from my NYC traffic project. All you need to do is place the code in Frame 1 of the root Timeline:

loadMovieNum("http://gizmo.everett.org/cam/flashimg.php?image=/cctv26.jpg&cacheBust=" + random(100000), 1)

Although this code works for any properly formatted SWF file, in this case the URL points to a PHP page that grabs the current image from Times Square. As far as Flash is concerned, there is no difference between a static SWF file on your server and a SWF file that is generated dynamically. I've added a cache buster random number to the end, so that each time I make a call for the image the URL will be slightly different. I'm doing this because the point of the SWF file is to be different each time.

The work for generating the SWF file is all done on the server. In this case, I'm using a modified version of Stefan Schüßler's FlashWriter Toolkit. The original version is available at his site, Bronson Beta. This PHP file wraps the JPEG file in a SWF file but does not resize or rotate the image. In the full version of the project, the application applies the changes to the layout.