The Flash Builder 4.5 package and, in fact, the ActionScript 3.0 language provides programmers with ample opportunities to use sound and microphone in their flex programs and regular flash drives in particular. Why is it necessary? First of all, it can be useful in creating programs such as all sorts of voice chats. Also, these elements can also be applied in the field of information security - for example, in voice recognition systems. But in this example and in this particular article, it will be said not so much about this as about the general rules for the use of these elements. It should also be noted that the work will be carried out with a flex-application.
Microphone connection
Let's start working with a microphone, for which we open, for example, Flash Builder 4.5 and create a Flex project on the AIR platform (a special platform that allows you to create not “flash drives” with the .swf format, but full-fledged applications - if you wish, even with the installation file and digital signature ). After which we can begin work. In order for the program to recognize the microphone connected to the computer, you need to register the following code:
public var micro:Microphone = Microphone.getMicrophone();
After executing this line of code, the program will select the first detected sound recorder. If no devices were connected, the
getMicrophone () method will return
null . It may be that several recording devices will be connected to one computer, in which case any of them can be accessed. The following code selects the second of the connected devices:
public var micro:Microphone = Microphone.getMicrophone(2);
If the second device does not exist, the value
null will also be returned. The index property itself, indicating the number of the device being requested, is of type int, that is, an integer type.
It should be noted that if the microphone has never been used in flash programs on your computer, Flash Player will display a special window asking for permission or blocking of this microphone. When the user selects one of the options, an event of the type STATUS will start, which can be traced. First you need to bind to this type of event a function that will be triggered when these events occur.
micro.addEventListener(StatusEvent.STATUS, onStatus);
This line of code can, for example, be entered into the event handler for pressing any button, or you can write it into the onCreationComplete function — then the handler will be bound to events immediately after the program is turned on.
import mx.controls.Alert; protected function onStatus(e:StatusEvent):void{ if(e.code == "Microphone.Unmuted") Alert.show(" ."); else if(e.code == "Microphone.Muted") Alert.show(" ."); }
In this short example, we create a STATUS event listener, which, when it receives a signal about the corresponding event, executes the body of the onStatus function. In this case, e.code is the value that is passed to the handler after pressing one of the buttons — enable or disable.
Now let's try to record something by speaking into the microphone. When any sounds come into the microphone, an event of type SAMPLE_DATA is activated. To record sound from a microphone, we first create some kind of storage for it. This will help us to create a variable of such data type as ByteArray.
public var bArr:ByteArray = new ByteArray;
Now, as in the previous example, we add an event listener to our micro variable.
micro.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleC);
After that, we write the handler function for the SAMPLE_DATA event.
protected function onSampleC(e:SampleDataEvent):void{ var sample:Number; bArr.clear(); while(e.data.bytesAvailable){ sample = e.data.readFloat(); bArr.writeFloat(sample); } }
In this example, we first clear our bArr byte array so that when the program is reused, the second record is not recorded after the first one into the same array. After that, we read the sound in our array as bytes, until it stops.
The Microphone class also has another interesting method. It allows you to send sound from a microphone to a sound-reproducing device (that is, speakers). It looks like this:
micro.setLoopBack(true);
But along with this method, you must use one more:
mic.setUseEchoSuppression(true);
This method involves the use of special echo cancellation technology. Even with it, the sound can be slightly distorted, and without it, when using micro.setLoopBack (true); You can even damage your speakers.
It is also possible to adjust the volume of the sound coming through the microphone. For this is the parameter gain. For example, by creating an HSlider object on a form, you can write a handler function for the slider movement event, creating a simple volume control. To do this, we write the function:
protected function onSlCh():void { micro.gain = sl.value; }
where sl is the id parameter value of the HSlider object.
Sound reproduction
The main sound object in ActionScript 3.0 is an instance of the Sound class. That he has such a method as play (), which allows you to play sound through speakers or headphones. Our first task will be downloading and playing audio in mp3 format. First of all, to load any mp3-file into an object of the class Sound, you need to create a so-called URLRequest - set the path to the file. For example:
public var urlr:URLRequest = new URLRequest("sample.mp3");
In this example, the sound is loaded from the folder of the created application project and is called sample.mp3. If you need to download a sound file from another local disk, you should create a URLRequest variable like this:
public var urlr:URLRequest = new URLRequest("C:/sample.mp3");
After that, you can immediately register the following code:
public var snd:Sound = new Sound(urlr);
And run the method
snd.play();
hanging it on a handler, for example, pressing a button.
You can do it differently, playing the sound when it is loaded and ready to use. To do this, once again use the event handler functions. Create an event handler that is activated when the audio file is loaded into the program:
snd.addEventListener(Event.COMPLETE, onSndReady);
In this case, the snd variable should be created not in the same way as in the previous example, but as follows:
public var urlr:URLRequest = new URLRequest("C:/sample.mp3"); public var snd:Sound = new Sound();
In the handler function of any button press event, write
snd.load();
This method will load the sound file specified in the urlr, and when the download is complete, send the COMPLETE event.
At first, we will not pass any variables to the Sound class constructor.
After this, create the specified onSndReady function:
function onSndReady(e:Event):void { var lsnd:Sound = e.target as Sound; lsnd.play(); }
As a result, as soon as the sound file specified in urlr is loaded into the program, it will be played.
It is also quite simple to write a function that will track the sound loading during its preparation for playback. To do this, on the form of the program you should create, for example, a TextArea object. Having set the value of its id = txt property, we will write the connection of the load event listener using the already familiar scheme:
snd.addEventListener(ProgressEvent.PROGRESS, onLoading);
After that, we will write a function that in our improvised text log txt will display the value of the loaded data as a percentage:
function onLoading(e:ProgressEvent):void { var loaded:int = Math.round(100 *(e.bytesLoaded / e.bytesTotal)); txt.text += " "+loaded+"%."; }
As a result, the percentage of audio file loading will be displayed in the log called txt.
')
Finally, you should talk about some additional parameters of the play () method.
It has two of them - it is the startTime parameter, specified in milliseconds, and the loops parameter, which indicates how many times the downloaded sound should be repeated.
For example, snd.play (1000,3) will play the sound loaded into snd starting from 1 second and then repeat it three times.
This concludes the article on loading and playing audio, as well as using the microphone in ActionScript 3.0 and their most common uses. Thank you for your attention and good luck in writing your own programs.