9

can anybody help me how to parse and play this .pls file in android

 [playlist]
 NumberOfEntries=1
 File1=http://stream.radiosai.net:8002/
Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
user1048958
  • 371
  • 2
  • 5
  • 18

1 Answers1

13

A quick search resulted in this very basic PlsParser from the NPR app that, by the looks of it, will simply parse the .pls file and return all embedded URLs in a list of strings. You'll probably want to take a look at that as a starting point.

After that, you should be able to feed the URL to a MediaPlayer object, although I'm not completely sure about what formats/protocols are supported, or what limitations might apply in the case of streaming. The sequence of media player calls will look somewhat like this.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://stream.radiosai.net:8002/");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare(); //also consider mp.prepareAsync().
mp.start();

Update: As far as I can tell, you can almost literally take the referenced code and put it your own use. Note that the code below is by no means complete or tested.

public class PlsParser {
        private final BufferedReader reader;

        public PlsParser(String url) {
            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 "";
        }
}

Once you have that, you can simply create a new PlsParser with the url of the .pls file and call getUrls afterwards. Each list item will be a url as found in the .pls file. In your case that'll just be http://stream.radiosai.net:8002/. As said, you can then feed this to the MediaPlayer.

IteratioN7T
  • 363
  • 8
  • 21
MH.
  • 45,303
  • 10
  • 103
  • 116
  • I'm not quite sure what part you're having trouble with? I added some code related to the actual parsing to help you get started. If that's not what you're looking for, please elaborate a bit more on what you're trying to accomplish. – MH. Nov 26 '11 at 19:57