I made a simple test to make MediaPlayer play some live streaming data via localSocket.
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); //<-- error
mPlayer.prepare();
System.out.println("----=========mPlayer set===============-- ");
}
catch (IOException e)
{//
}
}
}
IOLoop io00 = new IOLoop();
io00.start();
This code fails, with IllegalArgumentException
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)
so googled.
Basically, they say LocalSocket FileDescriptor is not seekable so not adequate for the data source.
However, according to AndroidDeveloper-Media Playback
http://developer.android.com/guide/topics/media/mediaplayer.html
It clearly stated:
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 from a data stream arriving over a network connection, all using MediaPlayer APIs.
So, it's a strange situation.
Also, there is voice chat apps like LINE
etc. What is the workaround?
Any thought? Thank you.
EDIT:
found a similar topic:
Can I use MediaPlayer play video from stream line
How do you play Android InputStream on MediaPlayer?
https://code.google.com/p/aacdecoder-android/
What a mess..
EDIT2
This is a real good project to illustrate these area.
https://github.com/fyhertz/libstreaming
I think MediaCodec
is the way to go instead of MediaRecorder etc.