title: How to implement Audio calling on sockets in Android
body: <p>I have searched and found a code of voice streaming or audio calling between two android phones on simple sockets. I have implemented this , but this code is not working. I am unable to hear any voice.</p>
  
  <p>Receiver Code :</p>
  
  <pre><code>    private int sampleRate = 44100;
      private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
      private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
      int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
  
  
  public void startReceiving() {
  
          Thread receiveThread = new Thread (new Runnable() {
  
              @Override
              public void run() {
  
                  try {
  
                      DatagramSocket socket = new DatagramSocket(50005);
                      Log.d("VR", "Socket Created");
  
  
                      byte[] buffer = new byte[256];
                      speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);
  
  
  
                      while(status == true) {
                          try {
  
  
                              DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                              socket.receive(packet);
                              Log.d("VR", "Packet Received");
  
                              //reading content from packet
                              buffer=packet.getData();
                              Log.d("VR", "Packet data read into buffer");
  
                              //sending data to the Audiotrack obj i.e. speaker
                              speaker.write(buffer, 0, minBufSize);
  
                              Log.d("VR", String.valueOf(buffer));
  
                              speaker.play();
  
                          } catch(IOException e) {
                              Log.e("VR","IOException");
                          }
                      }
  
  
                  } catch (SocketException e) {
                      Log.e("VR", "SocketException");
                  }
  
  
              }
  
          });
          receiveThread.start();
      }
  </code></pre>
  
  <p>Sender Code:</p>
  
  <pre><code>public void startStreaming() {
          Thread streamThread = new Thread(new Runnable() {
  
              @Override
              public void run() {
                  try {
  
                      DatagramSocket socket = new DatagramSocket();
                      Log.d("VS", "Socket Created");
  
                      byte[] buffer = new byte[minBufSize];
  
                      Log.d("VS","Buffer created of size " + minBufSize);
                      DatagramPacket packet;
  
                      final InetAddress destination = InetAddress.getByName("192.168.0.216");
                      Log.d("VS", "Address retrieved");
  
  
                      recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
                      Log.d("VS", "Recorder initialized");
  
                      recorder.startRecording();
  
  
                      while(status == true) {
  
  
                          //reading data from MIC into buffer
                          minBufSize = recorder.read(buffer, 0, buffer.length);
  
                          //putting buffer in the packet
                          packet = new DatagramPacket (buffer,buffer.length,destination,port);
  
                          socket.send(packet);
                      }
  
                  } catch(UnknownHostException e) {
                      Log.e("VS", "UnknownHostException");
                  } catch (IOException e) {
                      e.printStackTrace();
                      Log.e("VS", "IOException");
                  } 
              }
  
          });
          streamThread.start();
       }
  </code></pre>
  
  <p>I debugged the code , and packets are successfully transmitting and <code>speaker.play()</code> is also called. But there is no <code>Voice</code></p>
  
  <p>I have implemented this code in a single application and activity. With two Buttons <code>Start Listening</code> and <code>Start Streaming</code> </p>