gpt4 book ai didi

java - 在 Android 上通过套接字接收实时音频数据

转载 作者:行者123 更新时间:2023-11-29 04:33:36 25 4
gpt4 key购买 nike

我编写了一个应用程序,通过 WiFi 连接到 Rpi 上的网络套接字,并成功地向客户端和服务器发送数据,例如字符串。下一步是让客户端(在本例中为我的手机)实时接收和播放从服务器发送的音频数据。

到目前为止,我已经声明了套接字和音频变量。一旦按下按钮,就会启动异步任务“Listen”。我知道它可以连接到 socket 。之后,我尝试打开一个输入流并将其送入缓冲区,然后实时播放该缓冲区。

我想我已经接近弄清楚了,但我认为我还没有完全弄清楚。我在我的代码中用 TODO 标记了我不确定的区域。

public class MainActivity extends AppCompatActivity {
//audio variables
static final int sampleFreq = 44100;
static final int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
static final int streamType = STREAM_MUSIC;
static final int audioMode = MODE_STREAM;
static final int bufferSize = getMinBufferSize(sampleFreq, channelConfig, audioMode);

//socket variables
public Socket mySocket;
private int SERVERPORT = 33333;
private static final String SERVER_IP = "172.24.1.1";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}

public void onClickListen(View view) {
//call async task
new Listen().execute();
}


private class Listen extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {

try {
//get IP and port number
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.d(debugStr, "In initial listen connect");
//create socket
mySocket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (mySocket == null) {
str1 = "Socket became null, listener";
return null;
}



//TODO here is all the new audio stuff
try {
//creates buffer to hold incoming audio data
byte [] audioBuffer = new byte[4096];

//creates input stream readers to read incoming data
BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
DataInputStream myDis = new DataInputStream(myBis);
Log.d(debugStr, "Input created, listener");

// Read the file into the music array.
int i = 0;

//TODO unsure of while loop condition
while (myDis.read() != -1) {
//since incoming audio is unknown size, use estimated buffer size
audioBuffer[bufferSize-1-i] = myDis.readByte();
i++;
}
//create audio track object
AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);

//TODO write audio data to somewhere?
//TODO should this be in a while loop for live stream?
//TODO will this actually play the audio?
myAudioTrack.write(audioBuffer, 0, bufferSize);

//close input streams
myDis.close();
myBis.close();


} catch (IOException e1) {
e1.printStackTrace();
}
try {
mySocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
}
}
}

我认为我的 while 循环条件不正确,或者这是否是从输入流读取数据的正确方法。我还认为 audioTrack.write 应该在 while 循环中,如果它确实播放音频的话。

非常感谢任何帮助或澄清!

最佳答案

我想通了。

byte [] audioBuffer = new byte[4096];

//creates input stream readers to read incoming data
BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
DataInputStream myDis = new DataInputStream(myBis);

Log.d(debugStr, "Input created, listener");
AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);
//Log.d(debugStr, String.valueOf(mySocket.getInputStream().read(audioBuffer)));

Log.d(debugStr, "track made");
// Read the file into the music array.
int i = 0;
//TODO unsure of while loop condition
while (mySocket.getInputStream().read(audioBuffer) != -1) {

audioBuffer[audioBuffer.length-1-i] = myDis.readByte();
myAudioTrack.play();
myAudioTrack.write(audioBuffer, 0, audioBuffer.length);
i++;
}



//close input streams
myDis.close();
myBis.close();

关于java - 在 Android 上通过套接字接收实时音频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42709499/

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