📜 ⬆️ ⬇️

Manage Flash Object in Javascript

Manage Flash Object in JavaScript

JavaScript features in 95% of cases allow you to solve any problem for Web 2.0 . But sometimes you want a little more, a little prettier, perhaps a little faster. In this article, using examples, I want to show how to fill these 5% of the missing functionality with Flash .
This article will be useful to developers who write mostly in JavaScript and have minimal knowledge of ActionScript 3 .

For inspiration


I was inspired to write this article by an example from the FancyUpload package that implements simultaneous uploading of several files to a server with a nice progress bar in the gmail style (note: you can select several files at once) .
')
This example is interesting because design and management is controlled by CSS and JavaScript . JavaScript, if necessary, uses the necessary functionality from Flash .

If you like it, go ahead: we will look at how this works on a simpler example.


Sound notification



Consider the following example:


When working with our page can be a very important event. It is so important that you need to somehow be notified about it, even if the user is in another window or another program. One option is to give a beep. A possible solution is a non-standard BGSOUND tag, but we will focus on Flash. Implement the following functionality: in the JavaScript code, you can specify the name of the mp3 file to play (let's call it a ringtone ), the number of repetitions for the ringtone being played, the sound level in percent, and, in the extended version, the level is incrementing “from” and “to” and the time in seconds for which the sound level should increase.

Sit down for flash


In order to “share” any of the methods for accessing it from the base class of the Flash movie, you need to connect an ExternalInterface from JavaScript :

package
{
import flash.external.ExternalInterface;
...


and also describe the method for accessing with ExternalInterface.addCallback directive:
ExternalInterface.addCallback("setRingtone", setRingtone);


I sketched the base class for our example, the functionality that implements the ringtone playback was brought into a separate class com.ria.media.Ringtone , its implementation is not particularly important for our example.

Features of flash object loading


There is one more detail that needs to be considered when designing a Flash movie: Flash object loading is not instantaneous, that is, between the moment when the JavaScript object uses the flash object to embed the code and the moment when the “advanced” methods of our object become available. passes some time.

It is possible to unequivocally establish the fact of availability of methods by executing the JavaScript function that we specified, after all the ExternalInterface.addCallback directives. This is implemented, for example, by the directive ExternalInterface.call

If the flash-movie is inserted not by means of JavaScript, then it is possible that the flash was loaded before the JavaScript-code and the call to the function specified by us would lead to an error. How to solve this situation can be found here.

Just a little JavaScript left


I apologize in advance for the inconvenience, but I am used to writing on MooTools and do not want to back down from this habit. I’ll download the Flash movie as a convenient MooTools 1.2 Swiff plugin, there is a similar module for jQuery , and a universal SWFObject solution (about SWFObject in Habré )

After a flash object is loaded, its “shared” methods can be accessed approximately like this in pure JavaScript:
document.getElementById('myFlashObjectId').myMethod(param1,...)
, on MooTools get a little shorter
$('myFlashObjectId').myMethod(param1,...)


Actually, that's all that can come in handy when writing your own JavaScript code.

Do not forget to turn on the sound and see what happened .

The source code of the sample can be found here . Code highlighted by Ria SourceTabs

Source: https://habr.com/ru/post/60978/


All Articles