[英]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.