Java t或使用已注册的设备 //应用程序已知。 ////////////////////////////////////////////////////////// 布尔useKnownDevices=false; ///////////////////////
t或使用已注册的设备 //应用程序已知。 ////////////////////////////////////////////////////////// 布尔useKnownDevices=false; ///////////////////////////////////////////////////////// //处理音频或视频的命令行选项, //持续时间和要保存到的文件。 ///////////////////////////////////////////////////////// 对于(int i=0;i 0&!useKnownDevices){ audioDevice=(CaptureDeviceInfo)设备。elementAt(0); }否则 audioDevice=CaptureDeviceManager.getDevice(音频设备名称); 如果(音频设备==null){ System.out.println(“找不到合适的音频设备。正在退出”); 系统出口(1); } captureLocation=audioDevice.getLocator(); 格式[0]=音频_格式; if(destinationName==null) destinationName=默认音频名称; 容器=音频容器; } ///////////////////////////////////////////////////////////////// //执行视频捕获的设置。包括找到合适的 //设备,查看其MediaLocator并设置内容 //类型。 //////////////////////////////////////////////////////////////// else if(选中。等于(视频)){ 设备=CaptureDeviceManager.getDeviceList(视频格式); if(devices.size()>0&!useKnownDevices) videoDevice=(CaptureDeviceInfo)设备。elementAt(0); 其他的 videoDevice=CaptureDeviceManager.getDevice(视频设备名称); 如果(视频设备==null){ System.out.println(“找不到合适的视频设备。正在退出”); 系统出口(1); } captureLocation=videoDevice.getLocator(); 格式[0]=视频_格式; if(destinationName==null) destinationName=默认的\u视频\u名称; 容器=视频容器; }else if(选中。等于(两者)){ captureLocation=null; 格式=新格式[2]; 格式[0]=音频_格式; 格式[1]=视频格式; 容器=视频容器; if(destinationName==null) destinationName=默认的\u视频\u名称; } //////////////////////////////////////////////////////////////////// //通过执行所有必要的处理器和数据链路准备 //Location2Location类。 //////////////////////////////////////////////////////////////////// destinationLocation=新的MediaLocator(destinationName); System.out.println(“正在配置捕获,请稍候”); capture=新位置2位置(captureLocation、destinationLocation、, 格式,容器,1.0); ///////////////////////////////////////////////////////////////////////////// //开始录制并告诉用户。指定文件的长度 //录音。然后等待最多4倍的持续时间 //录音 //(可能需要更长的时间来接收/写入数据,因此应等待一点以防万一)。 ///////////////////////////////////////////////////////////////////////////// System.out.println(“开始录制”+持续时间+“秒数” +选定+“…”; capture.setStopTime(新时间(持续时间)); if(waitFor==0) 等待时间=(整数)(4000*持续时间); 其他的 等待*=1000; int waited=capture.transfer(waitFor); ///////////////////////////////////////////////////////// //报告录制成功(或不成功)。 ///////////////////////////////////////////////////////// int state=capture.getState(); if(state==Location2Location.FINISHED) System.out.println(已选择 +“捕获成功时间约为” +((整数)((整数+500)/1000)) +“秒。数据写入”+目的地名称); else if(state==Location2Location.FAILED) System.out.println(已选择 +“大约在之后捕获失败” +((int)((等待+500)/1000))+“秒”); 否则{ System.out.println(已选择 +“大约之后仍在进行捕获” +((int)((等待+500)/1000))+“秒”); System.out.println(“可能失败的进程”); } 系统出口(0); } }Java t或使用已注册的设备 //应用程序已知。 ////////////////////////////////////////////////////////// 布尔useKnownDevices=false; ///////////////////////,java,audio,javasound,mixer,Java,Audio,Javasound,Mixer,t或使用已注册的设备 //应用程序已知。 ////////////////////////////////////////////////////////// 布尔useKnownDevices=false; ///////////////////////////////////////////////////////// //处理音频或视频的命令行选项, //持续时间和要保存到的文件。 //////////////////////////////////////////////////////
“Java的声音教程,但不知怎么的,对初学者来说太复杂了。”哪种语言听起来“简单”?+1这些教程针对的是有一定经验的人。教程应该是这样,如果这是复杂的,我怀疑任何其他教程将更好。我建议你试着去看看你能得到什么。您要检测的步骤1已明确说明。只需运行代码。如果代码失败,请返回并询问更具体的问题。也许这更符合您的要求:
import javax.sound.sampled.*;
public class SoundTrial {
public static void main(String[] args) {
Mixer.Info[] info = AudioSystem.getMixerInfo();
int i =0;
for(Mixer.Info print : info){
System.out.println("Name: "+ i + " " + print.getName());
i++;
}
}
}
Name: 0 Primary Sound Driver
Name: 1 Speakers and Headphones (IDT High Definition Audio CODEC)
Name: 2 Independent Headphones (IDT High Definition Audio CODEC
Name: 3 SPDIF (Digital Out via HP Dock) (IDT High Definition Audio CODEC)
Name: 4 Primary Sound Capture Driver
Name: 5 Integrated Microphone Array (ID
Name: 6 Microphone (IDT High Definition
Name: 7 Stereo Mix (IDT High Definition
Name: 8 Port Speakers and Headphones (IDT Hi
Name: 9 Port SPDIF (Digital Out via HP Dock)
Name: 10 Port Integrated Microphone Array (ID
Name: 11 Port Microphone (IDT High Definition
Name: 12 Port Stereo Mix (IDT High Definition
Name: 13 Port Independent Headphones (IDT Hi
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import java.util.*;
/*******************************************************************************
* A simple application to allow users to capture audio or video through devices
* connected to the PC. Via command-line arguments the user specifies whether
* audio (-a) or video (-v) capture, the duration of the capture (-d) in
* seconds, and the file to write the media to (-f).
*
* The application would be far more useful and versatile if it provided control
* over the formats of the audio and video captured as well as the content type
* of the output.
*
* The class searches for capture devices that support the particular default
* track formats: linear for audio and Cinepak for video. As a fall-back two
* device names are hard-coded into the application as an example of how to
* obtain DeviceInfo when a device's name is known. The user may force the
* application to use these names by using the -k (known devices) flag.
*
* The class is static but employs the earlier Location2Location example to
* perform all the Processor and DataSink related work. Thus the application
* chiefly involves CaptureDevice related operations.
*
* @author Michael (Spike) Barlow
******************************************************************************/
public class SimpleRecorder {
/////////////////////////////////////////////////////////////
// Names for the audio and video capture devices on the
// author's system. These will vary system to system but are
// only used as a fallback.
/////////////////////////////////////////////////////////////
private static final String AUDIO_DEVICE_NAME = "DirectSoundCapture";
private static final String VIDEO_DEVICE_NAME = "vfw:Microsoft WDM Image Capture:0";
///////////////////////////////////////////////////////////
// Default names for the files to write the output to for
// the case where they are not supplie by the user.
//////////////////////////////////////////////////////////
private static final String DEFAULT_AUDIO_NAME = "file://./captured.wav";
private static final String DEFAULT_VIDEO_NAME = "file://./captured.avi";
///////////////////////////////////////////
// Type of capture requested by the user.
//////////////////////////////////////////
private static final String AUDIO = "audio";
private static final String VIDEO = "video";
private static final String BOTH = "audio and video";
////////////////////////////////////////////////////////////////////
// The only audio and video formats that the particular application
// supports. A better program would allow user selection of formats
// but would grow past the small example size.
////////////////////////////////////////////////////////////////////
private static final Format AUDIO_FORMAT = new AudioFormat(
AudioFormat.LINEAR);
private static final Format VIDEO_FORMAT = new VideoFormat(
VideoFormat.CINEPAK);
public static void main(String[] args) {
//////////////////////////////////////////////////////
// Object to handle the processing and sinking of the
// data captured from the device.
//////////////////////////////////////////////////////
Location2Location capture;
/////////////////////////////////////
// Audio and video capture devices.
////////////////////////////////////
CaptureDeviceInfo audioDevice = null;
CaptureDeviceInfo videoDevice = null;
/////////////////////////////////////////////////////////////
// Capture device's "location" plus the name and location of
// the destination.
/////////////////////////////////////////////////////////////
MediaLocator captureLocation = null;
MediaLocator destinationLocation;
String destinationName = null;
////////////////////////////////////////////////////////////
// Formats the Processor (in Location2Location) must match.
////////////////////////////////////////////////////////////
Format[] formats = new Format[1];
///////////////////////////////////////////////
// Content type for an audio or video capture.
//////////////////////////////////////////////
ContentDescriptor audioContainer = new ContentDescriptor(
FileTypeDescriptor.WAVE);
ContentDescriptor videoContainer = new ContentDescriptor(
FileTypeDescriptor.MSVIDEO);
ContentDescriptor container = null;
////////////////////////////////////////////////////////////////////
// Duration of recording (in seconds) and period to wait afterwards
///////////////////////////////////////////////////////////////////
double duration = 10;
int waitFor = 0;
//////////////////////////
// Audio or video capture?
//////////////////////////
String selected = AUDIO;
////////////////////////////////////////////////////////
// All devices that support the format in question.
// A means of "ensuring" the program works on different
// machines with different capture devices.
////////////////////////////////////////////////////////
Vector devices;
//////////////////////////////////////////////////////////
// Whether to search for capture devices that support the
// format or use the devices whos names are already
// known to the application.
//////////////////////////////////////////////////////////
boolean useKnownDevices = false;
/////////////////////////////////////////////////////////
// Process the command-line options as to audio or video,
// duration, and file to save to.
/////////////////////////////////////////////////////////
for (int i = 0; i 0 && !useKnownDevices) {
audioDevice = (CaptureDeviceInfo) devices.elementAt(0);
} else
audioDevice = CaptureDeviceManager.getDevice(AUDIO_DEVICE_NAME);
if (audioDevice == null) {
System.out.println("Can't find suitable audio device. Exiting");
System.exit(1);
}
captureLocation = audioDevice.getLocator();
formats[0] = AUDIO_FORMAT;
if (destinationName == null)
destinationName = DEFAULT_AUDIO_NAME;
container = audioContainer;
}
/////////////////////////////////////////////////////////////////
// Perform setup for video capture. Includes finding a suitable
// device, obatining its MediaLocator and setting the content
// type.
////////////////////////////////////////////////////////////////
else if (selected.equals(VIDEO)) {
devices = CaptureDeviceManager.getDeviceList(VIDEO_FORMAT);
if (devices.size() > 0 && !useKnownDevices)
videoDevice = (CaptureDeviceInfo) devices.elementAt(0);
else
videoDevice = CaptureDeviceManager.getDevice(VIDEO_DEVICE_NAME);
if (videoDevice == null) {
System.out.println("Can't find suitable video device. Exiting");
System.exit(1);
}
captureLocation = videoDevice.getLocator();
formats[0] = VIDEO_FORMAT;
if (destinationName == null)
destinationName = DEFAULT_VIDEO_NAME;
container = videoContainer;
} else if (selected.equals(BOTH)) {
captureLocation = null;
formats = new Format[2];
formats[0] = AUDIO_FORMAT;
formats[1] = VIDEO_FORMAT;
container = videoContainer;
if (destinationName == null)
destinationName = DEFAULT_VIDEO_NAME;
}
////////////////////////////////////////////////////////////////////
// Perform all the necessary Processor and DataSink preparation via
// the Location2Location class.
////////////////////////////////////////////////////////////////////
destinationLocation = new MediaLocator(destinationName);
System.out.println("Configuring for capture. Please wait.");
capture = new Location2Location(captureLocation, destinationLocation,
formats, container, 1.0);
/////////////////////////////////////////////////////////////////////////////
// Start the recording and tell the user. Specify the length of the
// recording. Then wait around for up to 4-times the duration of
// recording
// (can take longer to sink/write the data so should wait a bit incase).
/////////////////////////////////////////////////////////////////////////////
System.out.println("Started recording " + duration + " seconds of "
+ selected + " ...");
capture.setStopTime(new Time(duration));
if (waitFor == 0)
waitFor = (int) (4000 * duration);
else
waitFor *= 1000;
int waited = capture.transfer(waitFor);
/////////////////////////////////////////////////////////
// Report on the success (or otherwise) of the recording.
/////////////////////////////////////////////////////////
int state = capture.getState();
if (state == Location2Location.FINISHED)
System.out.println(selected
+ " capture successful in approximately "
+ ((int) ((waited + 500) / 1000))
+ " seconds. Data written to " + destinationName);
else if (state == Location2Location.FAILED)
System.out.println(selected
+ " capture failed after approximately "
+ ((int) ((waited + 500) / 1000)) + " seconds");
else {
System.out.println(selected
+ " capture still ongoing after approximately "
+ ((int) ((waited + 500) / 1000)) + " seconds");
System.out.println("Process likely to have failed");
}
System.exit(0);
}
}