Record voice with Java

后端 未结 1 908
余生分开走
余生分开走 2020-12-03 08:30

I want to record voice using a Java application; I guess this will be basically an applet that will run on client side. But I don\'t have any idea of how to do it... any ide

相关标签:
1条回答
  • 2020-12-03 08:37

    I am late to the party, but here are the official docs on capturing audio: http://docs.oracle.com/javase/tutorial/sound/capturing.html

    (And copied directly from the link above here is some sample code to do it:)

    TargetDataLine line;
    DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                    format); // format is an AudioFormat object
    if (!AudioSystem.isLineSupported(info)) {
        // Handle the error ...
    
    }
    // Obtain and open the line.
    try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
    } catch (LineUnavailableException ex) {
        // Handle the error ...
    }
    
    // Assume that the TargetDataLine, line, has already
    // been obtained and opened.
    ByteArrayOutputStream out  = new ByteArrayOutputStream();
    int numBytesRead;
    byte[] data = new byte[line.getBufferSize() / 5];
    
    // Begin audio capture.
    line.start();
    
    // Here, stopped is a global boolean set by another thread.
    while (!stopped) {
        // 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);
    }
    
    0 讨论(0)
提交回复
热议问题