Java – call peer to peer audio on Android: Voice Interruption and lag (delay in receiving data packets) increase
I'm trying to build a peer to peer audio call on Android I use my Android phone to communicate with my tablet, but after receiving about 40 packets, the phone almost stops receiving packets, and then suddenly receives some packets and plays them, etc., but the waiting time increases Similarly, tablets initially receive packets and play them, but the delay increases, and voice begins to crash after a period of time, as if some packets were lost What's the idea that led to this problem
This is the application code... I just give the IP addresses of the sender and receiver in the recordaudio class when I run it on two devices
public class AudioRPActivity extends Activity implements OnClickListener { DatagramSocket socketS,socketR; DatagramPacket recvP,sendP; RecordAudio rt; PlayAudio pt; Button sr,stop,sp; TextView tv,tv1; File rf; boolean isRecording = false; boolean isPlaying = false; int frequency = 44100; int channelConfiguration = AudioFormat.CHANNEL_CONfigURATION_MONO; int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.text1); tv1 = (TextView)findViewById(R.id.text2); sr = (Button)findViewById(R.id.sr); sp = (Button)findViewById(R.id.sp); stop = (Button)findViewById(R.id.stop); sr.setOnClickListener(this); sp.setOnClickListener(this); stop.setOnClickListener(this); stop.setEnabled(false); try { socketS=new DatagramSocket(); socketR=new DatagramSocket(6000); } catch(SocketException se) { tv.setText(se.toString()); finish(); } } public void onClick(View v) { if(v == sr) record(); else if(v == sp) play(); else if(v == stop) stopPlaying(); } public void play() { stop.setEnabled(true); sp.setEnabled(false); pt = new PlayAudio(); pt.execute(); } public void stopPlaying() { isRecording=false; isPlaying = false; stop.setEnabled(false); } public void record() { stop.setEnabled(true); sr.setEnabled(false); rt = new RecordAudio(); rt.execute(); } private class PlayAudio extends AsyncTask<Void,String,Void> { @Override protected Void doInBackground(Void... arg0) { isPlaying = true; int bufferSize = AudioTrack.getMinBufferSize(frequency,channelConfiguration,audioEncoding); byte[] audiodata = new byte[bufferSize]; try { AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,frequency,audioEncoding,4*bufferSize,AudioTrack.MODE_STREAM); audioTrack.setPlaybackRate(frequency); audioTrack.play(); while(isPlaying) { recvP=new DatagramPacket(audiodata,audiodata.length); socketR.receive(recvP); audioTrack.write(recvP.getData(),recvP.getLength()); } audioTrack.stop(); audioTrack.release(); } catch(Throwable t) { Log.e("Audio Track","Playback Failed"); } return null; } protected void onProgressUpdate(String... progress) { tv1.setText(progress[0].toString()); } protected void onPostExecute(Void result) { sr.setEnabled(true); sp.setEnabled(true); } } private class RecordAudio extends AsyncTask<Void,Void> { @Override protected Void doInBackground(Void... arg0) { isRecording = true; try { int bufferSize = AudioTrack.getMinBufferSize(frequency,audioEncoding); AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,4*bufferSize); byte[] buffer = new byte[bufferSize]; audioRecord.startRecording(); int r=0; while(isRecording) { int brr = audioRecord.read(buffer,bufferSize); sendP=new DatagramPacket(buffer,brr,InetAddress.getByName("sender's/receiver's ip"),6000); socketS.send(sendP); publishProgress(String.valueOf(r)); r++; } audioRecord.stop(); audioRecord.release(); } catch(Throwable t) { Log.e("AudioRecord","Recording Failed...."); } return null; } protected void onProgressUpdate(String... progress) { tv.setText(progress[0].toString()); } protected void onPostExecute(Void result) { sr.setEnabled(true); sp.setEnabled(true); } } }
Solution
When sending voice over the network, if the frequency is more than 8000, I will have trouble 44100 sounds terrible That may be my case
Another difficulty is that it is difficult to say the order in which packets arrive using UDP I've seen an implementation recombine them in the right order, but I can't find it now