public static ByteArrayOutputStream out = new ByteArrayOutputStream();//写出流 public void record() { try { AudioFormat format = AudioUtils.getAudioFormat();//获取需要采样声音的参数信息 DataLine.Info info = new DataLine.Info(TargetDataLine.class,format); // format is an AudioFormat object TargetDataLine line = (TargetDataLine)(AudioSystem.getLine(info));//获取与指定的Line.Info对象中的描述匹配的行。 //通俗点就是获取一下话筒Line line.open(format);//以指定的格式打开行,使该行获取任何所需的系统资源,并可以运行。 /* *****这里的DataLine等操作,可以直接CV拷贝,这些都是固定格式。***** */ // Assume that the TargetDataLine, line, has already // been obtained and opened. int numBytesRead; byte[] data = new byte[line.getBufferSize() / 5];//设置缓冲区大小 // Begin audio capture. line.start();//开始记录声音 //允许线路从事数据I / O。 如果在已经运行的行上调用该方法,该方法什么也不做。 除非缓冲区中的数据已被刷新, //否则线路将从线路停止时未处理的第一帧开始恢复I / O。 当音频捕获或播放开始时,会生成一个START事件。 // Here, stopped is a global boolean set by another thread. for(;;) {//这里设置成为死循环,方便一直记录,不停歇。 // Read the next chunk of data from the TargetDataLine. numBytesRead = line.read(data, 0, data.length);//读取字节到字节数组 // Save this chunk of data. out.write(data, 0, numBytesRead);//写出流 } } catch (LineUnavailableException e) { e.printStackTrace(); } }
什么是DataLine.Info ?DataLine.Info (Java Platform SE 8 )