flex4 - NetStream.soundtransform.volume doesn't change

959 Views Asked by At

I have problem with receive netstream sound volume changing

        <![CDATA[

    import mx.events.FlexEvent;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.media.Camera;
    import flash.media.Microphone;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.SoundTransform;

            private const SERVER:String = 'rtmfp://p2p.rtmfp.net/';
            private const DEVKEY:String = 'my dev key';
            private const REG:String = 'scripts/reg.php';
            private const GETID:String = 'scripts/getid.php';

        private var netConnection:NetConnection;
        private var netStreamPublish:NetStream;
        private var streamRcv:NetStream;    
        private var videoRcv:Video;
        private var PeerId:String;
        private var newVolume:Number = 0;


        private function connect():void
        {
            netConnection = new NetConnection();
            netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
            netConnection.connect(SERVER + DEVKEY);
        }

        private function netConnectionHandler(event:NetStatusEvent):void
        {
            trace('netConnection:', event.info.code);
            switch(event.info.code)
            {
                case "NetConnection.Connect.Success":
                    PublicherConnect();
                    break;
            }
        }

            private function InsertID():void {
            PeerId = netConnection.nearID;
            var loader:URLLoader = new URLLoader();
            try {
                    loader.load(new URLRequest(REG+'?insert='+PeerId));
                }
             GetID();
            }

            private function GetID():void {
                   var loader2:URLLoader = new URLLoader();
                   loader2.load(new URLRequest(GETID));
                   loader2.addEventListener(Event.COMPLETE, function(e:Event):void {

                       var farid:String = e.target.data;

                       if (farid.length) {
                            initRcvStream(farid);
                       }
                       });

                  } 

        private function PublicherConnect():void
        {  
            netStreamPublish = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS);
            netStreamPublish.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);

            var camera:Camera = Camera.getCamera();
            myCameraDisplay.attachCamera(camera);
            netStreamPublish.attachCamera(camera);
            var mic:Microphone = Microphone.getMicrophone();
            mic.gain = myVolume.value;
            netStreamPublish.attachAudio(mic);
            netStreamPublish.publish('media');

            var client:Object = new Object;
           client.onPeerConnect = function(c:NetStream):Boolean {
           netStreamPublish.send(c.farID);
           initRcvStream(c.farID);
           return true;
           };
           netStreamPublish.client = client;
            InsertID();
           }

          private function micVolumeChanged(e:Event):void
            {
                var mic:Microphone = Microphone.getMicrophone();
                mic.gain = e.target.value;
            }

            private function initRcvStream(peerID:String):void {

                streamRcv = new NetStream(netConnection, peerID);
                streamRcv.play('media');    

                var rcvSndTransform:SoundTransform = new SoundTransform();
                rcvSndTransform.volume = newVolume;
                streamRcv.soundTransform = rcvSndTransform;

                videoRcv = new Video;
                videoRcv.attachNetStream(streamRcv);
                rcvVideoDisplay.addChild(videoRcv);

                }   

            private function speakerVolumeChanged(e:Event):void
            {   
                newVolume = e.target.value;
                rcvVideoDisplay.volume = newVolume;
                streamRcv.soundTransform = new SoundTransform(newVolume);
            }   
        ]]>

    <mx:VideoDisplay id="rcvVideoDisplay" x="20" y="50" width="300" height="250" volume=".9" />
    <mx:VideoDisplay id="myCameraDisplay" x="370" y="50" width="300" height="250"/>
    <mx:HSlider id="rcvVolume" change="speakerVolumeChanged(event)" minimum="0" maximum="1" showDataTip="false" snapInterval=".01" x="45" y="250" value=".9"/>
    <mx:HSlider id="myVolume" change="micVolumeChanged(event)" minimum="0" maximum="1" showDataTip="false" snapInterval=".01"  x="395" y="250" value=".9" />

And this does't work. Who can help me, please? Update: posted full code. I use FlashDevelop 4.6.4, Flex4, SDK 4.6, flashPlayer 11.1. I mean if streamRcv soundTransform volume greater than 0, sound is "on", but doesn't change with silder "rcvVolume". If streamRcv soundTransform volume equal to 0, at first, video have no sound, but if drag slider, sound volume controlled normally.

1

There are 1 best solutions below

11
On BEST ANSWER

On Flex, to play a video stream we can use a VideoPlayer or a VideoDisplay components or a Video object with NetStream.

