Dear users,
I have developed a webcam recorder and a player which first records the input from webcam and then plays it.So far all the things are working well i.e. my application first records and then plays but during playback if I try to pause the video,it pauses and if I try to resume it,it doesn’t work.
I have used two NetStream ,one for recording purpose and one for playing purpose.
Following is my code,the first portion consists of recording and latter portion for playing.
Here I used ‘ns’ NetStream for recording and ‘nsStream’ NetStream for playing.
vidDisplay is video for recording and vidPlay video for playing.
'plbar’is the movieclip having recording buttons and mcVideoControls is the movieclip having playing buttons.
While recording is going on plbar and vidDisplay are visible and while playing mcvideoControls and vidPlay are visible.
The problem I am facing is in function playClicked and I marked it with **
ITS NEARLY ONE WEEK GONE AND I AM STUCK IN THIS PROBLEM,SO PLEASE HELP ME ON THIS AND YOUR HELP WILL BE VERY PRECIOUS TO ME
package irul
{
import flash.display.;
import flash.net.;
import flash.events.;
import flash.media.;
import flash.geom.;
import flash.utils.Timer;
import flash.text.;
public class Recorder extends MovieClip
{
private var connURL:String = “rtmp://MYSERVERIP/videostreaming/myvideos”;
private var conn:NetConnection;
private var ns:NetStream;
private var nsStream:NetStream;
private var cam:Camera;
private var mic:Microphone;
private var disp_time:Timer;
private var infoObj:Object;
private var movLoaded:Boolean = false;
public var vidPlay:Video;
private var timeLeft:Timer = new Timer(1000,0);
private var timeCount:int = 1000;
private var timeleft:int = 10;
private var videoName:String = "_1Vid";
private const BUFFER_TIME:Number = 8;
private const SMOOTHING:Boolean = true;
private var bolLoaded:Boolean;
// object holds all meta data
private var objInfo:Object;
private var tmrDisplay:Timer;
NetConnection.defaultObjectEncoding=flash.net.ObjectEncoding.AMF3;
//CONSTRUCTOR
public function Recorder()
{
Connectors();
}
//FOR RTMP CONNECTION
public function Connectors():void
{
mcVideoControls.visible = false;
conn = new NetConnection();
conn.connect(connURL);
conn.client= this;
conn.addEventListener(NetStatusEvent.NET_STATUS,nethandler);
conn.objectEncoding=flash.net.ObjectEncoding.AMF3;
}
//NET STATUS HANDLER FUNCTION
private function nethandler(evt:NetStatusEvent):void
{
var info_status:String = evt.info.code;
if(info_status == "NetConnection.Connect.Success")
{
connStream();
buttonListeners();
}
else if(info_status=="NetStream.Unpause.Notify")
{
trace("Unpause is done :" + info_status );
}
else
{
trace(info_status);
}
}
//NET STREAM CONNECTION FOR RECORDING
private function connStream():void
{
ns = new NetStream(conn);
ns.addEventListener(NetStatusEvent.NET_STATUS,nethandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,async_handler);
setupCam();
}
//CAMERA SETUP FUNCTION
private function setupCam():void
{
cam = new Camera();
mic = new Microphone();
cam = Camera.getCamera();
cam.setMode(320, 238, 15);
cam.setQuality(0,80);
mic = Microphone.getMicrophone();
mic.rate=21;
ns.attachCamera(cam);
ns.attachAudio(mic);
vidDisplay.attachCamera(cam);
}
//EVENTLISTENER FUNCTION FOR VARIOUS RECORDING FUNCTION
private function buttonListeners():void
{
plbar.record_btn.addEventListener(MouseEvent.CLICK,connectCam);
plbar.stop_btn.addEventListener(MouseEvent.CLICK,stopRecord);
plbar.play_btn.addEventListener(MouseEvent.CLICK,playRecord);
}
//FOR RECORDING
private function connectCam(evts:MouseEvent):void
{
ns.publish(videoName,“record”);
}
//FOR STOPPING RECORDS
private function stopRecord(evt:MouseEvent):void
{
ns.close();
vidDisplay.clear();
}
//FOR PLAYING RECORD
private function playRecord(evt:MouseEvent):void
{
plbar.visible = false;
vidDisplay.visible = false;
mcVideoControls.visible = true;
createNetstream();
initVideoPlayer();
}
///RECORD PORTION FINISHED
public function async_handler(evt:AsyncErrorEvent):void
{
trace(evt.toString());
}
///PLAY PORTION STARTS HERE
//NETSTREAM FOR PLAYING
private function createNetstream():void
{
nsStream = new NetStream(conn);
nsStream.client =this;
nsStream.addEventListener(NetStatusEvent.NET_STATUS,nethandler);
nsStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,async_handler);
nsStream.bufferTime = BUFFER_TIME;
vidPlay = new Video();
vidPlay.name = "vidPlay";
addChild(vidPlay);
vidPlay.smoothing = SMOOTHING;
vidPlay.attachNetStream(nsStream);
}
//INITIALIZING VIDEO PLAYER CONTROLS
public function initVideoPlayer():void
{
bolLoaded = true;
mcVideoControls.btnPause.visible = false;
mcVideoControls.back_btn.addEventListener(MouseEvent.CLICK,goBack);
mcVideoControls.btnPause.addEventListener(MouseEvent.CLICK,playClicked);
mcVideoControls.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);
mcVideoControls.btnStop.addEventListener(MouseEvent.CLICK, stopClicked);
}
**private function playClicked(e:MouseEvent):void
{
if(e.target.name == “btnPlay”)
{
mcVideoControls.btnPause.visible = true;
mcVideoControls.btnPlay.visible = false;
if(bolLoaded)
{
nsStream.play(videoName,0,-1);
bolLoaded = false;
}
else
{
nsStream.resume();
}
}
else
{
mcVideoControls.btnPause.visible = false;
mcVideoControls.btnPlay.visible = true;
nsStream.pause(); ]
}
}
//STOPPING VIDEO PLAYER
private function stopClicked(e:MouseEvent):void
{
// calls stop function
stopVideoPlayer();
}
public function onMetaData(info:Object):void
{
}
//TO GO BACK FOR RECORDING
public function goBack(evt:MouseEvent):void
{
mcVideoControls.visible = false;
vidDisplay.visible = true;
plbar.visible = true;.
nsStream.close();
removeChild(vidPlay);
}
//FUNCTION THAT STOPS PLAYING
private function stopVideoPlayer():void
{
bolLoaded =true;
nsStream.close();
vidPlay.clear();
mcVideoControls.btnPause.visible = false;
mcVideoControls.btnPlay.visible = true; ]
}
//FUNCTION CALLED AFTER COMPLETE PLAYING
public function onPlayStatus(info:Object):void
{
stopVideoPlayer();
}
///PLAY PORTION FINISHED
}
}