Flex 3 Cookbook: Chapter 8, Images, Bitmaps, Videos, Sounds
Pages: 1, 2, 3, 4, 5, 6, 7, 8

The complete code listing is shown here:

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="450" height="550">
    <mx:Script>
        <![CDATA[
            import mx.core.BitmapAsset;

            [Embed(source="../assets/mao.jpg")]
            private var mao:Class;

            private function convolve():void
            {
                var asset:BitmapAsset = new mao() as BitmapAsset;
                var convolution:ConvolutionFilter = 
new ConvolutionFilter(matrixXSlider.value, matrixYSlider.value,
            [input1.text, input2.text, input3.text, input4.text, input5.text, 
input6.text],
                divisorSlider.value, biasSlider.value, true);
                var _filters:Array = [convolution];
                asset.filters = _filters;
                img.source = asset;
            }

        ]]>
    </mx:Script>
    <mx:Button click="convolve()" label="convolve away"/>
    <mx:HBox>
        <mx:Text text="Matrix X"/>
        <mx:VSlider height="100" id="matrixXSlider" value="5.0" change="convolve()"/>
        <mx:Text text="Matrix Y"/>
        <mx:VSlider height="100" id="matrixYSlider" value="5.0" change="convolve()"/>
        <mx:Text text="Divisor"/>
        <mx:VSlider height="100" id="divisorSlider" value="5.0" change="convolve()"/>
        <mx:Text text="Bias"/>
        <mx:VSlider height="100" id="biasSlider" value="5.0" change="convolve()"/>
        <mx:VBox>
            <mx:TextInput id="input1" change="convolve()" width="40"/>
            <mx:TextInput id="input2" change="convolve()" width="40"/>
            <mx:TextInput id="input3" change="convolve()" width="40"/>
            <mx:TextInput id="input4" change="convolve()" width="40"/>
            <mx:TextInput id="input5" change="convolve()" width="40"/>
            <mx:TextInput id="input6" change="convolve()" width="40"/>
        </mx:VBox>
    </mx:HBox>
    <mx:Image id="img"/>
</mx:VBox>

Section 8.7: Send Video to an FMS Instance via a Camera

Problem

You want to send a stream from the user's camera to a Flash Media Server (FMS) instance for use in a chat or other live media application.

Solution

Capture the user's camera stream by using the flash.media.Camera.getCamera method and then attach that camera to a NetStream that will be sent to the Flash Media Server instance. Use the publish method of the NetStream class to send the stream with a specified name to the application that will handle it.

Discussion

The publish method indicates to a Flash Media Server that has been connected to via the NetConnection class, that the NetStream will be sending information to the server. What the server does with that information depends on the application, but there are flags that can be set in the publish method that indicate to the server and the Flash Player what should be done with the streamed information. The publish method has the following signature:

publish(name:String = null, type:String = null):void

Its parameters are as follows:

name:String (default = null)
A string that identifies the stream. If you pass false, the publish operation stops. Clients that subscribe to this stream must pass this same name when they call NetStream.play.
type:String (default = null)
A string that specifies how to publish the stream. Valid values are record, append, and live (the default). If you pass record, Flash Player publishes and records live data, saving the recorded data to a new FLV file with a name matching the value passed to the name parameter. The file is stored on the server in a subdirectory within the directory that contains the server application. If the file already exists, it is overwritten. If you pass append, Flash Player publishes and records live data, appending the recorded data to an FLV file with a name that matches the value passed to the name parameter, stored on the server in a subdirectory within the directory that contains the server application. If no file with a name matching the name parameter is found, a file is created. If you omit this parameter or pass live, Flash Player publishes live data without recording it. If a file with a name that matches the value passed to the name parameter exists, the file is deleted.

When you record a stream by using Flash Media Server, the server creates an FLV file and stores it in a subdirectory in the application's directory on the server. Each stream is stored in a directory whose name matches the application instance name passed to NetConnection.connect. The server creates these directories automatically; you don't have to create one for each application instance. For example, the following code shows how you would connect to a specific instance of an application stored in a directory named lectureSeries in your application's directory. A file named lecture.flv is stored in a subdirectory named /yourAppsFolder/lectureSeries/streams/Monday:

  var myNC:NetConnection = new NetConnection();
  myNC.connect("rtmp://server.domain.com/lectureSeries/Monday");
  var myNS:NetStream = new NetStream(myNC);
  myNS.publish("lecture", "record");

If you don't pass a value for the instance name that matches, the value passed to the name property is stored in a subdirectory named /yourAppsFolder/appName/streams/_definst_ (for default instance).

