我目前正在玩一个小程序来学习更多关于udp数据报传输的知识。 我的脚本基于我在这里找到的一个例子,我正在做的只是使用带有两个线程的AudioTrack和AudioRecorder。一个接收来自远程设备的数据包并使用AudioTrack播放,另一个将AudioRecorder录制的数据包发送到远程设备。
经过一些测试后,整个hing似乎运行良好,声音从A到B以及从B到A都没有问题。请注意我使用的两个设备是两个三星Galaxy s4 mini(实际上它适用于任何三星装置)。然后我在华硕变压器平板电脑和Acer Iconia 500之间使用它,运行良好,但噪音很小。
现在我试着让它与索尼Xperia E和三星Galaxy(当然可以正常工作)一起使用,但是那里有一个很大的问题:当它从xperia传到银河系时,我听到了声音,但是没有任何东西在另一边,xperia的扬声器似乎无法播放这些数据包(我在日志中看到它们被转移)。我希望这可能是一个设置问题,但对此没什么把握,当我与华为设备(任何类型)通信时也会发生同样的行为。
如果您想看到我的配置和所有内容,以下是我的UDP流媒体脚本:
public void startStreaming() {
Thread streamThread = new Thread(new Runnable() {
int bytesRcv;
@Override
public void run() {
int minAudioBufSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
byte[] datagramBuffer = new byte[512];
DatagramPacket packet;
InetAddress destination;
try {
destination = InetAddress.getByName(REMOTE_ADDRESS);
} catch (UnknownHostException e1) {
Log.e("Streaming", "Unknown host: " + e1.getLocalizedMessage());
destination = null;
}
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
minAudioBufSize);
Log.d("Streaming", "Recorder initialized");
recorder.startRecording();
while (running == true) {
try{
// reading data from MIC into buffer
bytesRcv = recorder.read(datagramBuffer, 0,
datagramBuffer.length);
// putting buffer in the packet
packet = new DatagramPacket(datagramBuffer, bytesRcv,
destination, SERVER_PORT);
Log.d("Streaming", "Packet size: " + packet.getLength());
Log.d("Streaming", "Packet content: " + Arrays.toString(datagramBuffer));
socket.send(packet);
} catch (IOException e) {
Log.e("Streaming",
"Could not send package: "
+ e.getLocalizedMessage());
running = false;
}catch (IllegalArgumentException iae){
Log.e("Streaming", "Error in audio datagram" + iae.getMessage());
running = false;
}
}
}
});
streamThread.start();
这是我接收数据包的脚本:
public void startReceive() {
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
int audioBufSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
byte[] datagramBuffer = new byte[512];
speaker = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
audioBufSize, AudioTrack.MODE_STREAM);
speaker.play();
while (running == true) {
try {
DatagramPacket packet = new DatagramPacket(
datagramBuffer, datagramBuffer.length);
socket.receive(packet);
Log.d("Receiver", "Received " + packet.getLength() + " bytes");
// sending data to the Audiotrack obj i.e. speaker
speaker.write(datagramBuffer, 0, packet.getLength());
Log.d("Receiver", "Writing buffer content: " + Arrays.toString(datagramBuffer));
} catch (IOException e) {
Log.e("Receiver",
"Could not send package: "
+ e.getLocalizedMessage());
running = false;
}catch (IllegalArgumentException iae){
Log.e("Receiver", "Error in audio datagram" + iae.getMessage());
running = false;
}
}
}
});
receiveThread.start();
}
有人已经遇到过这种问题吗?任何建议都会很棒。
由于
答案 0 :(得分:0)
好的解决了,它是关于采样率和缓冲区大小的。