我正在开发一个应用程序,用于在Android上通过WiFi发送语音消息。我正在使用UDP数据包发送/接收语音消息。代码工作正常我可以看到所有日志,并可以验证数据包是否正在发送和接收,但我无法听到任何消息。我尝试将音量设置为最大值,并将两个设备的音轨类别的频率更改为48000。现在我需要的最后一件事是验证数据包是否为空并且可以使用某些MD5或crc有技术...我如何在我的代码中使用它来找出这些数据包包含什么? 这是我的发件人代码 package com.example.sendvoice;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText target;
private TextView streamingLabel;
private Button startButton, stopButton;
public byte[] buffer;
public static DatagramSocket socket;
private int port = 50005; // which port??
AudioRecord recorder;
// Audio Configuration.
private int sampleRate = 48000; // How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private boolean status = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("VS", "Inside Oncreate");
target = (EditText) findViewById(R.id.target_IP);
streamingLabel = (TextView) findViewById(R.id.streaming_label);
startButton = (Button) findViewById(R.id.start_button);
stopButton = (Button) findViewById(R.id.stop_button);
streamingLabel.setText("Press Start! to begin");
startButton.setOnClickListener(startListener);
stopButton.setOnClickListener(stopListener);
}
private final OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
status = false;
recorder.release();
Log.v("VS", "Recorder released");
}
};
private final OnClickListener startListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
status = true;
startStreaming();
}
};
public void startStreaming() {
Thread streamThread = new Thread(new Runnable() {
@Override
public void run() {
try {
int minBufSize = AudioRecord.getMinBufferSize(sampleRate,
channelConfig, audioFormat);
DatagramSocket socket = new DatagramSocket();
Log.v("VS", "Socket Created");
byte[] buffer = new byte[minBufSize];
Log.v("VS", "Buffer created of size " + minBufSize);
DatagramPacket packet;
final InetAddress destination = InetAddress
.getByName(target.getText().toString());
Log.v("VS", "Address retrieved:" + destination);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, channelConfig, audioFormat, minBufSize);
Log.v("VS", "Recorder initialized");
recorder.startRecording();
Log.v("VS", "Recording Started");
//Toast.makeText(MainActivity.this, "Speak Up",
// Toast.LENGTH_LONG).show();
while (status == true) {
// reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
// putting buffer in the packet
packet = new DatagramPacket(buffer, buffer.length,
destination, port);
Log.v("VS", "Packet contains"+packet.toString());
socket.send(packet);
}
} catch (UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
}
}
});
streamThread.start();
}
}
这是我的接收器代码
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button receiveButton, stopButton;
public static DatagramSocket socket;
private AudioTrack speaker;
// Audio Configuration.
private int sampleRate = 48000 ; // How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private boolean status = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("VR", "Inside Oncreate");
receiveButton = (Button) findViewById(R.id.receive_button);
stopButton = (Button) findViewById(R.id.stop_button);
findViewById(R.id.receive_label);
receiveButton.setOnClickListener(receiveListener);
stopButton.setOnClickListener(stopListener);
}
private final OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View v) {
status = false;
speaker.release();
Log.v("VR", "Speaker released");
}
};
private final OnClickListener receiveListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
status = true;
startReceiving();
}
};
public void startReceiving() {
Log.v("VR", "Inside Recieve button");
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Log.v("VR", "Inside Recieve run");
DatagramSocket socket = new DatagramSocket(50005);
Log.v("VR", "Socket Created");
byte[] buffer = new byte[4096];
// minimum buffer size. need to be careful. might cause
// problems. try setting manually if any problems faced
int minBufSize = AudioRecord.getMinBufferSize(sampleRate,
channelConfig, audioFormat);
Log.v("VR", "minBufSize:"+minBufSize);
speaker = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, channelConfig, audioFormat, minBufSize,
AudioTrack.MODE_STREAM);
speaker.play();
Log.v("VR", "speaker.play()");
while (status == true) {
try {
Log.v("VR", "Inside While");
DatagramPacket packet = new DatagramPacket(buffer,
buffer.length);
Log.v("VR", "packet created");
socket.receive(packet);
Log.v("VR", "Packet Received");
Log.v("VR", "Packet cont"+packet.getData());
// reading content from packet
buffer = packet.getData();
Log.v("VR", "Packet data read into buffer"+buffer.toString());
// sending data to the Audiotrack obj i.e. speaker
speaker.write(buffer, 0, minBufSize);
Log.v("VR", "Writing buffer content to speaker");
} catch (IOException e) {
Log.e("VR", "IOException");
}
}
} catch (SocketException e) {
Log.e("VR", "SocketException");
}
}
});
receiveThread.start();
}
}
我们非常感谢任何使用校验和方法检查数据包的建议。