title: Receive Live Audio Data Via Socket on Android
body: <p>I have coded an app to connect to a network socket on an Rpi over WiFi and have successfully sent data, such as a string, to and from the client and server. The next step is to have the client, my phone in this case, receive and play audio data in real time that is being sent from the server. </p>
  
  <p>So far I have socket and audio variables declared. Once a button is hit, an async task "Listen" is started. I know it can connect to the socket. After that, I'm trying to open an input stream and feed it into a buffer, and then have that buffer played in real time.</p>
  
  <p>I think I'm close to figuring it out, but I don't think I have it quite right yet. I have marked areas I am unsure of with TODO in my code.</p>
  
  <pre><code>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&lt;Void, Void, Void&gt; {
      @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;
          }
      }
  }
  }
  </code></pre>
  
  <p>I don't think my while loop condition is correct, or if that is even the correct way to read data from an input stream. I also think that the audioTrack.write should be in the while loop, if that does actually make audio play.</p>
  
  <p>Any help or clarification is greatly appreciated!</p>