header
ask question
Click here to ask Question Now Its free No registration required. Flash, Flex, Flash Media Server, ActionScript,Adobe Air. Most questions receive a response in an hour.
Amina
Points:260
Posts:11

2/24/2010 10:54:13 AM

Title: recording webcam


hi
someone has an idea on how to record her webcam using flex builder 3 and Red 5 in mp3



Ruby
Points: 0
Posts:0
2/24/2010 12:32:59 PM



A simple AIR application that records a video stream from a webcam and saves a temporary FLV file to the desktop. The UI will consist of a window displaying the current video feed and a button to start and stop recording.

Execution:

1. Create a WebcamPanel class that extends off of the Panel class and will display the video feed:

This class will basically consist of a method "insertWebcamVideo" that will build the UIComponent and attach a Video object to it.


      public function insertWebcamVideo():void{

          var videoHolder:UIComponent = new UIComponent();
          var camera:Camera = Camera.getCamera();
          video = new Video(camera.width*2, camera.height*2);
          video.attachCamera(camera);
          videoHolder.addChild(video);
          addChild(videoHolder);
          videoHolder.y =10; 

      }
   2. Attach the WebcamPanel our main mxml file:

      <webcam:WebcamPanel id="webCam_pnl" width="360" height="320" />
   3. Create a button to toggle record off and on:

      <mx:Button id="record_btn" label="Record" click="recordPushed()" />
   4. Setup the recordPushed method to initialize our FLV file via the SimpleFLVWriter class if the label is "Record." In addtion to writing the header information for the file, this code sets up the interval that will take snapshots of the webcam feed:


      if(record_btn.label == "Record"){

          curVidFile = File.createTempFile();
          myWriter.createFile(curVidFile, 360,320, 10);
          recordInterval = setInterval(recordVid, 100);
          record_btn.label = "Stop";

      }

   5. For the else clause of our "if" statement we will close up the FLV file and copy it to the desktop:


      }else{


          clearInterval(recordInterval);
          myWriter.closeFile();
          var newFileNameStr:String = curVidFile.name.split(".tmp").join(".flv");
          var desFile:File = new File(File.desktopDirectory.nativePath +"\\" +newFileNameStr);
          curVidFile.copyTo(desFile);
          record_btn.label = "Record"; 

      }
   6. The code for the snap shot interval:


      private function recordVid():void{

          var snapshot:BitmapData = new BitmapData(320, 240, true);
          snapshot.draw(webCam_pnl.video);
          myWriter.saveFrame( snapshot ); 

      }
   7. Compile your project




scripter
Points: 0
Posts:0
2/24/2010 12:45:00 PM



code for recording audio using red5


