Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Playing mp3 directly from the input stream without writing into temp files

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

like image 809
Karthic Rao Avatar asked Feb 19 '15 10:02

Karthic Rao


2 Answers

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.

like image 71
Joao Sousa Avatar answered Oct 13 '22 10:10

Joao Sousa


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

like image 23
Karthic Rao Avatar answered Oct 13 '22 12:10

Karthic Rao