我正在尝试通过连接服务器(Java)-客户端(Android)实时制作音频循环。服务器(PC/Java)客户端向客户端(手机/Android)发送数据并返回。
我放了一段代码来澄清: -服务器
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* This class implements a TCP Server.<p>
*/
public class GTCPServer {
public static final int SERVER_PORT = 4444; // Port number
private boolean running = true; // Boolean to control stream
private ServerSocket serverSocket;
// Input-Output streams:
private DataInputStream dis;
private DataOutputStream dos;
// Buffer:
private byte[] bytes;
private static final int BUFFER_SIZE = 512 * 4; // Size
/**
* Constructor. Starts Server.
*/
public GTCPServer() {
bytes = new byte[BUFFER_SIZE]; // Create buffer
// Put values to test:
bytes[0] = 3;
bytes[BUFFER_SIZE - 1] = 7;
try {
// Create Server Socket and wait connections.
serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Conecting...");
// Within accept methode we are waiting connection.
// When it's ready, create Client Socket:
Socket client = serverSocket.accept();
System.out.println("Receiving...");
try {
// Streams:
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
} catch (Exception e) {
System.out.println("Error.");
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Error.");
e.printStackTrace();
}
} // GTCPServer()
/**
* Send (write) a byte[] buffer within TCP.
* @param buffer - Data.
* @param offset - Position.
* @param count - Number of bytes to write.
*/
public void write(byte[] buffer, int offset, int count) {
try {
dos.write(buffer, offset, count); // Write
} catch (IOException e) {
e.printStackTrace();
}
} // write()
} // GTCPServer
客户:
import android.util.Log; import java.io.*; import java.net.InetAddress; import java.net.Socket; import com.appacoustic.java.g.G; public class GTCPClient_A { public static final String SERVER_IP = "192.168.1.101"; // Server IP (PC) public static final int SERVER_PORT = 4444; // Port number private boolean running = true; // Boolean to control stream // Input-output streams: private DataInputStream dis; private DataOutputStream dos; // Buffer: private byte[] bytes; private static final int BUFFER_SIZE = 512 * 4; // Size /** * Constructor. */ public GTCPClient_A() { bytes = new byte[BUFFER_SIZE]; // Create buffer } // GTCPClient_A() /** * Execute Thread. It isn't override because we are using a subclass (connectTask) wich extends AsyncTask. */ public void run() { try { // Get InetAddress (IPv4 Server) InetAddress serverAddr = InetAddress.getByName(SERVER_IP); Log.e("GTCPClient_A", "Conecting..."); // Create a Socket ti connect with Server: Socket socket = new Socket(serverAddr, SERVER_PORT); try { dis = new DataInputStream(socket.getInputStream()); dos = new DataOutputStream(socket.getOutputStream()); int lap = 0; // Para llevar la cuenta de las vueltas while (running) { dis.read(bytes); // Leemos por TCP System.out.println("IN_ini_["+lap+"]: "+bytes[0]); System.out.println("IN_fin_["+lap+"]: "+bytes[BUFFER_SIZE - 1]); G.toc = System.currentTimeMillis(); G.ticToc(); G.tic = G.toc; dos.write(bytes); // Escribimos por TCP System.out.println("OUT_ini_["+lap+"]: "+bytes[0]); System.out.println("OUT_fin_["+lap+"]: "+bytes[BUFFER_SIZE - 1]); lap++; } } catch (Exception e) { Log.e("GTCP", "SERVIDOR: Error", e); } finally { socket.close(); } } catch (Exception e) { Log.e("GTCP", "CLIENTE: Error", e); } } // run() } // GTCPClient_A
我已经通过蓝牙和 TCP 对其进行了编程,但效果不佳。延迟太长(大约 5 秒)。令我惊讶的是,当尝试在 Java 而不是 Android 中部署客户端时,它确实有效(代码几乎相同)。事实上,我对着麦克风说话(我安排了 JASIOHost ASIO 驱动程序),从我拥有服务器的计算机,TCP 数据传输到同一 WiFi 内的另一台计算机,返回并听到服务器计算机中的实时完美扬声器。
因此,看来问题出在Android上。但我想要的是使用智能手机或平板电脑作为客户端。
我也在客户端用这条线测试过: android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); // 优先
查找Wear 数月尝试未成功的解决方案...