gpt4 book ai didi

java - Android 中通过 TCP 进行实时音频流传输

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:08 24 4
gpt4 key购买 nike

我正在尝试通过 TCP 将实时音频从一个应用程序发送到另一应用程序。我可以连接公 socket 并连接它们,但我没有听到任何声音。请帮我。这是发件人代码。

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() 并使用它来读取数据并将它们保存在缓冲区中。

关于java - Android 中通过 TCP 进行实时音频流传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com