title: Stream Live Android Audio to Server
body: <p>I'm currently trying to stream live microphone audio from an Android device to a Java program. I started off with sending the live audio between two android devices to confirm my method was correct. The audio could be heard perfectly with barely any delay on the receiving device. Next I send the same audio stream to a small Java program and I verified that the data was being sent here correctly too. Now what I want to do is encode this data and somehow play it back on the server running the Java program. I would rather play it in a web browser using HTML5 or JavaScript but I am open to alternative methods such as VLC.</p>
  
  <p>Here is the code for the Android app which sends the live microphone audio</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  
  private Button startButton,stopButton;
  
  public byte[] buffer;
  public static DatagramSocket socket;
      AudioRecord recorder;
  
  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);
      private boolean status = true;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
       startButton = (Button) findViewById (R.id.start_button);
       stopButton = (Button) findViewById (R.id.stop_button);
  
       startButton.setOnClickListener(startListener);
       stopButton.setOnClickListener(stopListener);
  
       minBufSize += 2048;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  private final OnClickListener stopListener = new OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
                  status = false;
                  recorder.release();
                  Log.d("VS","Recorder released");
      }
  };
  
  private final OnClickListener startListener = new OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
                  status = true;
                  startStreaming();           
      }
  };
  
  
  
  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);
  
  
                  Log.d("VS", "Address retrieved");
                  recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                  Log.d("VS", "Recorder initialized");
  
  
                  recorder.startRecording();
  
  
                  InetAddress IPAddress = InetAddress.getByName("192.168.1.5");
                  byte[] sendData = new byte[1024];
                  byte[] receiveData = new byte[1024];
  
  
                  while (status == true)
                  {
                      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 50005);
                      socket.send(sendPacket);
                  }
  
              } catch(UnknownHostException e) {
                  Log.e("VS", "UnknownHostException");
              } catch (IOException e) {
                  Log.e("VS", "IOException");
                  e.printStackTrace();
              } 
  
  
          }
  
      });
      streamThread.start();
  }
  }
  </code></pre>
  
  <p>And here is the code for the Java program reading in the data..</p>
  
  <pre><code>class Server
  {
     public static void main(String args[]) throws Exception
        {
           DatagramSocket serverSocket = new DatagramSocket(50005);
              byte[] receiveData = new byte[1024];
              byte[] sendData = new byte[1024];
              while(true)
                 {
                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  
  
  
                serverSocket.receive(receivePacket);
                String sentence = new String( receivePacket.getData().toString());
  
                System.out.println("RECEIVED: " + sentence);
             }
    }
  }
  </code></pre>
  
  <p>I know that I should encode the audio on the app side before sending this to the Java program but I'm not to sure how to go about encoding while using AudioRecorder. I would prefer not to use NDK as I have no experience with it and do not really have time to learn how to use it....yet :)</p>