title: Stream Audio From One Android Phone to Another
body: <p>I'm making two apps - receiver and sender, each of which goes on an Android device. The sender should be able to stream local audio files to the receiver, over Wifi, preferably using UDP or RTP.</p>
  
  <p>I have made the receiver app, it runs fine on Internet radio stations and all, but the trouble now is in making the sender app. I haven't been able to find any reliable resources online. 
  Some of the resources I referred to and why they haven't helped:</p>
  
  <p><a href="http://rxwen.blogspot.in/2010/04/streaming-audio-on-android.html" rel="nofollow noreferrer">#1</a>
  Audiotrack doesn't support mp3, which would be a major disadvantage.</p>
  
  <p><a href="https://stackoverflow.com/questions/9770565/streaming-real-time-audio">#2</a>
  This uses something called a ParcelFileDescriptor. Whether this is the result of reading too many unfamiliar API's or not, I just cannot understand what that line <code>ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(socket)</code> is doing. It seems to be creating a new <code>parcelfiledescriptor</code> from <code>socket</code>, but I thought the code was supposed to be sending it to <code>socket</code>.</p>
  
  <p>So can anyone suggest either an alternative, or a modification of the above types of code that would work for my application? For your reference, I'm attaching the source code of the receiver side app.</p>
  
  <pre><code>package com.example.audioclient;
  
  import java.io.IOException;
  
  import android.media.*;
  import android.media.MediaPlayer.*;
  import android.media.MediaPlayer.OnPreparedListener;
  import android.net.Uri;
  import android.os.Bundle;
  import android.app.Activity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class RTPClient extends Activity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener{
  
  String URL_OF_FILE = "http://fr3.ah.fm:9000";
  private String TAG = getClass().getSimpleName();
  private MediaPlayer mp = null;
  
  private Button play;
  private Button pause;
  private Button stop;
  
  private EditText tv; 
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      play = (Button) findViewById(R.id.play);
      //pause = (Button) findViewById(R.id.pause);
      stop = (Button) findViewById(R.id.stop);
  
      tv = (EditText) findViewById(R.id.editText1);
  
      //URL_OF_FILE = tv.getText().toString();
  
      play.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
              play();
             }
      });
  
      /*pause.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
              pause();
             }
      });*/
  
      stop.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View view) {
                  stop();
                 }
      });
  }
  
  private void play() {
        //Uri myUri = Uri.parse("http://currentstream1.publicradio.org:80/");
          URL_OF_FILE = tv.getText().toString();
          Uri myUri = Uri.parse(URL_OF_FILE);
        try {
            if (mp == null) {
                this.mp = new MediaPlayer();
            } else {
                mp.stop();
                mp.reset();
            }
            mp.setDataSource(this, myUri); // Go to Initialized state
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnBufferingUpdateListener(this);
  
            mp.setOnErrorListener(this);
            mp.prepareAsync();
  
            Log.d(TAG, "LoadClip Done");
              } catch (Throwable t) {
                  Log.d(TAG, t.toString());
              }
          }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  
      @Override
  public boolean onError(MediaPlayer mp, int what, int extra) {
          StringBuilder sb = new StringBuilder();
          sb.append("Media Player Error: ");
          switch (what) {
          case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
              sb.append("Not Valid for Progressive Playback");
              break;
          case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
              sb.append("Server Died");
              break;
          case MediaPlayer.MEDIA_ERROR_UNKNOWN:
              sb.append("Unknown");
              break;
          default:
              sb.append(" Non standard (");
              sb.append(what);
              sb.append(")");
          }
          sb.append(" (" + what + ") ");
          sb.append(extra);
          Log.e(TAG, sb.toString());
          return true;
          }
  
  
  @Override
  public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "Stream is prepared");
        mp.start();
  }
  
  private void pause() {
        mp.pause();
  }
  
  private void stop() {
      mp.stop();
  
  }
  
   @Override
   public void onDestroy() {
       super.onDestroy();
       stop();
  
   }
  
   public void onCompletion(MediaPlayer mp) {
        stop();
   }
  
   public void onBufferingUpdate(MediaPlayer mp, int percent) {
       Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
   }
  
  }
  </code></pre>