<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" 
	width="323"
	height="150"
	backgroundGradientAlphas="[1.0, 1.0]" 
	backgroundGradientColors="[#f4f4f4, #cccccc]"

	creationComplete="init()">

	<mx:Style>
		.playStyle {icon: Embed(source="icons/play_grey.png");}
		.pauseStyle {icon: Embed(source="icons/pause_grey.png");}
	</mx:Style>



	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import flash.net.NetStream;

			private var myMic:Microphone;
			private var nc:NetConnection;
			private var ns:NetStream;
			private var recordingState:String = "idle";
			private var myDuration:Number = 0;
			private var myServer:String = null;
			private var mySoundFile:String = null;
			private var myTimer:Timer = null;
			private var playbackTimer:Timer = null;
			private var timeLimit:Number = 0;

			private function init():void {

				// Not yet connected to the server. Disable the buttons.
				record_btn.enabled = false;
				stop_btn.enabled = false;
				play_btn.enabled = false;

				myServer = Application.application.parameters.myServer;
				mySoundFile = Application.application.parameters.mySound;
				timeLimit = Application.application.parameters.timeLimit;
				
				/*
				myServer="rtmp://dev.llc.msu.edu/oflaDemo/recordYourself";
				mySoundFile="test002";
				timeLimit = 100;
				*/
				
				initMic("0xcccccc");
				
				myTimer = new Timer(1000, timeLimit);
				myTimer.addEventListener("timer", timerHandler);
				timeText.text = "Recording time limit: " + createCountdown(0);

				record_btn.addEventListener(MouseEvent.CLICK, record_start);
				stop_btn.addEventListener(MouseEvent.CLICK, record_stop);
				play_btn.addEventListener(MouseEvent.CLICK, record_play);
				
				setupNC();

				addEventListener(Event.ENTER_FRAME, showMicLevel);

				myMic = Microphone.getMicrophone();
				myMic.setSilenceLevel(0);
				myMic.rate = 22;
				statusText.text = "connecting";

			}
			
			private function setupNC():void{
				nc = new NetConnection();
				nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
				nc.connect(myServer);
			}
			
			private function netStatus(event:NetStatusEvent):void {
				switch (event.info.code){
					case "NetConnection.Connect.Success" :
						setStatus("Connected");
						ns = new NetStream(nc);
						ns.attachAudio(myMic);
						record_btn.enabled = true;
						stop_btn.enabled = true;
						play_btn.enabled = true;
						break;
					case "NetStream.Record.Start" :
						setStatus("recording...");
						break;
					case "NetStream.Record.Stop" :
						setStatus("stopped recording");
						break;
					case "NetStream.Buffer.Full" :
						setStatus("playing");
						break;
					case "NetStream.Play.Start" :
						setStatus ("buffering");
						break;
					case "NetStream.Seek.Notify" :
						setStatus("Seeking");
						break;
					case "NetStream.Play.Reset" :
						setStatus("pausing");
						break;
					case "NetStream.Play.Stop" :
						//setStatus("stopping");
						break;
					case "NetStream.Buffer.Flush" :
						//setStatus("flushing buffer");
						break;
					case "NetStream.Play.Complete" :
						setStatus("Playback complete");
						break;
				}
			}
			
			private function record_start(event:Event):void{
				initMic("0xff0000");
				ns.close();
				ns = new NetStream(nc);
				ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
				ns.attachAudio(myMic);
				ns.publish(mySoundFile, "record");
				recordingState = "recording";
				setPBstatus('recording');
				myTimer.start();
			}
			
			private function record_stop(event:Event):void{
				switch (recordingState) {
					case "recording" :
						ns.close();
						myTimer.reset();
						timeText.text = createCountdown(0);
						break;
						setStatus("stopped");
					case "playing" :
						//Alert.show("Stopping now");
						ns.play(false);
						ns.close();
						playProgress.visible = false;
						micLevel.visible = true;
						ns = new NetStream(nc);
						ns.attachAudio(myMic);
						break;
				}
				recordingState = "idle";
				setPBstatus('ready');
				initMic("0xcccccc");
			}

			private function record_play(event:Event):void{
				switch(recordingState){
				case "idle" :
					play_btn.styleName = "pauseStyle";
					play_btn.label = "pause";
					playSound();
					break;
				
				case "playing" :
					recordingState = "paused";
					play_btn.styleName = "playStyle";
					play_btn.label = "play";
					ns.togglePause();
					break;
				
				case "paused" :
					play_btn.label = "pause";
					play_btn.styleName = "pauseStyle";
					recordingState = "playing";
					ns.togglePause();
					break;
				}
			
			}

		private function playSound():void{
			playProgress.visible = true;
			micLevel.visible = false;

			recordingState = "playing";
			ns.close();
			setStatus("buffering");
			var myClient:Object = new Object();
			ns = new NetStream(nc);
			ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			ns.bufferTime = 5;
			ns.client = myClient;
			myClient.onMetaData = function(myMeta:Object):void{
				myDuration = myMeta["duration"];
				
				playbackTimer = new Timer(1000, myDuration);
				playbackTimer.addEventListener("timer", playbackTimerHandler);
				timeText.text = createTime(0);
				playbackTimer.start();
				
			}
			myClient.onPlayStatus = function(myPBstatus:Object):void{
				if(myPBstatus["code"] == "NetStream.Play.Complete"){
					setStatus("Playback complete");
					recordingState = "idle";
					play_btn.styleName = "playStyle";
					ns = new NetStream(nc);
					ns.attachAudio(myMic);
					playProgress.value = 0;
					playProgress.visible = false;
					micLevel.visible = true;
				}
				recordingState = "idle";
			}
			ns.play(mySoundFile);
		}

        public function playbackTimerHandler(event:TimerEvent):void {
        	timeText.text = createTime(ns.time);
        	playProgress.value = (ns.time / myDuration) * 10;
        	//timeText.text = ns.time.toString();
   	    }

		private function scanPosition():void {
			ns.play(false);
			//divide by 10 because the Slider values go from 0 - 10 and we want a value
			//between 0 - 1.0
			ns.play(mySoundFile);
			ns.seek (playProgress.value/10 * myDuration);
		}

			private function showMicLevel(event:Event):void{
				switch (recordingState){
					case "recording" :
						micLevel.setProgress(myMic.activityLevel, 100);
						break;
					case "idle" :
						micLevel.setProgress(myMic.activityLevel, 100);
						break;
					case "playing" :
						micLevel.setProgress(ns.time, myDuration);
						timeText.text = createCounter(ns.time);
						break;
				}
			}

			private function setPBstatus(msg:String):void{
				micLevel.label = msg;
			}
			
			private function initMic(myColor:String):void{
				micLevel.setStyle("barColor", myColor);
			}

			private function createCountdown(time:int):String{

				time = timeLimit - time;
				var myBuffer:String = "0";

				var currentMins:int = Math.floor(time/60);
				var currentSecs:int = (time - (currentMins*60));
				var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString()) 
				+ ":" 
				+ ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());
				
				return mySecs;
			}

			private function createCounter(time:int):String{

				var myBuffer:String = "0";

				var currentMins:int = Math.floor(time/60);
				var currentSecs:int = (time - (currentMins*60));
				var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString()) 
				+ ":" 
				+ ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());
				
				return mySecs;
			}

		private function createTime(time:int):String{

			//time = timeLimit - time;
			var myBuffer:String = "0";

			var currentMins:int = Math.floor(time/60);
			var currentSecs:int = (time - (currentMins*60));
			var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString()) 
			+ ":" 
			+ ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());
				
			return mySecs;
		}

	        public function timerHandler(event:TimerEvent):void {

	        	if(myTimer.currentCount < timeLimit){
	    	        timeText.text = createCountdown(myTimer.currentCount);
	        	} else {
	        		myTimer.reset();
	        		record_stop(null);
	        	}

	   	    }

		private function setStatus(msg:String):void{
			statusText.text = msg;
		}

		]]>
	</mx:Script>

	<mx:Text id="statusText" text="" width="320" height="25" y="124" x="3" textAlign="center"/>

	<mx:Text id="timeText" text="00:00" width="323" height="23" y="109" x="0" textAlign="center"/>
	<mx:Text id="titleText" text="Audio Recorder" width="317" height="50" y="0" x="3" textAlign="center" color="#0000FF" fontSize="24" fontWeight="bold"/>
	<mx:ProgressBar x="0" y="36" mode="manual" id="micLevel" label="" labelPlacement="bottom" width="320" themeColor="#ff0000" fontSize="10" fontWeight="normal"/>

	<mx:ControlBar x="0" y="63" width="320" height="45" horizontalAlign="center">
   
		<mx:Button 
			label="record" 
			id="record_btn"
			width="75" 
			borderColor="#F71F09" 
			color="#009900"
			icon="@Embed(source='icons/record_grey.png')"			
		/>
		<mx:Button 
			label="stop" 
			id="stop_btn" 
			width="75" 
			height="35"
			color="#009900"
			icon="@Embed(source='icons/stop_grey.png')"			
		/>
		<mx:Button 
			label="play" 
			id="play_btn" 
			width="75" 
			borderColor="#1005F6" 
			color="#009900"
			styleName="playStyle"
		/>   
	</mx:ControlBar>
	<mx:HSlider change="scanPosition()" id="playProgress" showTrackHighlight="true" width="320" x="0" y="34" visible="false"/>
   	<mx:Text id="versionText" text="v2009.1.22.03" width="320" height="15" y="134" x="3" textAlign="right" fontSize="8"/>
   
   
