我正在制作简单的wifi聊天应用程序。该应用程序有两种方法可以从麦克风输入并播放到扬声器。该应用程序的日志说我正在接收语音,但我无法听到发言者的任何声音。请帮帮我。
private void makeCall(final String IP_Address) {
// New thread to make calls to the other phone
Thread makeCallThread = new Thread() {
@Override
public void run() {
super.run();
// start sending calls to the other phone
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
Log.d("VS", "Socket Created");
byte[] buffer = new byte[minBufSize];
Log.d("VS", "Buffer created of size " + minBufSize);
DatagramPacket packet;
InetAddress destination = null;
try {
destination = InetAddress.getByName(IP_Address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
Log.d("VS", "Address retrieved");
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, minBufSize);
Log.d("VS", "Recorder initialized");
recorder.startRecording();
while (status) {
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
Log.d("VS", "I have read from the mic.");
//putting buffer in the packet
packet = new DatagramPacket(buffer, buffer.length, destination, port);
try {
socket.send(packet);
Log.d("VS", "packet has been send.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
makeCallThread.start();
}
此方法接收呼叫:
public void startReceiving(View view) {
Thread receiveThread = new Thread (new Runnable() {
@Override
public void run() {
try {
DatagramSocket socket = new DatagramSocket(50005);
Log.d("VR", "Socket Created");
byte[] buffer = new byte[256];
//minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);
speaker.play();
while( status ) {
try {
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
socket.receive(packet);
Log.d("VR", "Packet Received");
//reading content from packet
buffer=packet.getData();
Log.d("VR", "Packet data read into buffer");
//sending data to the Audiotrack obj i.e. speaker
speaker.write(buffer, 0, minBufSize);
Log.d("VR", "Writing buffer content to speaker");
} catch(IOException e) {
Log.e("VR","IOException");
}
}
} catch (SocketException e) {
Log.e("VR", "SocketException");
}
}
});
receiveThread.start();
}
日志说明如下:
08-24 23:21:52.639 20103-20450/com.example.shahjahan.udp D/VR﹕ Packet data read into buffer
08-24 23:21:52.639 20103-20450/com.example.shahjahan.udp D/VR﹕ Writing buffer content to speaker
08-24 23:21:52.689 20103-20450/com.example.shahjahan.udp D/VR﹕ Packet Received
08-24 23:21:52.689 20103-20450/com.example.shahjahan.udp D/VR﹕ Packet data read into buffer
08-24 23:21:52.689 20103-20450/com.example.shahjahan.udp D/VR﹕ Writing buffer content to speaker
08-24 23:21:52.739 20103-20450/com.example.shahjahan.udp D/VR﹕ Packet Received
08-24 23:21:52.739 20103-20450/com.example.shahjahan.udp D/VR﹕ Packet data read into buffer
08-24 23:21:52.739 20103-20450/com.example.shahjahan.udp D/VR﹕ Writing buffer content to speaker
但是,说话者不玩任何东西。请帮我解决这个问题。