flex4 - NetStream.soundtransform.volume不会改变

时间:2014-12-01 22:45:48

标签: actionscript-3 flex netstream

我遇到接收网络音量变化的问题

        <![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" />

这不起作用。谁能帮帮我呢?更新:发布完整代码。我使用FlashDevelop 4.6.4,Flex4,SDK 4.6,flashPlayer 11.1。我的意思是如果streamRcv soundTransform音量大于0,声音是&#34; on&#34;,但是没有用silder&#34; rcvVolume&#34;来改变。如果streamRcv soundTransform音量等于0,首先视频没有声音,但如果拖动滑块,音量控制正常。

1 个答案:

答案 0 :(得分:0)

在Flex上,要播放视频流,我们可以使用VideoPlayerVideoDisplay组件或Video NetStream对象。

这是如何将这些元素与实时流一起使用以及如何调整音量的最小工作示例:

<?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>

我希望这有助于您解决问题。