Recipe 14.5 Capturing and Playing Audio from a Microphone

14.5.1 Problem

You want to get audio from a user's microphone to add it to a movie clip within the Flash movie or publish it to a FlashCom application. (See Recipe 14.9.)

14.5.2 Solution

Use the Microphone.get( ) method.

14.5.3 Discussion

The Microphone.get( ) method is analogous to the Camera.get( ) method, but it gets audio data from a user's microphone instead of video from his camera. For example:

myMic_mic = Microphone.get(  );

The Microphone.get( ) method gets the data for the default microphone (as set in the Microphone tab of the Player Settings dialog box) if you do not specify any parameters. However, if the user has multiple microphones, you can use the Microphone.names array to offer the user a choice among them. See Recipe 14.4, which demonstrates the analogous technique for multiple video cameras.

There is the possibility that the user does not have any microphone. The elements in the Microphone.names array correspond to the recording devices reported by the user's operating system. In most cases the devices are sound cards, and the existence of a sound card does not necessarily mean there is a microphone. Therefore, there is no definitive way to determine whether a user has a microphone (although if the Microphone.names array is empty, he does not).

if (Microphone.names.length > 0) {   // Place code here for getting and using a microphone stream. The user may, in   // fact, have only a sound card and no microphone. } else {   // Place code here to handle the case in which the user has no microphone. }

Like the Camera.get( ) method, Microphone.get( ) is not useful in isolation. You need to actually do something with the data it returns. You can apply that data to a net stream, in order to publish it to a FlashCom application (see Recipe 14.9), or you can apply it to a movie clip within the same Flash movie. Applying the microphone audio to a movie clip allows a user to hear his microphone output locally (generally, at the same time it is being published to a FlashCom application). You can attach the microphone output to a movie clip using the attachAudio( ) method, as follows:

mySoundHolderMc.attachAudio(myMic_mic);

Attaching the audio to a movie clip automatically plays the audio that is being retrieved from the microphone. You do not need to tell it to start, and you cannot tell the sound to stop. You can, however, set the Microphone object to null, which closes the stream, effectively stopping the sound. Alternatively, you can use a Sound object to control the volume, as discussed in Recipe 14.6. You cannot rewind or otherwise seek through microphone output.

If you are allowing a user to monitor both his microphone and camera output, and you have created the video object using the technique from Recipe 14.3, then it is convenient to attach the microphone audio to the video object's wrapper movie clip:

// Attach the camera video to the nested video object (my_video). myVideo_mc.my_video.attachVideo(Camera.get(  )); // Attach the microphone audio to the wrapper movie clip (myVideo_mc). myVideo_mc.attachAudio(Microphone.get(  ));

14.5.4 See Also

Recipe 13.2, Recipe 14.3, Recipe 14.4, Recipe 14.6, and Recipe 14.9



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net