• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

通过UDP发送音频

audio 9次浏览

因此,我一直在寻找发送tut的麦克风数据,但没有找到任何麦克风。
因此,我阅读了有关开行的Oracle教程,并且可以将音频录制到ByteArrayOutputStream,但是现在有两个问题!

第一:
如何播放录制的音频。

第二:如果我将其记录到BAOS中,我将如何动态发送它。
我想我会发送数据数组,但是每次接收到它都太浪费处理器以至于无法将其写入BAOS还是我可以做不同的事情?

当前代码:

import java.io.ByteArrayOutputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class MicrophoneRecorder {
    static boolean stopped = false;

    public static void main(String[] args) {
        AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
        TargetDataLine line = null;
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Not supported!");
        }
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int numBytesRead = 0;
        byte[] data = new byte[line.getBufferSize() / 5];
        line.start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                stopped = true;
            }
        }).start();
        while (!stopped) {
            numBytesRead = line.read(data, 0, data.length);
            out.write(data, 0, numBytesRead);
        }
    }

}

感谢您提供的任何帮助。

真诚的,罗伯托·阿尼奇·巴尼奇

附言

看到这个,不起作用
http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html

P.P.S.

UDP是一种很好的解决方案还是应该使用RTSP

解决方案如下:

这是一个示例代码,可以帮助我通过UDP流化并使用音频。您可以更改无限循环以限制音频流的持续时间。下面是客户端和服务器代码。音频输入来自麦克风。

服务器:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class Sender {

public static void main(String[] args) throws IOException {

    AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, true);
    TargetDataLine microphone;
    SourceDataLine speakers;
    try {
        microphone = AudioSystem.getTargetDataLine(format);

        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        microphone = (TargetDataLine) AudioSystem.getLine(info);
        microphone.open(format);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int numBytesRead;
        int CHUNK_SIZE = 1024;
        byte[] data = new byte[microphone.getBufferSize() / 5];
        microphone.start();


        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
        speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        speakers.open(format);
        speakers.start();


        // Configure the ip and port
        String hostname = "localhost";
        int port = 5555;

        InetAddress address = InetAddress.getByName(hostname);
        DatagramSocket socket = new DatagramSocket();
        byte[] buffer = new byte[1024];
        for(;;) {                
            numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
          //  bytesRead += numBytesRead;
            // write the mic data to a stream for use later
            out.write(data, 0, numBytesRead); 
            // write mic data to stream for immediate playback
            speakers.write(data, 0, numBytesRead);            
            DatagramPacket request = new DatagramPacket(data,numBytesRead, address, port);
            socket.send(request);

        }

    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } 
}}

客户:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;   
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class UdpClient {

public static void main(String[] args) throws LineUnavailableException {
     AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, true);
    TargetDataLine microphone;
    SourceDataLine speakers;
    microphone = AudioSystem.getTargetDataLine(format);

    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    microphone = (TargetDataLine) AudioSystem.getLine(info);
    microphone.open(format);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int numBytesRead;
    int CHUNK_SIZE = 1024;
    byte[] data = new byte[microphone.getBufferSize() / 5];
    microphone.start();

    int bytesRead = 0;
    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
    speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    speakers.open(format);
    speakers.start();

    String hostname = "localhost";
    int port = 5555;

    try {
        InetAddress address = InetAddress.getByName(hostname);
        DatagramSocket socket = new DatagramSocket();

        DatagramSocket serverSocket = new DatagramSocket(5555);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];

        while (true) {

            byte[] buffer = new byte[1024];
            DatagramPacket response = new DatagramPacket(buffer, buffer.length);
            serverSocket.receive(response);

            out.write(response.getData(), 0, response.getData().length);
            speakers.write(response.getData(), 0, response.getData().length);
            String quote = new String(buffer, 0, response.getLength());

            System.out.println(quote);
            System.out.println();

            //Thread.sleep(10000);
        }

    } catch (SocketTimeoutException ex) {
        System.out.println("Timeout error: " + ex.getMessage());
        ex.printStackTrace();
    } catch (IOException ex) {
        System.out.println("Client error: " + ex.getMessage());
        ex.printStackTrace();
    }/* catch (InterruptedException ex) {
        ex.printStackTrace();
    }*/
}}


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)