8

I'm creating an input stream to buffer and stream a mp3 from cloud .

URL url = new URL("http://xxxx.yyy.com/Demo.mp3");

InputStream inputStream = url.openStream();

Now how do i playback the mp3 from media player without using a temporary file to store it and read back from the same ? I'm developing for Android Lollipop

Star OS
  • 119
  • 1
  • 1
  • 15
Karthic Rao
  • 3,624
  • 8
  • 30
  • 44
  • [Here is an answer for Android 6.0 onwards](https://stackoverflow.com/questions/12920429/anyone-have-mediaplayer-working-with-parcelfiledescriptor-and-createpipe/52720388#52720388) – mahadevan gss Oct 09 '18 at 11:52

3 Answers3

1

I'm pretty sure the MediaPlayer can handle remote URLs. Take a look at this example. Check the setDataSource method from the MediaPlayer class as well.

EDIT: Since you really really want to use an inputstream, I think you'll need to go low-level. Check the AudioTrack class. This SO answer might help. There are also a couple of issues here and here that might be relevant.

Community
  • 1
  • 1
Joao Sousa
  • 4,055
  • 1
  • 26
  • 29
  • 1
    I dont want to do that . MediaPlayer doesnt allow me to manage or get access to buffered content . I have a need to manipulate the buffered content , So i thought of doing the other way around , Stream the mp3 myself,buffer it and then feed it to MediaPlayer . But i couldnt find a direct way to do it without writing this buffer into temporary files and reading back from them to play it using MediaPlayer ! This is what i want to achieve .. – Karthic Rao Feb 19 '15 at 11:44
1

This problem persists even today !!! Check these link out https://code.google.com/p/android/issues/detail?id=29870 and

http://www.piterwilson.com/blog/2014/03/11/android-mediaplayer-not-quite-there-yet/ . There is absolutely no way either to get access and control over the MediaPlayer buffer , neither to feed the buffered mp3 content stored in an byte array into MediaPplayer as an argument to play it . So People either convert the mp3 buffer to PCM and use AudioTrack to play it or write the byte array of the input stream into a local socket and make Mediaplayer read back using the socket file descriptor like mentioned this following link Audio stream buffering

Community
  • 1
  • 1
Karthic Rao
  • 3,624
  • 8
  • 30
  • 44
  • I haven't seen anyone get success with the 'inputstream -> socket file descriptor' method, the Android docs for MediaPlayer's setDataSource(fd) says: "The FileDescriptor must be seekable (N.B. a LocalSocket is not seekable). " Please correct me if wrong as I'm using the local http server solution which is nasty. – NeilS Nov 26 '15 at 15:22
0

The solution I'm using to feed binary data directly to MediaPlayer is to use ParcelFileDescriptor#createPipe() (API level 9) and MediaPlayer#setDataSource(java.io.FileDescriptor).

Here's sample code (untested):

ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
FileDescriptor fd = pipe[0].getFileDescriptor();
mediaPlayer.setDataSource(fd);
OutputStream out = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);

From this point on, whatever you write in the output stream will be received by the MediaPlayer. This is pretty fast since it uses a kernel FIFO to transfer data (no sockets, no TCP) and as far as I understand is fully in RAM (no actual files are used).

Martin Marinov
  • 1,167
  • 3
  • 15
  • 25