I came across something very similar. I use Galaxy S2 for debugging and trying to play a direct radio stream, I got all kinds of errors and problems. The first thing you need to know is that since some devices support wma files (Windows Media Audio format), but Galaxy doesnโt, unless you find a way to convert this stream to a playable stream, you have a problem.
The solution (view) I found for the problem is the following: since I read for a long time what you can do, instead of using MediaPlayer and try to use InputStream to in a straightforward way, read with audio (right now I only worked with Mp3, because, as I said , wma will not work), get a few seconds of audio in an mp3 file, and then use MediaPlayer to play this file locally, while re-buffering the next part of the sound in a second file, and then just play back and forth these files ( you can set the length of each file)
I leave you part of the code that I tried to implement and worked for buffering and playing mp3 on my phone, but I did not continue the project, but I have not yet found a resolution solution for wma.
public void onClick(View v) { try { URL url = new URL("http://........./myStation.mp3"); InputStream inputStream = url.openStream(); long bytesRead = 0; File root = android.os.Environment.getExternalStorageDirectory(); File dir = new File (root.getAbsolutePath() + "/usbStorage"); if(dir.exists()== false) { dir.mkdirs(); } File outputSource = new File(dir, "test.mp3"); FileOutputStream fileOutputStream = new FileOutputStream(outputSource); //Log.d(LOG_TAG, "FileOutputStream: " + outputSource); int c; int howManyBytes = 12000; //You decide the amount of bytes to read because the live stream while keep on going forver while ((c = inputStream.read()) != 10000) { fileOutputStream.write(c); bytesRead++; if (bytesRead > howManyBytes) break; } fileOutputStream.close(); MediaPlayer mp = MediaPlayer.create(this, Uri.fromFile(outputSource)); mp.start(); // no need to call prepare(); create() does that for you } catch(Exception e) { Log.e("radio", e.getMessage()); } }
source share