当前位置: 代码迷 >> java >> 你如何从Java中的声卡中捕获声音?
  详细解决方案

你如何从Java中的声卡中捕获声音?

热度:102   发布时间:2023-07-25 19:12:25.0

我正在制作一个程序,让您远程访问计算机并对其进行控制。 到目前为止,键击,鼠标点击,鼠标指针和远程显示的所有功能都非常完美。 我的下一步是将音频从远程计算机发送到控制计算机。 我搜索并搜索,但我只找到有关录制麦克风输入的信息。 我需要知道如何从计算机的声卡中捕获声音并通过Socket发送。 麦克风捕获代码的示例如下所示:

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Port;
import javax.sound.sampled.TargetDataLine;

    public class Start {

    public static TargetDataLine line;

        public static void main(String[] args){
        AudioFormat format = getAudioFormat();
        Line.Info info = Port.Info.SPEAKER;
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();

            Thread stop = new Thread(){
                @Override
                public void run(){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    e.printStackTrace();
                    }
                line.stop();
                line.close();
            }
        };
        stop.start();

        AudioInputStream ais = new AudioInputStream(line);

        File file = new File("C:/Users/Savannah/Desktop/sound.au");
        file.createNewFile();

        AudioSystem.write(ais, AudioFileFormat.Type.AU, file);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public static AudioFormat getAudioFormat(){
        float sampleRate = 16000;
        int sampleSizeInBits = 8;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                                             channels, signed, bigEndian);
        return format;
    }

}

要从计算机的声卡中捕获声音并通过Socket发送,您可以使用下面的代码。 首先运行服务器然后运行客户端类。

public class Server {
ServerSocket MyService;
Socket clientSocket = null;
InputStream input;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
static Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();

Server() throws LineUnavailableException {

    try {
        Mixer mixer_ = AudioSystem.getMixer(mixerInfo[0]);
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        MyService = new ServerSocket(500);
        clientSocket = MyService.accept();

        input = new BufferedInputStream(clientSocket.getInputStream());
        while (input.read(tempBuffer) != -1) {
            sourceDataLine.write(tempBuffer, 0, 10000);

        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    return new AudioFormat(
            sampleRate,
            sampleSizeInBits,
            channels,
            signed,
            bigEndian);
}

public static void main(String s[]) throws LineUnavailableException {
    Server s2 = new Server();
}}

和客户方:

public class Client {
boolean stopCapture = false;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket sock = null;
public static void main(String[] args) {
    Client tx = new Client();
    tx.captureAudio();
}
private void captureAudio() {
    try {
        sock = new Socket("192.168.1.5", 500);
        out = new BufferedOutputStream(sock.getOutputStream());
        in = new BufferedInputStream(sock.getInputStream());
        Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(
                TargetDataLine.class, audioFormat);
        Mixer mixer = AudioSystem.getMixer(mixerInfo[2]);

        targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
        targetDataLine.open(audioFormat);
        targetDataLine.start();

        Thread captureThread = new CaptureThread();
        captureThread.start();

    } catch (Exception e) {
        System.out.println(e);
        System.exit(0);
    }
}
class CaptureThread extends Thread {

    byte tempBuffer[] = new byte[10000];

    @Override
    public void run() {
        stopCapture = false;
        try {
            while (!stopCapture) {
                int cnt = targetDataLine.read(tempBuffer, 0,
                        tempBuffer.length);
                out.write(tempBuffer);
           }
           } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;

    int sampleSizeInBits = 8;

    int channels = 1;

    boolean signed = true;

    boolean bigEndian = false;

    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
            bigEndian);
}}
  相关解决方案