This an minimal working example of how to use theses elements with a live stream and how to adjust their sound volume :

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               width="890" height="371" minWidth="955" minHeight="600"
               creationComplete="init(event)">

    <fx:Script>
        <![CDATA[

            import mx.events.FlexEvent
            import flash.events.AsyncErrorEvent
            import flash.events.NetStatusEvent
            import flash.events.MouseEvent
            import flash.events.Event

            import spark.components.mediaClasses.DynamicStreamingVideoItem
            import spark.components.mediaClasses.DynamicStreamingVideoSource

            private const server:String = 'rtmp://localhost/live'
            private const stream:String = 'test'
            private var nc:NetConnection
            private var ns:NetStream
            private var video:Video
            private var video_item:DynamicStreamingVideoItem
            private var video_source:DynamicStreamingVideoSource
            private var volume:Number = 0.5

            private function btn_start_clickHandler(e:MouseEvent):void {            

                video_item = new DynamicStreamingVideoItem()
                video_item.streamName = stream

                // for VideoPlayer and VideoDisplay components we have to use
                // an DynamicStreamingVideoSource object as source
                video_source = new DynamicStreamingVideoSource()
                video_source.host = server
                video_source.streamItems = new <DynamicStreamingVideoItem>[video_item]
                video_source.streamType = 'live'                        

                video_player.source = video_source 
                // I disabled the VideoDisplay component just to start only playing
                // the live stream with the VideoPlayer component
                //video_display.source = video_source               

                nc = new NetConnection()
                nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent):void{})
                nc.addEventListener(
                    NetStatusEvent.NET_STATUS, function(e:NetStatusEvent):void{
                        if(e.info.code == 'NetConnection.Connect.Success'){

                            ns = new NetStream(nc)
                            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent):void{})
                            ns.addEventListener(NetStatusEvent.NET_STATUS, function(e:NetStatusEvent):void{})

                            video = new Video(240, 180)
                            // I disabled the Video object just to start only playing
                            // the live stream with the VideoPlayer component
                            //video.attachNetStream(ns)
                            video.smoothing = true
                            uic.addChild(video)

                            ns.play(stream)

                            ns.soundTransform = new SoundTransform(volume)
                            // you can use the SoundTransform to adjust the sound volume like this :
                            // var sound_transform:SoundTransform = new SoundTransform()
                            //     sound_transform.volume = volume
                            // ns.soundTransform = sound_transform

                        }
                    }
                )
                nc.connect(server)

            }

            private function init(e:FlexEvent):void {

                // you can hide the VideoPlayer component controls panel
                //video_player.playerControls.visible = false

                // init volume
                video_player.volume = video_display.volume = volume

            }

            private function volume_slider_changeHandler(e:Event):void {

                volume = e.target.value             
                video_display.volume = video_player.volume = volume             
                ns.soundTransform = new SoundTransform(volume)

            }

            private function player_type_clickHandler(e:MouseEvent):void {

                // here we have to use Component.stop() and Component.source = '' to avoid errors
                video_player.stop()
                video_display.stop()
                video_player.source = ''
                video_display.source = ''
                video.attachNetStream(null)

                switch (RadioButton(e.target).value){

                    case 'video_player':                        
                        video_player.source = video_source
                        break

                    case 'video_display':                       
                        video_display.source = video_source
                        break

                    case 'video': 
                        video.attachNetStream(ns)
                        break                   

                }

            }

        ]]>
    </fx:Script>

    <s:VideoPlayer id="video_player" x="46" y="100" width="240" height="180"/>
    <s:VideoDisplay id="video_display" x="322" y="100" width="240" height="180"
                    autoDisplayFirstFrame="true" autoPlay="true" enabled="true" volume="0.5"/>
    <s:Button id="btn_start" x="48" y="38" width="89" height="35" label="Start"
              click="btn_start_clickHandler(event)"/>
    <mx:UIComponent id="uic" x="597" y="100" width="240" height="180"/>
    <s:HSlider id="volume_slider" x="302" y="49" width="94" height="16"
               change="volume_slider_changeHandler(event)" maximum="1" minimum="0" stepSize="0.1"
               value="0.5"/>
    <s:Label x="245" y="51" text="Volume"/>
    <s:RadioButton x="122" y="303" label="VideoPlayer" click="player_type_clickHandler(event)"
                   enabled="true" groupName="player_type" selected="true" value="video_player"/>
    <s:RadioButton x="400" y="303" label="VideoDisplay" click="player_type_clickHandler(event)"
                   groupName="player_type" value="video_display"/>
    <s:RadioButton x="686" y="303" label="Video" click="player_type_clickHandler(event)"
                   groupName="player_type" value="video"/>
</s:Application>

I hope that helps you to resolve your problem.