我正在尝试通过TCP将实时音频从一个应用程序发送到另一应用程序。
我可以连接男性插座,但是没有任何声音。
请帮我。
这是发件人代码。
public void startStreaming() {
Thread streamThread = new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new ServerSocket(port);
s = socket.accept();
Log.d("VS", "Socket Created");
byte[] buffer = new byte[1024];
BufferedOutputStream out = new BufferedOutputStream(new ByteArrayOutputStream(1024));
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
Log.d("VS","Buffer created of size " + minBufSize);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS", "Recorder initialized");
recorder.startRecording();
while(status == true) {
recorder.read(buffer, 0, buffer.length);
Log.d("VS", "Packet created");
out.write(buffer);
out.flush();
Log.d("VS", "Packet Sent");
}
} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
}
}
});
streamThread.start();
}
}
这是接收者代码。
public void receiveButton(View view){
status = true;
Log.d("VR","start receiving called");
startReceiving();
}
public void startReceiving() {
Thread receiveThread = new Thread (new Runnable() {
@Override
public void run() {
try {
socket = new Socket("192.168.0.3",50005);
Log.d("VR", "Socket Created1");
byte[] buffer = new byte[1024];
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(buffer));
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);
speaker.play();
while(status == true) {
Log.d("VR", "Packet Received");
in.read(buffer);
Log.d("VR", "Packet data read into buffer");
speaker.write(buffer, 0, buffer.length);
Log.d("VR", "Writing buffer content to speaker");
}
} catch (SocketException e) {
Log.e("VR", "SocketException");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
receiveThread.start();
}
}
请帮我。
在查看日志时,感觉代码运行正常,但是没有声音发出。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
这些权限也添加在manifest.xml中
最佳答案
您没有使用套接字。您必须获取接收器代码,然后才能使用socket.getIntpuStreamer()并使用它来读取数据并将其保存在缓冲区中。