</mx:Application>


Ruby
Points: 0
Posts:0
2/24/2010 1:11:48 PM



Amina you will need another file for flv recorder
SimpleFLVWriter .as

you can get it from here

http://www.tdotblog.info/?q=node/1

MaxFlash
Points: 0
Posts:0
2/25/2010 4:47:22 AM



recording of mp3 at server side is not possible due to following reasons:

It requires mp3 compressor on flash player which is not present , to do this on server side it requires heavy processing so not much streams can be saved as mp3. How ever you can use some FLV to MP3 converter to do this. But i think converting to mp3 requires some licensing.

Amina
Points: 0
Posts:0
2/25/2010 6:08:36 AM



Thank you for your responses they are very helpful.I learn with you :D


Post your Reply
Name  

Email

Type your Reply or Answer

Are you human? What is 5+3 



Members Login

Email  
Password
Forgot Password





This website focus on: Flash | Flex | FMS | RED5 | WOWZA | Flash Media Server | Adobe AIR | ActionScript,Flash Solutions | Flash Question | Flash Answers | Flash Developers | Flash Problem, Flash Help, Flash bugs, Flash workaround | Flash Blog | Flex Question Answers | Flash Forum | Flex Development | Actionscript development | Flash development | Adobe AIR development
Copyright © 2008 AskMeFlash.com. All rights reserved. Privacy Policy | Terms & Conditions