如何使用Java捕获PC声音并通过套接字发送它

时间:2019-05-08 09:50:33

标签: java audio audio-capture

我正在尝试从pc(从扬声器/耳机)捕获音频,并通过套接字(如果可能的话,通过UDP)将其发送到另一台必须播放的计算机。我发现一些代码可以做到这一点:

服务器:

    Calendar calendar1 = Calendar.getInstance();
    calendar1.add(Calendar.DAY_OF_MONTH, -1);

    new DatePickerDialog(this,listener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
    DatePickerDialog.getDatePicker().setMinDate(calendar1.getTimeInMillis());
    DatePickerDialog.show();

客户:

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.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();
    }}

但是客户抛出

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.Socket;

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.38", 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) {
            e.printStackTrace();
            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) {
                e.printStackTrace();
                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);
    }}

,我不知道该怎么做(我知道这不是UDP套接字,但是我首先想拥有一些可以工作的代码)。预先感谢。

0 个答案:

没有答案