Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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" minWidth="200" minHeight="100"
- applicationComplete="OnLoad()">
- <fx:Script>
- <![CDATA[
- import flash.events.*;
- import flash.external.ExternalInterface;
- import flash.media.Microphone;
- import mx.collections.XMLListCollection;
- import mx.controls.*;
- import mx.events.*;
- import mx.managers.PopUpManager;
- import mx.utils.Base64Decoder;
- import mx.utils.Base64Encoder;
- private var DOUBLE_CLICK_SPEED:int = 10;
- private var mic:Microphone = Microphone.getMicrophone();
- private var last:String = "";
- private var selected:String = "";
- private var lastTime:Date = new Date();
- private var delay:int = 1000; // 1 second buffer
- private var _delay:int = 0;
- private var server:String = "66.168.187.147";
- private var TextSock:Socket;
- private var VoiceSock:Socket;
- private var Nick:String = "Guest";
- private var MyRoom:String = null;
- private var sound:Sound = null;
- private var sound_Playing:Boolean = false;
- private var _vbuf:ByteArray = new ByteArray();
- [Bindable]
- private var Rooms:XML =
- <list>
- <room title="Offline" code="100"></room><room title="Debug" code="100"></room>
- </list>;
- [Bindable]
- private var Rooms_Data:XMLListCollection =
- new XMLListCollection(Rooms.room);
- private function treeLabel(item:Object):String {
- var node:XML = item as XML;
- if( node.localName() == "room" )
- return node.@title;
- else if( node.localName() == "user" )
- return node.@nick;
- else
- return "NULL";
- }
- private function OnLoad():void
- {
- //try {
- //Security.loadPolicyFile("xmlsocket://"+server+":843/");
- TextSock = new Socket();
- TextSock.addEventListener(Event.CONNECT, textConnect);
- TextSock.addEventListener(Event.CLOSE, textDisconnect);
- TextSock.addEventListener(ProgressEvent.SOCKET_DATA, textReceive);
- //Security.loadPolicyFile("xmlsocket://"+server+":"+portNumber+"/");
- TextSock.connect(server, 843);
- ChatTree.addEventListener( MouseEvent.DOUBLE_CLICK, dblClick );
- ChatTree.doubleClickEnabled = true;
- mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
- mic.addEventListener(ActivityEvent.ACTIVITY, this.onMicActivity);
- mic.addEventListener(SampleDataEvent.SAMPLE_DATA, this.micParse);
- mic.setUseEchoSuppression(true);
- mic.gain = 100;
- mic.rate = 44;
- mic.setSilenceLevel(voicelimit.value, 1);
- if (mic.muted) {
- btn_mute.label = "UnMute";
- Security.showSettings(SecurityPanel.PRIVACY);
- if (!mic.muted)
- btn_mute.label = "Mute";
- }
- // load post variables
- var tmp:String = LoaderInfo(this.root.loaderInfo).parameters.name as String;
- if (tmp != null) {
- Nick = tmp;
- Nickname.text = Nick;
- } else {
- mx.controls.Alert.show("Could not grab Nick");
- }
- //Rooms = new XML("<list><room title=\"Lobby\"></room><room title=\"Alt Lobby\"></room><room title=\"Afk\"><user nick=\"Dummy\"/></room></list>");
- //Rooms_Data = new XMLListCollection(Rooms.room);
- //} catch (error:Error) {
- // Alert.show(error.toString(), "OnLoad Error");
- //}
- //playSound(null);
- }
- private function onMicStatus(event:StatusEvent):void
- {
- try {
- if (event.code == "Microphone.Unmuted")
- {
- btn_mute.label = "Mute";
- }
- else if (event.code == "Microphone.Muted")
- {
- miclevel.value = 0;
- btn_mute.label = "UnMute";
- }
- } catch (error:Error) {
- Alert.show(error.toString(), "onMicStatus Error");
- }
- }
- private function onMicActivity(event:ActivityEvent):void {
- if (event.activating && btn_mute.label == "Mute") {
- //btn2.label = "true";
- } else {
- //btn2.label = "false";
- miclevel.value = 0;
- }
- }
- private function micParse(event:SampleDataEvent):void {
- //try {
- if (mic.muted)
- return;
- if (btn_mute.label == "Mute") {
- miclevel.value = mic.activityLevel;
- // we are connected
- if (VoiceSock.connected) {
- var buf:ByteArray = new ByteArray();
- event.data.readBytes(buf, 0, event.data.bytesAvailable);
- VoiceSock.writeBytes(buf, 0, buf.length);
- VoiceSock.flush();
- }
- }
- //} catch (error:Error) {
- // Alert.show(error.toString(), "micParse Error");
- //}
- }
- private function Join():void {
- if (selected != MyRoom) {
- MyRoom = selected;
- RemoveName(Nick);
- AddName(Nick, MyRoom);
- }
- }
- private function dblClick(evt:MouseEvent):void {
- // if we selected a room, pick room
- var tmp:XML = ChatTree.selectedItem as XML;
- if (tmp.localName() == "room") {
- last = selected;
- selected = tmp.@title;
- //mx.controls.Alert.show("Selected: Room="+selected);
- } else if (tmp.localName() == "user") {
- // if we selected a name, pick parent node
- last = selected;
- selected = tmp.parent().@title;
- //mx.controls.Alert.show("Selected: User="+selected);
- }
- Join();
- }
- private function AddName(name:String, dest:String):void {
- btn2.label = selected;
- var newNode:XML = <user/>;
- newNode.@nick = name;
- var room:XMLList = Rooms.room.(@title == dest);
- if (room.localName() == "room") {
- if( room.length() > 0 ) {
- room[0].appendChild(newNode);
- }
- }
- }
- private var _foundNode:XML = null;
- private function FindUser(name:String, target:XML):Boolean {
- if (target.localName() == "user") {
- // is a user listing
- if (target.@nick == name) {
- _foundNode = target;
- return true;
- }
- } else {
- // is a channel listing
- if (target.children().length() > 0) {
- for each (var _node:XML in target.children()) {
- if (FindUser(name, _node))
- return true;
- }
- }
- }
- return false;
- }
- private function FindRoom(name:String, target:XML):Boolean {
- if (target.localName() == "user")
- return false;
- if (target.localName() != "user") {
- // did we find it?
- if (target.@title == name) {
- _foundNode = target;
- return true;
- }
- // does it have children?
- if (target.children().length() > 0) {
- // scan for child rooms
- for each (var _node:XML in target.children()) {
- if (FindRoom(name, _node))
- return true;
- }
- }
- }
- return false;
- }
- private function RemoveName(name:String):void {
- if (!FindUser(name, Rooms))
- return;
- var children:XMLList = XMLList(_foundNode.parent()).children();
- for(var i:Number=0; i < children.length(); i++) {
- if( children[i].@nick == _foundNode.@nick ) {
- delete children[i];
- break;
- }
- }
- }
- private function MicToggle():void {
- if (mic.muted) { // mic disabled
- Security.showSettings(SecurityPanel.PRIVACY);
- if (!mic.muted)
- btn_mute.label = "Mute";
- } else { // mic enabled
- if (btn_mute.label == "UnMute") {
- btn_mute.label = "Mute"; // unmuted
- } else {
- miclevel.value = 0;
- btn_mute.label = "UnMute"; // muted
- }
- }
- }
- protected function textConnect(event:Event):void
- {
- trace("Text connected.");
- // send nickname
- // start voice comms
- VoiceSock = new Socket();
- VoiceSock.addEventListener(Event.CONNECT, voiceConnect);
- VoiceSock.addEventListener(Event.CLOSE, voiceDisconnect);
- VoiceSock.addEventListener(ProgressEvent.SOCKET_DATA, voiceReceive);
- VoiceSock.connect(server, 3784);
- }
- protected function textDisconnect(event:Event):void
- {
- trace("Text Disconnected.");
- if (VoiceSock.connected) {
- VoiceSock.close();
- }
- Alert.show("Text Connection Disconnect");
- }
- private var sbuf:String = "";
- protected function textReceive(event:ProgressEvent):void
- {
- //try {
- sbuf += TextSock.readUTFBytes(TextSock.bytesAvailable);
- trace("Text received "+sbuf.length+".");
- while (sbuf.indexOf("\r\n") > -1) {
- // strip next line out of buffer
- var line:String = sbuf.substr(0,sbuf.indexOf("\r\n"));
- sbuf = sbuf.substr(sbuf.indexOf("\r\n")+2);
- if (line.indexOf("PING") == 0) {
- TextSock.writeUTF("PONG\r\n");
- continue;
- }
- if (line.indexOf("PONG") == 0) {
- // ignore
- continue;
- }
- Alert.show(line, "Message:");
- }
- //} catch (error:Error) {
- // Alert.show(error.toString(), "textReceive Error");
- //}
- }
- protected function voiceConnect(event:Event):void
- {
- trace("voice connected.");
- //Alert.show("Voice Connected");
- }
- protected function voiceDisconnect(event:Event):void
- {
- trace("voice Disconnected.");
- //Alert.show("Voice Connection Disconnect");
- }
- private var player:Timer = null;
- private var buf:ByteArray = new ByteArray();
- protected function voiceReceive(event:ProgressEvent):void
- {
- //try {
- //event.target.removeEventListener(ProgressEvent.SOCKET_DATA, SoundBuffer);
- //var bytes:ByteArray = new ByteArray();
- var avail:uint = VoiceSock.bytesAvailable;
- trace("voice received "+avail+".");
- if (avail > 0) {
- // get bytes from socket, write to buffer
- var _buf:ByteArray = new ByteArray();
- VoiceSock.readBytes(_buf);
- buf.writeBytes(_buf); // add to buf
- if (player == null)
- player = new Timer(1000, 1); // 1 second delay
- /*if (!player.running) {
- if (!sound_Playing) {
- // received data and we aren't cued to play
- player.addEventListener(TimerEvent.TIMER, playSound);
- player.start();
- }
- }*/
- playSound(null);
- }
- //} catch (error:Error) {
- // Alert.show(error.toString(), "voiceReceive Error");
- //}
- }
- private var _numSamples:int;
- private var _lastname:String = "";
- private var _lasttimer:String = "";
- private function playSound(evt:TimerEvent):void
- {
- // Sock -> buf -> _vbuf -> sound
- trace("playSound()");
- //try {
- if (_vbuf == null)
- _vbuf = new ByteArray();
- if (sound != null)
- sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
- var _buf:ByteArray = new ByteArray();
- _buf.writeBytes(_vbuf, _vbuf.position, _vbuf.length-_vbuf.position);
- _buf.writeBytes(buf);
- buf = new ByteArray();
- _vbuf = _buf;
- _vbuf.position = 0;
- if (_vbuf.length > 0) {
- trace("vbuf contains "+_vbuf.length+" bytes.");
- }
- sound = null;
- sound = new Sound();
- sound.addEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer, false, 0, true);
- //_numSamples = _vbuf.length/4;
- //sound.extract(_vbuf, int(_vbuf.length * 44.1));
- //_phase = 0;
- //channel.addEventListener( Event.SOUND_COMPLETE, playbackComplete );
- //channel = sound.play();
- sound_Playing = true;
- sound.play();
- //Alert.show("Playing Sound");
- trace("Playing Sound");
- //} catch (error:Error) {
- // Alert.show(error.toString(), "playSound Error");
- //}
- }
- private var _phase:Number = 0;
- private function SoundBuffer(event:SampleDataEvent):void {
- //try {
- trace("[SoundBuffer]");
- //sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
- var l:Number = 0;
- var r:Number = 0;
- // buffer 4KB of data
- for (var i:int = 0; i < 4096; i++)
- {
- //if (_vbuf.length >= int(_phase)*4)
- //_vbuf.position = int(_phase)*4;
- if (_vbuf.position == _vbuf.length) {
- l = 0;
- } else {
- l = _vbuf.readFloat();
- }
- //_vbuf.position -= 1;
- if (_vbuf.position == _vbuf.length) {
- r = 0;
- } else {
- //r = _vbuf.readFloat();
- r=l;
- }
- // aiming for a 100mhz quality, or closest there of
- //for (var rate:int = 0; rate < 100 && rate+(mic.rate/2) < 100; rate += mic.rate) {
- event.data.writeFloat(l);
- event.data.writeFloat(r);
- /*_phase += 1.0;
- if (_phase >= _numSamples) {
- _phase -= _numSamples;
- }*/
- //}
- }
- /*if (_vbuf.position == _vbuf.length) {
- sound_Playing = false;
- sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
- }*/
- /*var _buf:ByteArray = new ByteArray();
- _buf.writeBytes(_vbuf, _vbuf.position, _vbuf.length);
- _vbuf = _buf;*/
- //player = new Timer(1000, 1);
- //player.addEventListener(TimerEvent.TIMER, playSound);
- //trace("Timer set durring playback.");
- System.gc();
- trace("[!SoundBuffer]");
- //} catch (error:Error) {
- // Alert.show(error.toString(), "SoundBuffer Error");
- //}
- }
- private function changevoicelimit(event:Event):void {
- mic.setSilenceLevel(voicelimit.value, 1);
- }
- protected function playbackComplete(event:Event):void
- {
- /*try {
- Alert.show("playbackComplete");
- // TODO Auto-generated method stub
- // cycle off read data
- var buf:ByteArray = new ByteArray();
- buf.readBytes(_vbuf, _vbuf.position, _vbuf.length);
- if (buf.length < 4096) {
- for (var c:int = 0; c < 4096-buf.length; c++) {
- buf.writeFloat(0);// silence
- }
- }
- _vbuf = buf;
- _vbuf.position = 0;
- _numSamples = _vbuf.length / 4;
- _phase = 0;
- channel = sound.play();
- } catch (error:Error) {
- Alert.show(error.toString(), "playbackComplete Error");
- }*/
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- Place non-visual elements (e.g., services, value objects) here -->
- </fx:Declarations>
- <s:Panel x="0" y="0" width="100%" height="100%" backgroundColor="#909090" title="LoLChat">
- <s:Label id="Nickname" right="10" top="-29" width="253" height="21" text="Guest"
- textAlign="right" verticalAlign="middle"/>
- <mx:Tree id="ChatTree" left="10" right="10" top="0" bottom="0" doubleClick="dblClick(event);"
- dataProvider="{Rooms_Data}" labelFunction="treeLabel" />
- <s:controlBarContent>
- <s:HGroup width="30%" height="44">
- <s:Button id="btn_join" width="30%" height="40" label="Join" click="Join();"/>
- <s:ToggleButton id="btn_mute" width="30%" height="40" label="Mute" click="MicToggle();"/>
- <s:Button id="btn2" width="30%" height="40" label=""/>
- </s:HGroup>
- <s:VGroup width="70%" height="44">
- <s:HSlider id="miclevel" width="100%" maximum="100" minimum="0" stepSize="1.0"/>
- <s:HSlider id="voicelimit" width="100%" change="changevoicelimit(event);" maximum="100"
- minimum="0" stepSize="0.1" value="10"/>
- </s:VGroup>
- </s:controlBarContent>
- </s:Panel>
- </s:Application>
Advertisement
Add Comment
Please, Sign In to add comment