Android read file from url
-
I created android application to stream online radio stations but it doesn't work.I want to read ip address from url and assign it to string and when user selects radio to listen I should add port to that string!!!
public class BackgroundService extends Service implements OnCompletionListener
{
MediaPlayer mediaPlayer;
private String STREAM_URL;@Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { final StringBuilder text = new StringBuilder(); new Thread(new Runnable() { public void run() { try { URL url = new URL("http://audiophileradio.stream/Ip.txt"); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; while ((str = in.readLine()) != null) { text.append(str); } in.close(); text.insert(text.length(), ":8000"); } catch (Exception e) { Log.d("MyTag",e.toString()); } } }).start();
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
String radio = sharedPreferences.getString("station", "8000");if (radio != null && radio.equals("8000")) { text.append(":8000"); STREAM\_URL = text.toString(); Toast.makeText(this,STREAM\_URL,Toast.LENGTH\_SHORT).show(); } if (radio != null && radio.equals("8010")) { text.append(":8010"); STREAM\_URL = text.toString(); } if (radio != null && radio.equals("8020")) { text.append(":8020"); STREAM\_URL = text.toString(); } if (radio != null && radio.equals("8030")) { text.append(":8030"); STREAM\_URL = text.toString(); } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(STREAM\_URL); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.setOnCo
-
I created android application to stream online radio stations but it doesn't work.I want to read ip address from url and assign it to string and when user selects radio to listen I should add port to that string!!!
public class BackgroundService extends Service implements OnCompletionListener
{
MediaPlayer mediaPlayer;
private String STREAM_URL;@Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { final StringBuilder text = new StringBuilder(); new Thread(new Runnable() { public void run() { try { URL url = new URL("http://audiophileradio.stream/Ip.txt"); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; while ((str = in.readLine()) != null) { text.append(str); } in.close(); text.insert(text.length(), ":8000"); } catch (Exception e) { Log.d("MyTag",e.toString()); } } }).start();
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
String radio = sharedPreferences.getString("station", "8000");if (radio != null && radio.equals("8000")) { text.append(":8000"); STREAM\_URL = text.toString(); Toast.makeText(this,STREAM\_URL,Toast.LENGTH\_SHORT).show(); } if (radio != null && radio.equals("8010")) { text.append(":8010"); STREAM\_URL = text.toString(); } if (radio != null && radio.equals("8020")) { text.append(":8020"); STREAM\_URL = text.toString(); } if (radio != null && radio.equals("8030")) { text.append(":8030"); STREAM\_URL = text.toString(); } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(STREAM\_URL); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.setOnCo
Depending on how long it takes your thread code to finish parsing http://audiophileradio.stream/Ip.txt, your preferences/append code is also accessing the
text
variable. What's the possibility of that happening out of order?Pavlex4 wrote:
mediaPlayer.setDataSource(STREAM_URL);
Have you tried hard-coding the value passed to
setDataSource()
to see if the reset of your player-related code is correct? This is unrelated to the problem you are having, but the fourif()
tests are not as efficient as they could be. You are checkingradio
's value four more times than is necessary. Consider:if (radio != null)
{
text.append(':').append(radio);STREAM\_URL = text.toString(); Toast.makeText(this, STREAM\_URL, Toast.LENGTH\_SHORT).show();
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles