我编写了一个应用程序,通过WiFi连接到Rpi上的网络套接字,并成功地向客户端和服务器发送数据,例如字符串。下一步是让客户端(在这种情况下我的手机)实时接收和播放从服务器发送的音频数据。
到目前为止,我已声明了套接字和音频变量。一旦按下按钮,就会出现异步任务" Listen"开始了。我知道它可以连接到套接字。之后,我尝试打开输入流并将其输入缓冲区,然后实时播放该缓冲区。
我想我已经接近搞清楚了,但我认为我还没有完全正确。我在代码中标记了我不确定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循环中,如果它确实让音频播放。
非常感谢任何帮助或澄清!
答案 0 :(得分:2)
我明白了。
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();