title: Android Media Player Fails With Local Socket, and what is the workaround to play streaming data for VoiceChat?
body: <p>I made a simple test to make MediaPlayer play some live streaming data via localSocket.</p>
  
  <pre><code>    class IOLoop extends Thread
      {
          @Override
          public void run()
          {
              try
              {
                  MediaPlayer mPlayer = new MediaPlayer();
                  mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  System.out.println("----======MediaPlayer()============-- ");
                  LocalSocket receiver = new LocalSocket();
                  System.out.println("----======new LocalSocket()============-- ");
                  FileDescriptor fd = receiver.getFileDescriptor();
                  System.out.println("----fd============-- ");
  
                      mPlayer.setDataSource(fd);  //&lt;-- error
                      mPlayer.prepare();
  
                  System.out.println("----=========mPlayer set===============-- ");
  
              }
              catch (IOException e)
              {//
              }
          }
      }
  
      IOLoop io00 = new IOLoop();
      io00.start();
  </code></pre>
  
  <p>This code fails, with <code>IllegalArgumentException</code></p>
  
  <pre><code>02-14 05:16:46.418  20424-20436/com.example.app I/System.out﹕ ----fd============--
  02-14 05:16:46.426  20424-20436/com.example.app W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0xa61ea908)
  02-14 05:16:46.426  20424-20436/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-197
      java.lang.IllegalArgumentException
              at android.media.MediaPlayer.setDataSource(Native Method)
              at android.media.MediaPlayer.setDataSource(MediaPlayer.java:976)
              at com.example.app.MainActivity$1IOLoop.run(MainActivity.java:51)
  </code></pre>
  
  <p>so <a href="https://www.google.com/search?q=getFileDescriptor#newwindow=1&amp;q=media%20player%20getfiledescriptor%20LocalSocket" rel="nofollow noreferrer">googled</a>.</p>
  
  <p>Basically, they say LocalSocket FileDescriptor is not seekable so not adequate for the data source.</p>
  
  <p>However, according to AndroidDeveloper-Media Playback</p>
  
  <p><a href="http://developer.android.com/guide/topics/media/mediaplayer.html" rel="nofollow noreferrer">http://developer.android.com/guide/topics/media/mediaplayer.html</a></p>
  
  <p>It clearly stated:</p>
  
  <blockquote>
    <p>The Android multimedia framework includes support for playing variety
    of common media types, so that you can easily integrate audio, video
    and images into your applications. You can play audio or video from
    media files stored in your application's resources (raw resources),
    from standalone files in the filesystem, or <strong>from a data stream
    arriving over a network connection, all using MediaPlayer APIs</strong>.</p>
  </blockquote>
  
  <p>So, it's a strange situation.</p>
  
  <p>Also, there is voice chat apps like <code>LINE</code> etc. What is the workaround?</p>
  
  <p>Any thought? Thank you.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>found a similar topic:</p>
  
  <p><a href="https://stackoverflow.com/questions/6678353/can-i-use-mediaplayer-play-video-from-stream-line">Can I use MediaPlayer play video from stream line</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5747060/how-do-you-play-android-inputstream-on-mediaplayer">How do you play Android InputStream on MediaPlayer?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20109443/play-aac-buffer-on-android">Play AAC buffer on Android</a></p>
  
  <p><a href="https://code.google.com/p/aacdecoder-android/" rel="nofollow noreferrer">https://code.google.com/p/aacdecoder-android/</a></p>
  
  <p>What a mess..</p>
  
  <hr>
  
  <p>EDIT2</p>
  
  <p>This is a real good project to illustrate these area.</p>
  
  <p><a href="https://github.com/fyhertz/libstreaming" rel="nofollow noreferrer">https://github.com/fyhertz/libstreaming</a></p>
  
  <p>I think <code>MediaCodec</code> is the way to go instead of MediaRecorder etc.</p>