我需要有关构建连接的帮助,该连接可以将Audio和String一起发送并在客户端进行拆分。现在我可以发送音频但是当我添加其他数据类型时,接收到的数据在另一侧有噪声:
这里我删除了发送字符串代码:
请帮帮我。
这是服务器代码:
package tw.rascov.MediaStreamer;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import java.net.ServerSocket;
import java.net.Socket;
public class MediaStreamServer {
static final int frequency = 44100;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
boolean isRecording;
int recBufSize;
ServerSocket serverSocket;
Socket socket;
AudioRecord audioRecord;
public MediaStreamServer(final Context ctx, final int port) {
recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, frequency, channelConfiguration, audioEncoding, recBufSize);
try {
serverSocket = new ServerSocket(port);
}
catch (Exception e) {
e.printStackTrace();
Intent intent = new Intent()
.setAction("tw.rascov.MediaStreamer.ERROR")
.putExtra("msg", e.toString());
ctx.sendBroadcast(intent);
return;
}
new Thread() {
byte[] buffer = new byte[recBufSize];
public void run() {
try
{
socket = serverSocket.accept();
}
catch (Exception e) {
e.printStackTrace();
Intent intent = new Intent()
.setAction("tw.rascov.MediaStreamer.ERROR")
.putExtra("msg", e.toString());
ctx.sendBroadcast(intent);
return;
}
audioRecord.startRecording();
isRecording = true;
while (isRecording) {
int readSize = audioRecord.read(buffer, 0, recBufSize);
try {
socket.getOutputStream().write(buffer, 0, readSize);
}
catch (Exception e) {
e.printStackTrace();
Intent intent = new Intent()
.setAction("tw.rascov.MediaStreamer.ERROR")
.putExtra("msg", e.toString());
ctx.sendBroadcast(intent);
break;
}
}
audioRecord.stop();
try { socket.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}.start();
}
public void stop() {
isRecording = false;
try { serverSocket.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
和客户代码:
public class MediaStreamClient {
static final int frequency = 44100;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
boolean isPlaying;
int playBufSize;
Socket socket;
AudioTrack audioTrack;
public MediaStreamClient(final Context ctx, final String ip, final int port) {
playBufSize=AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
//audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, playBufSize, AudioTrack.MODE_STREAM);
audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
frequency,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
AudioTrack.getMinBufferSize (
frequency,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT
),
AudioTrack.MODE_STREAM
);
audioTrack.setStereoVolume(1f, 1f);
new Thread() {
byte[] buffer = new byte[playBufSize];
public void run() {
try
{
socket = new Socket(ip, port);
}
catch (Exception e)
{
e.printStackTrace();
Intent intent = new Intent()
.setAction("tw.rascov.MediaStreamer.ERROR")
.putExtra("msg", e.toString());
ctx.sendBroadcast(intent);
return;
}
audioTrack.play();
isPlaying = true;
while (isPlaying)
{
int readSize = 0;
try
{
DataInputStream dIn = new DataInputStream(socket.getInputStream());
readSize = dIn.read(buffer);
}
catch (Exception e)
{
e.printStackTrace();
Intent intent = new Intent()
.setAction("tw.rascov.MediaStreamer.ERROR")
.putExtra("msg", e.toString());
ctx.sendBroadcast(intent);
break;
}
audioTrack.write(buffer, 0, readSize);
}
audioTrack.stop();
try { socket.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}.start();
}
public void stop() {
isPlaying = false;
}
public void setVolume(float lvol, float rvol) {
audioTrack.setStereoVolume(lvol, rvol);
}
}