Saturday, January 29, 2011

How to make an audio activity level meter per NetStream?

The NetStream class do not have any method to get audio activity level. It seems Microphone class has acitivityLevel. It means, players can not get the audio level from the NetStream class, but a publisher can make the audio level from the Microphone class. So it is a solution. The publisher make and send it to players. And they show audio levels on their video screens.

Add highlighted code to the MyNetConnection.

        public function onConnect(event:NetStatusEvent):void { 
            client = new MyNetConnectionClient();
            setupPingTimer();
        }

Then create MyNetConnectionClient. This is a publisher side code.

package my.project.flash {
 
    public class MyNetConnectionClient {

        private var camera:Camera;
        private var microphone:Microphone;
        private var nsPublish:NetStream;
    
        public function MyNetConnectionClient(netConnection:NetConnection) {
            camera = Camera.getCamera();
            microphone = Microphone.getMicrophone();   
            nsPublish = new NetStream(netConnection);
            nsPublish.publish("myPublishStreamId");
            nsPublish.attachCamera(camera);
            nsPublish.attachAudio(microphone);
            microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, nsPublishSampleAudioFrame);
        }
  
        public function nsPublishSampleAudioFrame(event:SampleDataEvent):void {
            var metaData:Object = new Object();
            metaData.audioActivityLevel = microphone.activityLevel;
            nsPublish.send("setAudioActivityLevel", metaData);
        }
    }
}

Then the player side client code. "setAudioActivityLevel" should be the method on a NetStream.client. So you can have audio activity level per stream.

var stream:Stream = new NetStream();
    stream.client = new MyNetStreamClient();
    stream.play("myPublishStreamId");

package my.project.flash {
    public class MyNetStreamClient {
        public function setAudioActivityLevel(param:Object):void {
            doSomethingToShowAutioActivityLevel(param.audioActivityLevel);
        }
    }
}


Hmmm, I changed a lot from my source code, and I do not check it. So I'm not sure it works or not. However, I hope this will give you some hints.

No comments:

Post a Comment