【问题标题】:How do you capture sound from the sound card in Java?你如何在 Java 中从声卡中捕获声音?
【发布时间】:2015-10-22 15:42:55
【问题描述】:

我正在制作一个程序,让您可以远程访问计算机并对其进行控制。到目前为止,从击键、鼠标点击、鼠标指针和远程显示等一切都可以完美运行。我的下一步是将音频从远程计算机发送到控制计算机。我搜索了再搜索,但我只找到了有关录制麦克风输入的信息。我需要知道如何从计算机的声卡中捕获声音并通过 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;
    }

}

【问题讨论】:

    标签: java audio capture


    【解决方案1】:

    要从计算机的声卡捕获声音并通过 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);
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 2012-07-18
      • 2011-09-01
      • 2010-11-20
      相关资源
      最近更新 更多