欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java自身的库实现录制扬声器的音频

程序员文章站 2024-03-25 08:22:34
...

一般来说,录制应用程序或扬声器的音频可以通过以下几种方式
windows下的钩子函数 线程进行监听
使用自带的库文件 对max进行操作
写一个虚拟声卡 起一个类似代理的作用
这里实现的是用javax.sounds库实现的,不过在这之前需要设置一下监听设备:
java自身的库实现录制扬声器的音频
监听线路的问题:貌似只能麦克风监听或扬声器数据监听
主函数如下:

private static String OUTPUT_FILE ="C:\\2.wav";
private static String RECORD_PORT_SELECT = "MICROPHONE";
private static float RECORD_VOLUME_LEVEL = 0.8f;
//---------------------
//输出文件目录 默认只支持wav一类
File outputFile =new File(OUTPUT_FILE);
			    //初始化系统
AudioFormat recordingFormat =new AudioFormat(
			    	AudioFormat.Encoding.PCM_SIGNED,
			    	44100.0F, 16, 2, 4, 44100.0F,
			    	false);
			        //录制线
TargetDataLine recordLine =null;
			   recordLine = (TargetDataLine)
AudioSystem.getTargetDataLine(recordingFormat);
recordLine.open(recordingFormat);
			 adjustRecordingVolume();
AudioFileFormat.Type fileType =	AudioFileFormat.Type.WAVE;
//开启录制线程
KokRecorder recorder = new  KokRecorder(recordLine,fileType,outputFile);
recorder.start();
			    	//System.out.println("Playing"+
			    		//"Song and Recording...");
			    	//初始化视图
CanvasFrame canvas = new CanvasFrame("录制");	        	    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

KokRecorder 的类

class KokRecorder extends Thread 
{ 
	private TargetDataLine recordLine; 
	private AudioFileFormat.Type recFormat;
	private AudioInputStream myAIS; 
	private File myOutFile; 
	public KokRecorder(TargetDataLine inLine, AudioFileFormat.Type tgtFmt, File inFile) { 
		recordLine = inLine; 
		myAIS = new AudioInputStream(inLine); 
		recFormat = tgtFmt;
		myOutFile = inFile; 
		} 
	   public void run() 
		{ try { 
		   //将线上监听的值 转向输出文件
			AudioSystem.write( myAIS,recFormat, myOutFile); 
			} catch (IOException e) { 
				e.printStackTrace(); } 
		}
		public void start() {
			recordLine.start(); 
			super.start(); 
			} 
		public void stopRecording() { 
				recordLine.stop(); 
				recordLine.close(); } 
}

相关设置

 public static void adjustRecordingVolume()
    throws Exception {
    Port.Info recPortInfo =new Port.Info(Port.class,
    	RECORD_PORT_SELECT, true);
  Port recPort = (Port)AudioSystem.getLine(
    	recPortInfo);
    setRecControlValue(recPort);
    }
  private static void setRecControlValue(Port inPort)
    throws Exception {
    inPort.open();
    Control [] controls = inPort.getControls();
     for(int i=0; i<controls.length; i++) {
  if(controls[i] instanceof
    	CompoundControl) {
    	Control[] members =
    		((CompoundControl)
    		controls[i]).
    		getMemberControls();
    for(int j=0; j<members.length; j++) {
    		setCtrl(members[j]);
    		} 
    	} 
    else
    	setCtrl(controls[i]);
    } 
    inPort.close();
    }
    private static void setCtrl(Control ctl) {
    	  if(ctl.getType().toString(
    	    	).equals("Select")) {
    	    	((BooleanControl)
    	    		ctl).setValue(true);
    	    	}
    	    if(ctl.getType().toString(
    	    	).equals("Volume")) {
    	    	FloatControl vol =
    	    		(FloatControl) ctl;
    	    	float setVal = vol.getMinimum()
    	    		+ (vol.getMaximum()
    	    		- vol.getMinimum())
    	    		* RECORD_VOLUME_LEVEL;
    	  	vol.setValue(setVal);
    	    } 
    }
相关标签: javacv java