Good evening everyone!
I am new to this Java thing, but I have learned so much over the past few weeks, and I feel like I'm really starting to get a hang of it. I am currently developing my first project, an android app for a small radio network which will allow you to stream the station live. To make this happen, I must query a .pls stream file, and extract the three possible URLs for the listen link. I cannot directly link to any of the URLs because they are dynamic and can change without warning. Before implementing this part of the code, I was able to easily make a direct link to one of those streams and get audio playing. Yay me. Anyways, I have a class set up, PlsParse, which connects, opens and parses the pls file, extracts the three links, and returns them to be open with MediaPlayer. This is when everything goes awry. There are no compile errors and it runs, it just crashes when I access the listen live page. Thanks for any help you may bring, and I promise to one day return the favor to a newbie and help them in their time of need. Much obliged and good night. Here is the code:
package com.comedy.app; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.LinkedList; import java.util.List; import com.comedy.app.ListenToStream.PlsParser; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.TextView; public class ListenToStream extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.audiostream); TextView artistName = (TextView) findViewById(R.id.txtArtistName); TextView albumName = (TextView) findViewById(R.id.txtAlbumName); RatingBar starRating = (RatingBar) findViewById(R.id.ratingBar1); ImageView albumPicture = (ImageView) findViewById(R.id.albumImage); MediaPlayer comedyStream = new MediaPlayer(); String urlToStream = null; try { urlToStream = new PlsParser( "http://url_to_the_pls_file.pls").toString(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } comedyStream.setAudioStreamType(AudioManager.STREAM_MUSIC); try { comedyStream.setDataSource(urlToStream); comedyStream.prepare(); // might take long! (for buffering, etc) comedyStream.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class PlsParser { private final BufferedReader reader; public PlsParser(String url) throws IOException, IOException { URLConnection urlConnection = new URL(url).openConnection(); this.reader = new BufferedReader(new InputStreamReader( urlConnection.getInputStream())); } public List<String> getUrls() { LinkedList<String> urls = new LinkedList<String>(); while (true) { try { String line = reader.readLine(); if (line == null) { break; } String url = parseLine(line); if (url != null && !url.equals("")) { urls.add(url); } } catch (IOException e) { e.printStackTrace(); } } return urls; } private String parseLine(String line) { if (line == null) { return null; } String trimmed = line.trim(); if (trimmed.indexOf("http") >= 0) { return trimmed.substring(trimmed.indexOf("http")); } return ""; } } }