about recording video
Hi friends in our project we need to record a video from webcam .
I came to know about JMF .I configured it properly and wrote simple code to access to webcam following manual of JMF.I tried my best to modify that code to record a video and save it to file.But i could not.
I tried artical and codes of Mr. Gal Ratner from about.java.com
but it fails in detecting my webcam.
If any one has tried to work on such code please help me
I will be very thankful if anyone can give me code
you can mail me on cddhesh1000@gmail.com
[567 byte] By [
C-ddhesha] at [2007-11-26 21:28:29]

# 1
This code works. You just have to press stop button to stop recording.
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import javax.media.datasink.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
class Abc implements ControllerListener, ActionListener
{
DataSource ds;
DataSink sink;
Processor pr;
MediaLocator mc;
JFrame jframe=new JFrame();
JButton jbutton=new JButton("Stop");
JPanel jpanel=new JPanel();
CaptureDeviceManager cdm=new CaptureDeviceManager();
CaptureDeviceInfo cdi;
MediaLocator cammc;
public void capture() throws Exception
{
jframe.setSize(300,300);
jpanel.setLayout(new BoxLayout(jpanel,BoxLayout.Y_AXIS));
jpanel.add(jbutton);
jframe.add(jpanel);
jbutton.addActionListener(this);
mc=new MediaLocator("file:C:/Workspace/aaaaa.mpg");
cdi= (CaptureDeviceInfo) cdm.getDeviceList(new VideoFormat(VideoFormat.RGB)).elementAt(0);
MediaLocator cammc=cdi.getLocator();
pr=Manager.createProcessor(cammc);
pr.addControllerListener(this);
pr.configure();
}
public void actionPerformed(ActionEvent e)
{
try
{
pr.stop();
sink.stop();
sink.close();
}
catch(Exception ep)
{
}
}
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof ConfigureCompleteEvent)
{
System.out.println ("ConfigureCompleteEvent");
Processor p = (Processor)e.getSourceController();
System.out.println ("ConfigureCompleteEvent");
processConfigured(p);
}
if(e instanceof RealizeCompleteEvent)
{
try
{
Processor p = (Processor)e.getSourceController();
System.out.println ("RealizeCompleteEvent");
MediaLocator dest = new MediaLocator("file:C:/Workspace/ring3.mpg");
sink = Manager.createDataSink(p.getDataOutput(), dest);
processRealized(p);
}
catch(Exception ep)
{
}
}
if(e instanceof ControllerClosedEvent)
{
System.out.println ("ControllerClosedEvent");
}
if(e instanceof EndOfMediaEvent)
{
System.out.println ("EndOfMediaEvent");
Processor p = (Processor)e.getSourceController();
p.stop();
}
if(e instanceof StopEvent)
{
System.out.println ("StopEvent");
Processor p = (Processor)e.getSourceController();
p.close();
try
{
sink.stop();
sink.close();
}
catch(Exception ee)
{
}
}
}
public void processConfigured(Processor p)
{
System.out.println("Entered processConfigured");
p.setContentDescriptor (new FileTypeDescriptor (FileTypeDescriptor.MSVIDEO ));
p.realize();
}
public void processRealized(Processor p)
{
jframe.setVisible(true);
pr=p;
System.out.println("Entered processRealized");
try
{
sink.addDataSinkListener(new DataSinkListener()
{
public void dataSinkUpdate(DataSinkEvent e)
{
if(e instanceof EndOfStreamEvent)
{
System.out.println ("EndOfStreamEvent");
sink.close();
}
}
});
sink.open();
sink.start();
}
catch(Exception eX)
{
}
System.out.println("Just before start");
p.start();
}
}
public class Camera2
{
public static void main(String args[]) throws Exception
{
Abc a=new Abc();
a.capture();
}
}
# 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException :0>=0
at java.util.Vector.elementAt(Vector.java:432)
at Abc.capture(Camera2.java:40)
at Camera2.main(Camera2.java:164)
Press any key to continue............
this is the first error line
cdi= (CaptureDeviceInfo) cdm.getDeviceList(new VideoFormat(VideoFormat.RGB)).elementAt(0);
this is 2nd line
a.capture();
I tried importing java.lang.*; but gives me the same error
i told you about the code of Mr. Gal Ratner from about.java.com, It is giving me the same error.
what else i'l have to do now?please help me.I have another code with me that is also from this forum that works well ie it only gets access to webcam in a frame
import javax.media.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.media.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Test1 extends JFrame implements ControllerListener
{
boolean stateTransitionOK = true;
Object waitSync = new Object();
Processor p;
public Test1()
{
try
{
p = Manager.createProcessor(new MediaLocator("vfw://0"));
p.addControllerListener(this);
p.configure();
waitForState(p.Configured);
p.setContentDescriptor(null);
TrackControl tc[] = p.getTrackControls();
TrackControl videoTrack = tc[0];
p.prefetch();
waitForState(p.Prefetched);
getContentPane().add(p.getVisualComponent());
p.start();
pack();
setVisible(true);
}
catch(Exception e){System.out.println(e);}
}
public static void main(String[] args)
{
Test1 t1 = new Test1();
}
public void controllerUpdate(ControllerEvent evt)
{
if (evt instanceof ConfigureCompleteEvent || evt instanceof RealizeCompleteEvent || evt instanceof PrefetchCompleteEvent)
{
synchronized (waitSync)
{
stateTransitionOK = true;
waitSync.notifyAll();}
}
else if (evt instanceof ResourceUnavailableEvent)
{
synchronized (waitSync)
{
System.out.println("ResourceUnavailable");
stateTransitionOK = false;
waitSync.notifyAll();
}
}
else if (evt instanceof EndOfMediaEvent)
{
p.close();
System.exit(0);
}}
boolean waitForState(int state)
{
synchronized (waitSync)
{
try
{while (p.getState() != state && stateTransitionOK)
waitSync.wait();
}
catch (Exception e) {}
}
return stateTransitionOK;
}
}
//TrackControl tc[] = p.getTrackControls();
TrackControl videoTrack = tc[0];
i m doubtful abt this tc[0] in comparison with that elementAt(0);