[英]Read / Write streamed audio via TCP socket Java
我是Java和套接字编程的新手,我正在寻找更多的起点/正确方向/思路的验证。
总体而言,我要实现的想法是:
老实说,我遇到的麻烦是服务器端,这是因为我不完全了解套接字编程。 我想将音频作为原始音频(通过字节/二进制数组作为参数)接收到我的服务器。 对于大多数文档,我发现它们基本上是说打开套接字,打开输入/输出流,读/写,关闭流,关闭套接字。 这适用于说普通文本,但不适用于音频。 我相信这可以通过音频来完成。 对于音频.wav,这样做的总体思路是什么? 有我不知道的API可以解决这个问题吗?
更新:8/30/2015
这是我到目前为止的TCP客户端/服务器代码。 现在,我正在使用的“捕获的音频”只是我读到客户端输出流中的现有.wav文件。 我现在面临的问题是创建的.wav听起来不像原始的。 这听起来像噪音,并且时间要短得多。
TCP客户端代码:
Socket serverSocket = null;
DataOutputStream out = null;
BufferedReader in = null;
try {
serverSocket = new Socket(serverHostname, port);
out = new DataOutputStream(serverSocket.getOutputStream());
in = new BufferedReader(
new InputStreamReader(serverSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + serverHostname);
System.exit(1);
}
DataInputStream stdIn = new DataInputStream(
new FileInputStream(".wav location"));
int readBytes;
byte[] temp = new byte[1024];
while( (readBytes = stdIn.read(temp, 0, temp.length)) != -1){
out.write(temp, 0, readBytes);
}
out.flush();
out.close();
in.close();
stdIn.close();
serverSocket.close();
}
到目前为止,这是我的服务器代码:
public void clientConnect(int socketPort) {
ServerSocket s_Socket = null;
try {
s_Socket = new ServerSocket(socketPort);
System.out.println ("SERVER Connection Socket Created");
try {
System.out.println ("Waiting for CLIENT Connection");
while (true){
new TcpAudioStreaming (c_Socket = s_Socket.accept());
System.out.println("CLIENT: " + c_Socket.getInetAddress());
System.out.println("PORT: " + c_Socket.getPort());
System.out.println("LOCAL PORT: " + c_Socket.getLocalPort());
}
}
catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
}
catch (IOException e){
System.err.println("Could not listen on port: " + socketPort);
System.exit(1);
}
finally {
try {
s_Socket.close();
}
catch (IOException e){
System.err.println("Could not close port: " + socketPort);
System.exit(1);
}
}
}
public void run() {
boolean end = false;
System.out.println ("New Communication Thread Started");
try {
//DataOutputStream outStream = new DataOutputStream(c_Socket.getOutputStream());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DataInputStream inStream = new DataInputStream(c_Socket.getInputStream());
int read;
byte[] temp = new byte[1024];
while( (read = inStream.read(temp, 0, temp.length)) != -1){
outStream.write(temp, 0, read);
}
outStream.flush();
byte[] audioBytes = outStream.toByteArray();
writeWave(audioBytes);
outStream.close();
inStream.close();
c_Socket.close();
}
catch (IOException e) {
System.err.println("Problem with Communication Server");
System.exit(1);
}
}
public void writeWave(byte[] audioArry) {
String filePath = "new .wav path";
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, false);
try {
ByteArrayInputStream inStream = new ByteArrayInputStream(audioArry);
long length = (long)(audioArry.length / audioFormat.getFrameSize());
AudioInputStream audioInputStreamTemp = new AudioInputStream(inStream, audioFormat, length);
File fileOut = new File(filePath);
if (AudioSystem.isFileTypeSupported(AudioFileFormat.Type.WAVE, audioInputStreamTemp)) {
System.out.println("Trying to write");
AudioSystem.write(audioInputStreamTemp, AudioFileFormat.Type.WAVE, fileOut);
System.out.println("Written successfully");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
Java套接字或TCP不能区分ASCII和二进制数据。 进行解释的是应用程序。
从一个简单的客户端/服务器应用程序开始,然后从那里继续前进。 有关介绍,请参见https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.