This method can dispatch a netStatus event with several different information objects. For example, if someone is already publishing on a stream with the specified name, the netStatus event is dispatched with a code property of NetStream.Publish.BadName. For more information, see the netStatus event.


This excerpt is from Flex 3 Cookbook. This highly practical book contains more than 300 proven recipes for developing interactive Rich Internet Applications and Web 2.0 sites. You'll find everything from Flex basics and working with menus and controls, to methods for compiling, deploying, and configuring Flex applications. Each recipe features a discussion of how and why it works, and many of them offer sample code that you can put to use immediately.

buy button

In the following example, the connection to the server is established, and the data from the camera is streamed to the server:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="500" 
creationComplete="setUpCam()">
    <mx:Script>
        <![CDATA[

            private var cam:Camera;
            private var nc:NetConnection;
            private var ns:NetStream;

            private function setUpCam():void
            {
                trace(Camera.names.join(","));
                //I'm doing this only because it's the only way the
                //flash player will pick up the camera on my MacBook
                cam = flash.media.Camera.getCamera("2");
                vid.attachCamera(cam);
                nc = new NetConnection();
                nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
                nc.connect("http://localhost:3002");
            }

            private function netStatus(event:NetStatusEvent):void
            {
                switch(event.info)
                {
                    case "NetConnection.Connect.Success":
                        ns = new NetStream(nc);
                        ns.attachCamera(cam, 20);
                        ns.attachAudio(Microphone.getMicrophone());
                        ns.publish("appname", "live");
                    break;
                }
            }

        ]]>
    </mx:Script>
    <mx:VideoDisplay id="vid" width="360" height="320"/>
</mx:Canvas>

Section 8.8: Access a User's Microphone and Create a Sound Display

Problem

You want to access a user's microphone and use the sound level of the microphone to draw a sound level.

Solution

Access the microphone by using the Microphone.getMicrophone method. Access the sound level that this method detects by using the mic.activityLevel property of the Microphone class on a regular interval.

Discussion

The Microphone class provides access to a user's microphone and computer, and the user must allow the Flash Player application access for you to use the class. The Microphone class shows the level of sound that the microphone is detecting, and dispatches events when sound begins and when there has not been any sound for a given period of time.

Three properties of the Microphone class monitor and control the detection of activity. The read-only activityLevel property indicates the amount of sound the microphone is detecting on a scale from 0 to 100. The silenceLevel property specifies the amount of sound needed to activate the microphone and dispatch an ActivityEvent.ACTIVITY event. The silenceLevel property also uses a scale from 0 to 100, and the default value is 10. The silenceTimeout property describes the number of milliseconds that the activity level must stay below the silence level, until an ActivityEvent.ACTIVITY event is dispatched to indicate that the microphone is now silent. The default silenceTimeout value is 2000. Although both Microphone.silenceLevel and Microphone.silenceTimeout are read-only, you can change their values by using the method.

The following example creates a Microphone object, which will prompt the user to accept or deny the Flash Player access to the microphone. Then, after microphone activity is detected via the Activity event, an enter frame event listener is added that will draw the soundLevel of the microphone into a Canvas.

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" 
creationComplete="createMic()">

<mx:Script>
    <![CDATA[
    import flash.media.Microphone;
    import flash.events.ActivityEvent;
    import flash.events.Event;
    import flash.events.StatusEvent;

      public var mic:Microphone;

      public function createMic():void
      {
        mic = Microphone.getMicrophone();
        mic.setLoopBack(true);
        mic.addEventListener(ActivityEvent.ACTIVITY, activity);
        mic.addEventListener(StatusEvent.STATUS, status);
        mic.addEventListener(Event.ACTIVATE, active);
      }

      private function active(event:Event):void
      {
        trace(' active ');
      }

      private function status(event:StatusEvent):void
      {
        trace("status");
      }

      private function activity(event:ActivityEvent):void
      {
        trace("active ");
        addEventListener(Event.ENTER_FRAME, showMicLevel);
      }

      private function showMicLevel(event:Event):void
      {
        trace(mic.gain+" "+mic.activityLevel+" "+mic.silenceLevel+" "+mic.rate);
        level.graphics.clear();
        level.graphics.beginFill(0xccccff, 1);
        level.graphics.drawRect(0, 0, (mic.activityLevel * 30), 100);
        level.graphics.endFill();
      }

    ]]>
</mx:Script>
<mx:Canvas width="300" height="50" id="level"/>
</mx:VBox>

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow