📄 capturedemo.java
字号:
// CaptureDemo.java
import javax.media.*;
import javax.media.control.*;
import javax.media.datasink.*;
import javax.media.format.*;
import javax.media.protocol.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class CaptureDemo extends Frame
implements ActionListener, DataSinkListener
{
StateHelper sh;
Processor processor;
DataSink filewriter = null;
CaptureDemo (String title)
{
super (title);
Panel p = new Panel ();
Button b = new Button ("Start");
b.addActionListener (this);
p.add (b);
b = new Button ("Stop");
b.addActionListener (this);
p.add (b);
add (p);
CaptureDeviceInfo di = null;
AudioFormat a = new AudioFormat (AudioFormat.LINEAR,
8000, 16, 1);
Vector deviceList;
deviceList = CaptureDeviceManager.getDeviceList (a);
if (deviceList.size () > 0)
di = (CaptureDeviceInfo) deviceList.firstElement ();
else
terminate ("Could not find 44,100 Hz 16-bit audio device.");
try
{
processor = Manager.createProcessor (di.getLocator ());
}
catch (IOException e)
{
terminate (e.toString ());
}
catch (NoProcessorException e)
{
terminate ("Could not find processor.");
}
// Create a helper to ensure certain states are reached.
sh = new StateHelper (processor);
// Configure the processor
if (!sh.configure (10000))
terminate ("Could not configure processor.");
// Set the output content type.
FileTypeDescriptor f;
f = new FileTypeDescriptor (FileTypeDescriptor.BASIC_AUDIO);
processor.setContentDescriptor (f);
// Realize the processor.
if (!sh.realize (10000))
terminate ("Could not realize processor.");
setSize (200, 60);
setResizable (false);
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
String cmd = e.getActionCommand ();
((Component) e.getSource ()).setEnabled (false);
if (cmd.equals ("Start"))
{
// Get the processor's output data source.
DataSource source = processor.getDataOutput ();
// Create a File protocol MediaLocator with the location of
// the file, to which the data is to be written.
MediaLocator dest = new MediaLocator ("file:out.au");
// Create a datasink to perform file writing. Add a data
// sink listener to wait for an end of stream event. Open
// the sink to ensure that it is writable.
try
{
filewriter = Manager.createDataSink (source, dest);
filewriter.addDataSinkListener (this);
filewriter.open ();
}
catch (NoDataSinkException e2)
{
terminate ("Could not create data sink.");
}
catch (IOException e2)
{
terminate (e2.toString ());
}
// If the Processor implements StreamWriterControl, we can
// call setStreamSizeLimit to set a limit on the size of the
// file that is written.
StreamWriterControl swc = (StreamWriterControl)
processor.getControl ("javax.media.control." +
"StreamWriterControl");
// Set limit to 5MB
if (swc != null)
swc.setStreamSizeLimit (5000000);
// Start the data transfer.
try
{
filewriter.start ();
}
catch (IOException e2)
{
System.out.println (e2);
}
processor.start ();
return;
}
processor.stop ();
processor.close ();
}
public void dataSinkUpdate (DataSinkEvent e)
{
if (e instanceof EndOfStreamEvent)
{
filewriter.close ();
System.exit (0);
}
}
void terminate (String msg)
{
System.out.println (msg);
System.exit (-1);
}
public static void main (String [] args)
{
new CaptureDemo ("Capture Demo");
}
}
class StateHelper implements javax.media.ControllerListener
{
Player player = null;
boolean configured = false;
boolean realized = false;
boolean prefetched = false;
boolean failed = false;
boolean closed = false;
public StateHelper (Player p)
{
player = p;
p.addControllerListener (this);
}
public boolean configure (int timeOutMillis)
{
long startTime = System.currentTimeMillis ();
synchronized (this)
{
if (player instanceof Processor)
((Processor) player).configure ();
else
return false;
while (!configured && !failed)
{
try
{
wait (timeOutMillis);
}
catch (InterruptedException ie)
{
}
if (System.currentTimeMillis () - startTime > timeOutMillis)
break;
}
}
return configured;
}
public boolean realize (int timeOutMillis)
{
long startTime = System.currentTimeMillis ();
synchronized (this)
{
player.realize ();
while (!realized && !failed)
{
try
{
wait (timeOutMillis);
}
catch (InterruptedException ie)
{
}
if (System.currentTimeMillis () - startTime > timeOutMillis)
break;
}
}
return realized;
}
public boolean prefetch (int timeOutMillis)
{
long startTime = System.currentTimeMillis ();
synchronized (this)
{
player.prefetch ();
while (!prefetched && !failed)
{
try
{
wait (timeOutMillis);
}
catch (InterruptedException ie)
{
}
if (System.currentTimeMillis () - startTime > timeOutMillis)
break;
}
}
return prefetched && !failed;
}
public void close ()
{
synchronized (this)
{
player.close ();
while (!closed)
{
try
{
wait (100);
}
catch (InterruptedException ie)
{
}
}
}
player.removeControllerListener (this);
}
public synchronized void controllerUpdate (ControllerEvent ce)
{
if (ce instanceof RealizeCompleteEvent)
realized = true;
else
if (ce instanceof ConfigureCompleteEvent)
configured = true;
else
if (ce instanceof PrefetchCompleteEvent)
prefetched = true;
else
if (ce instanceof ControllerErrorEvent)
failed = true;
else
if (ce instanceof ControllerClosedEvent)
closed = true;
else
return;
notifyAll();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -