Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:custom="com.talknicer.*"
- layout="absolute" creationComplete="init()" width="400" height="206"
- backgroundColor="#C7C2C2" xmlns:components="components.*"
- xmlns:s="library://ns.adobe.com/flex/spark"
- >
- <mx:Script>
- <![CDATA[
- import flash.events.*;
- import flash.media.*;
- import flash.net.*;
- import flash.utils.*;
- import mx.events.SliderEvent;
- public static const INACTIVE_STAT:int = 0;
- public static const PLAYBACK_STAT:int = 1;
- public static const RECORDING_STAT:int = 2;
- private var FLASH_SERVER:String = "rtmp://dev.llc.msu.edu/oflaDemo/RecordYourself";
- private var _nc:NetConnection = null;
- private var _inStream:NetStream = null;
- private var _outStream:NetStream = null;
- private var _microphone:Microphone = null;
- private var _bConnected:Boolean = false;
- private var _iStatus:int = 0;
- private var _bMicReady:Boolean = false;
- private var _bNetworkReady:Boolean = false;
- private var _internalFilename:String = genRandomStr(15);;
- public function getStatus():int {
- return _iStatus;
- }
- public function get getMic():Microphone {
- return _microphone;
- }
- public function get fileName():String {
- return _internalFilename+".flv";
- }
- private function setupMicrophone():void{
- _microphone = Microphone.getMicrophone();
- if (_microphone) {
- //_microphone.setLoopBack(true);
- _microphone.codec = SoundCodec.SPEEX;
- _microphone.encodeQuality = 10;
- _microphone.rate = 16;//44;renam
- _microphone.framesPerPacket = 2;
- _microphone.gain = 50;
- _microphone.setUseEchoSuppression(false);
- //_outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
- //_outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
- //_microphone.rate = 16; // is this a choice?!?
- //_microphone.framesPerPacket = 2;
- //_microphone.gain = 50;
- //_microphone.setUseEchoSuppression(true); // I am hoping you can try this with both true and false
- _bMicReady = true;
- //outStream.attachAudio(mic);
- //outStream.publish("ttestaudio","record");
- }
- }
- public function openConnection():Boolean{
- _nc = new NetConnection();
- _nc.client = this;//cl;
- _nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
- _nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
- _nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, catchAll);
- _nc.connect(FLASH_SERVER);
- return true;
- }
- private function netStatus(event:NetStatusEvent):void {
- var info:Object = event.info;
- switch (info.code){
- case "NetConnection.Connect.Success":
- _bNetworkReady = true;
- var ns:NetStream = new NetStream(_nc);
- ns.attachAudio(_microphone);
- break;
- case "NetConnection.Connect.Closed":
- closeConnection();
- break;
- }
- }
- //private function onAsyncError(event:AsyncErrorEvent):void {}
- //private function onSecurityError(event:SecurityErrorEvent):void {}
- public function onBWDone():void{}
- public function onPlayStatus(info:Object):void{
- trace("onPlayStatus:" + info.code);
- switch (info.code){
- case "NetStream.Play.Complete":
- stopPlayback();
- break;
- }
- }
- public function onMetaData(info:Object):void{
- trace("getting meta data");
- }
- private function catchAll(event:Object):void {
- if (event.type == "netStatus") {
- trace("catchAll:code:" + event.info.code);
- } else {
- trace("catchAll:event:" + event);
- }
- }
- private function playNetStatusHandler(event:NetStatusEvent):void {
- trace("my:"+event.info.code);
- switch (event.info.code) {
- case "NetStream.Pause.Notify":
- onVideoStop(null);
- //dispatchEvent(new Event(Event.COMPLETE));
- break;
- }
- }
- public function closeConnection():void{
- if (_nc) _nc.close();
- _bNetworkReady = false;
- }
- public function startPlaybackClass( fn:String = "" ):Boolean{
- if ( _bNetworkReady && _iStatus == INACTIVE_STAT ){
- _outStream = new NetStream (_nc);
- _outStream.client = this;
- _outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
- _outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
- _outStream.addEventListener(NetStatusEvent.NET_STATUS, playNetStatusHandler);
- if (fn == "") fn = _internalFilename;
- _outStream.play("flv:" + fn);
- _iStatus = PLAYBACK_STAT;
- }
- return false;
- }
- public function genRandomStr( len:int ):String {
- var tStr:String = new String();
- var lookup:String = "abcdefghijklmnopqrstuvwxyz1234567890_";
- for (var i:int = 0;i < len;i++){
- tStr += lookup.charAt(Math.floor(Math.random()*lookup.length));
- }
- return tStr;
- }
- public function startRecordClass():Boolean{
- if (_bMicReady && _bNetworkReady && _iStatus == INACTIVE_STAT ){
- _inStream = new NetStream(_nc);
- //_outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
- //_outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
- _inStream.addEventListener(NetStatusEvent.NET_STATUS, catchAll);
- _inStream.attachAudio(_microphone);
- //_internalFilename = genRandomStr(15);
- _inStream.publish(_internalFilename,"record");
- _iStatus = RECORDING_STAT;
- return true;
- }
- return false;
- }
- public function stopPlayback():void{
- if (_iStatus == PLAYBACK_STAT ){
- _outStream.pause();
- _outStream.play(false);
- _iStatus = INACTIVE_STAT;
- }
- }
- public function stopRecord():void{
- if (_inStream) _inStream.close();
- _inStream = null;
- _iStatus = INACTIVE_STAT;
- }
- //---------------clases end
- public static const RECORD_LABEL:String = "Record";
- public static const STOP_LABEL:String = "Stop";
- public static const PLAY_LABEL:String = "Play";
- public static const RECORD_STYLE:String = "recordStyle";
- public static const STOP_STYLE:String = "stopStyle";
- public static const PLAY_STYLE:String = "playStyle";
- private var recSprite:Sprite = new Sprite();
- private var stopRecSprite:Sprite = new Sprite();
- private var stopSprite:Sprite = new Sprite();
- private var playSprite:Sprite = new Sprite();
- private var submitSprite:Sprite = new Sprite();
- private function updateResultDisplay( str:String ):void {
- }
- private function init():void{
- // Current release of FMS only understands AMF0 so tell Flex to
- NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
- SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
- //NetStream.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
- openConnection();
- setupMicrophone();
- addEventListener(Event.ENTER_FRAME, showMicLevel);
- //_recorder.addEventListener(Event.COMPLETE, onVideoStop);
- var squareSize:uint = 10;
- //submitSprite.graphics.beginFill(0x00FF00);
- submitSprite.graphics.lineStyle(2,0x00FF00,1);
- submitSprite.graphics.moveTo(0,0);
- submitSprite.graphics.lineTo(squareSize/2,squareSize);
- submitSprite.graphics.lineTo(squareSize-2,0);
- submitSprite.x = 5;
- submitSprite.y = 5;
- //submitSprite.graphics.endFill();
- playSprite.graphics.beginFill(0x000000);
- playSprite.graphics.moveTo(0,0);
- playSprite.graphics.lineTo(squareSize/2,squareSize/2);
- playSprite.graphics.lineTo(0,squareSize);
- playSprite.graphics.lineTo(0,0);
- playSprite.graphics.endFill();
- playSprite.x=8;
- playSprite.y=5;
- stopRecSprite.graphics.beginFill(0x000000);
- stopRecSprite.graphics.drawRect(0, 0, squareSize, squareSize);
- stopRecSprite.graphics.endFill();
- stopRecSprite.x=5;
- stopRecSprite.y=6;
- stopSprite.graphics.beginFill(0x000000);
- stopSprite.graphics.drawRect(0, 0, squareSize, squareSize);
- stopSprite.graphics.endFill();
- stopSprite.x=5;
- stopSprite.y=6;
- recSprite.graphics.beginFill(0xff0000, 1);
- recSprite.graphics.drawCircle(0, 0, 4);
- recSprite.x=10;
- recSprite.y=10;
- //recordButton.addChild(recSprite);
- recCanvas.rawChildren.addChild(recSprite);
- recCanvas.rawChildren.addChild(stopRecSprite);
- playCanvas.rawChildren.addChild(playSprite);
- playCanvas.rawChildren.addChild(stopSprite);
- submitCanvas.rawChildren.addChild(submitSprite);
- stopRecSprite.visible = false;
- stopSprite.visible = false
- //recordButton.setStyle("icon",recIcon);
- //playButton.setStyle("icon", playIcon);
- //sumbmitButton.setStyle("icon", checkmarkIcon);
- }
- private function onSubmitClick():void
- {
- navigateToURL(new URLRequest("http://talknicer.com/vadim.cgi?filename="+fileName),"_self");
- }
- private function onVideoStop(evt:Event):void
- {
- //playButton.styleName = PLAY_STYLE;
- playButton.label = PLAY_LABEL;
- playSprite.visible = true;
- stopSprite.visible = false;
- //playButton.setStyle("icon", playIcon);
- }
- private function micSlider_changeHandler(event:SliderEvent):void
- {
- if (getMic)
- _microphone.gain = 100-event.value;
- }
- private function showMicLevel(event:Event):void
- {
- if (getMic)
- {
- micLeval.setProgress(100-getMic.activityLevel, 100);
- }
- }
- private function startPlayback():void {
- switch ( getStatus() ){
- case PLAYBACK_STAT:
- stopPlayback();
- //playButton.styleName = PLAY_STYLE;
- playButton.label = PLAY_LABEL;
- playSprite.visible = true;
- stopSprite.visible = false;
- //playButton.setStyle("icon",playIcon);
- break;
- case RECORDING_STAT:
- stopRecord();
- break;
- case INACTIVE_STAT:
- startPlaybackClass();
- //playButton.styleName = STOP_STYLE;
- playButton.label = STOP_LABEL;
- playSprite.visible = false;
- stopSprite.visible = true;
- //playButton.setStyle("icon",stopIcon);
- break;
- }
- }
- private function startRecord():void {
- switch ( getStatus() ){
- case RECORDING_STAT:
- stopRecord();
- recordButton.label = RECORD_LABEL;
- //recordButton.setStyle("icon",recIcon);
- stopRecSprite.visible = false;
- recSprite.visible = true;
- //recordButton.styleName = RECORD_STYLE;
- playButton.enabled = true;
- sumbmitButton.enabled = true;
- return;
- case PLAYBACK_STAT:
- stopPlayback();
- return;
- case INACTIVE_STAT:
- startRecordClass();
- recordButton.label = STOP_LABEL;
- recSprite.visible = false;
- stopRecSprite.visible = true;
- //recordButton.setStyle("icon", stopIcon);
- //recordButton.styleName = STOP_STYLE;
- return;
- }
- }
- ]]>
- </mx:Script>
- <mx:Button enabled="false" left="0" right="0" bottom="0" top="0"/>
- <mx:Canvas left="0" right="0" bottom="0" mouseChildren="false" top="0" />
- <mx:Text id="titleText" text="Audio Recorder" width="317" height="50" textAlign="center" color="#0000FF" fontSize="24" fontWeight="bold" horizontalCenter="0" top="20"/>
- <mx:VSlider height="60" x="27" y="90" maximum="100" minimum="0" value="50" id="micSlider" change="micSlider_changeHandler(event)" allowTrackClick="true" liveDragging="true" enabled="true" width="28"/>
- <mx:Canvas x="78" y="38" width="88" height="94">
- <mx:ProgressBar mode="manual" label=" " height="9" id="micLeval" rotation="90" width="50" x="0" y="56" paddingLeft="0"/>
- </mx:Canvas>
- <mx:Form x="71" y="88">
- <mx:HBox x="50" horizontalGap="0">
- <mx:Canvas id="recCanvas">
- <mx:Button id="recordButton" click="startRecord()" width="75"
- borderColor="#F71F09"
- color="#009900"
- label="{RECORD_LABEL}"
- />
- </mx:Canvas>
- <mx:Canvas id="playCanvas">
- <mx:Button enabled="false" id="playButton" click="startPlayback()" width="75"
- borderColor="#F71F09"
- color="#009900"
- label="{PLAY_LABEL}" />
- </mx:Canvas>
- <mx:Canvas id="submitCanvas">
- <mx:Button label="Submit" id="sumbmitButton" click="onSubmitClick()"
- enabled="false" fontWeight="normal"
- />
- </mx:Canvas>
- </mx:HBox>
- </mx:Form>
- </mx:Application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement