Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Socket在java中基于客户端服务器的音频直播_Java_Sockets_Audio - Fatal编程技术网

使用Socket在java中基于客户端服务器的音频直播

使用Socket在java中基于客户端服务器的音频直播,java,sockets,audio,Java,Sockets,Audio,给定的代码旨在使用JavaSocket在客户端和服务器之间进行实时音频流,但问题是,当我运行此项目时,客户端开始录制声音并将其发送到接收器(服务器)端。然后服务器缓冲接收到的声音,但不同时播放。但是当客户端关闭时,服务器开始播放声音。请帮助我。我需要服务器必须同时播放接收到的声音 \客户 import java.io.*; import java.net.*; import java.sound.sampled.*; import org.apache.commons.io.IOUtils;

给定的代码旨在使用JavaSocket在客户端和服务器之间进行实时音频流,但问题是,当我运行此项目时,客户端开始录制声音并将其发送到接收器(服务器)端。然后服务器缓冲接收到的声音,但不同时播放。但是当客户端关闭时,服务器开始播放声音。请帮助我。我需要服务器必须同时播放接收到的声音

\客户

import java.io.*;
import java.net.*;
import java.sound.sampled.*;
import org.apache.commons.io.IOUtils; 
public class ClientStream{ 
public ClientStream() throws IOException{
    isl.runListener();
}

private IncomingSoundListener isl = new IncomingSoundListener();    
AudioFormat format = getAudioFormat();
InputStream is;
Socket client;
String serverName = "192.168.2.8";
int port=3000;
boolean inVoice = true;


private AudioFormat getAudioFormat(){
    float sampleRate = 16000.0F;
    int sampleSizeBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;

    return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
}
class IncomingSoundListener {
    public void runListener(){
        try{
            System.out.println("Connecting to server:"+serverName+" Port:"+port);
            client = new Socket(serverName,port); 
            System.out.println("Connected to: "+client.getRemoteSocketAddress());
            System.out.println("Listening for incoming audio.");
            DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class,format);
            SourceDataLine speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
            speaker.open(format);
            speaker.start();
            while(inVoice){
                is = client.getInputStream();
                byte[] data = IOUtils.toByteArray(is);  
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                AudioInputStream ais = new AudioInputStream(bais,format,data.length);
                int bytesRead = 0;
                if((bytesRead = ais.read(data)) != -1){
                    System.out.println("Writing to audio output.");
                    speaker.write(data,0,bytesRead);

   //                 bais.reset();
                }
                ais.close();
                bais.close();

            }
           speaker.drain();
           speaker.close();
           System.out.println("Stopped listening to incoming audio.");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
public static void main(String [] args) throws IOException{
        new ClientStream();
    }
}

\服务器

import java.io.*;
import java.net.*;
import java.sound.sampled.*;
public class ServerStream {
private OutgoingSoudnListener osl = new OutgoingSoudnListener();
boolean outVoice = true;
AudioFormat format = getAudioFormat();

private ServerSocket serverSocket;
Socket server;
private AudioFormat getAudioFormat() {
    float sampleRate = 16000.0F;
    int sampleSizeBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;

   return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
}
public ServerStream() throws IOException{
    try{
        System.out.println("Creating Socket...");
        serverSocket = new ServerSocket(3000);
        osl.runSender();
    }catch(Exception e){
        e.printStackTrace();
    }
}
class OutgoingSoudnListener{
    public void runSender(){
        try{
            server = serverSocket.accept();
            System.out.println("Listening from mic.");
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class,format);
            TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(micInfo);
            mic.open(format);
            System.out.println("Mic open.");
            byte tmpBuff[] = new byte[mic.getBufferSize()/5];
            mic.start();
            while(outVoice) {
                System.out.println("Reading from mic.");
                int count = mic.read(tmpBuff,0,tmpBuff.length);
                if (count > 0){
                    System.out.println("Writing buffer to server.");
                    out.write(tmpBuff, 0, count);
                }               
                }
            mic.drain();
            mic.close();
            System.out.println("Stopped listening from mic.");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
public static void main (String args[]) throws IOException{
    new ServerStream();

}

}

客户端-服务器连接,随后基于TCP协议模型。 你可以在他们的文件中确认

您所寻求的是基于UDP的,您可能会损失包,但这就是工作方式。这就是流媒体视频的工作原理,你得到一些,你失去一些


现在,您要问的是,使用TCP协议实现时遇到的一个问题是,TCP基于确认以保持通信同步,如果您的服务器或客户端未能确认,那么您可能会遇到流被卡住,因此。

我没有得到,它如何帮助我使我的应用程序工作。你是在建议我使用UDP吗?是的。您应该使用UDP建立流媒体通道。我还建议TCP中的ack可能会导致实时通信的一些延迟。请记住,TCP中不会丢失任何数据包。