【问题标题】:How to record audio with the specified audio device in Java?如何在 Java 中使用指定的音频设备录制音频?
【发布时间】:2015-08-07 02:19:48
【问题描述】:

我有两个连接到计算机的麦克风(一个内置麦克风,一个基于 USB)。我想用他们每个人录制相应的音频。

主要记录代码如下:

class Record implements Runnable{
    byte bts[] = new byte[10000];
    public void run() { 
        baos = new ByteArrayOutputStream();     
        try {
            stopflag = false;
            while(stopflag != true)
            {                   
                int cnt = td.read(bts, 0, bts.length);
                if(cnt > 0)
                {
                    baos.write(bts, 0, cnt);
                }

                byte copyBts[] = bts;
                bais = new ByteArrayInputStream(copyBts);
                ais = new AudioInputStream(bais, af, copyBts.length/af.getFrameSize());
                try{
                    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, af);
                    sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
                    sd.open(af);
                    sd.start();             

                    //read from audio stream
                    int Buffer_Size = 10000;
                    audioDataBuffer = new byte[Buffer_Size];                   
                    intBytes = ais.read(audioDataBuffer, 0,audioDataBuffer.length);                     
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(baos != null){
                    baos.close();
                }   
            } catch (Exception e) {
                e.printStackTrace();
            }finally{                   
                td.close();                 
            }
        }
    }       
}

我可以使用this code 列出音频设备。结果是这样的:

OS: Windows 8.1 6.3/x86
Java: 1.8.0_45 (Oracle Corporation)

Mixer: Direct Audio Device: DirectSound Playback [Primary Sound Driver]
Mixer: Direct Audio Device: DirectSound Playback [Speakers / HP (IDT High Definition Audio CODEC)]
Mixer: Direct Audio Device: DirectSound Capture [Primary Sound Capture Driver]
Mixer: Direct Audio Device: DirectSound Capture [Internal Microphone Array (IDT ]**//integrated device**
Mixer: Direct Audio Device: DirectSound Capture [Microphone (2- USB PnP Sound De]**//usb device**
...

现在我如何要求程序一次只从 USB 设备(或集成设备)录制音频?

【问题讨论】:

    标签: java audio javasound


    【解决方案1】:

    找到您要使用的Mixer 并替换您的

    sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    

    符合

    sd = (SourceDataLine) mixer.getLine(dataLineInfo);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2017-12-19
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多