Android Open Source - PlayerHater Playlist Parser






From Project

Back to project page PlayerHater.

License

The source code is released under:

Apache License

If you think the Android project PlayerHater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2013 Chris Rhoden, Rebecca Nesson, Public Radio Exchange
 * //from w ww  . ja v  a2 s.  c  o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.prx.playerhater.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;

import android.net.Uri;

public final class PlaylistParser {

  private static String[] PLS_MIME_TYPES = new String[] { "audio/scpls",
      "audio/x-scpls" };
  private static String[] M3U_MIME_TYPES = new String[] { "audio/x-mpegurl" };

  public static Uri[] parsePlaylist(Uri uri) {
    try {
      HttpClient httpclient = new DefaultHttpClient();
      HttpResponse response = httpclient.execute(new HttpHead(uri
          .toString()));
      Header contentType = response.getEntity().getContentType();
      if (contentType != null) {
        String mimeType = contentType.getValue().split(";")[0].trim();

        for (String plsMimeType : PLS_MIME_TYPES) {
          if (plsMimeType.equalsIgnoreCase(mimeType)) {
            return parsePls(uri);
          }
        }

        for (String m3uMimeType : M3U_MIME_TYPES) {
          if (m3uMimeType.equalsIgnoreCase(mimeType)) {
            return parseM3u(uri);
          }
        }
      }
    } catch (Exception e) {}
    return new Uri[] { uri };
  }

  private static Uri[] parsePls(Uri uri) {
    try {
      HttpClient httpclient = new DefaultHttpClient();
      HttpResponse response = httpclient.execute(new HttpGet(uri.toString()));
      HttpEntity entity = response.getEntity();
      InputStream inputStream = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          inputStream));
      String header = reader.readLine();
      if (header.trim().equalsIgnoreCase("[playlist]")) {
        String line;
        ArrayList<Uri> uriList = new ArrayList<Uri>();
        do {
          line = reader.readLine();
          if (line != null) {
            if (line.startsWith("File")) {
              String fileName = line.substring(
                  line.indexOf("=") + 1).trim();
              uriList.add(Uri.parse(fileName));
            }
          }
        } while (line != null);
        if (uriList.size() > 0) {
          Uri[] res = new Uri[uriList.size()];
          return uriList.toArray(res);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Uri[] { uri };
  }

  private static Uri[] parseM3u(Uri uri) {

    try {
      HttpClient httpclient = new DefaultHttpClient();
      HttpResponse response = httpclient.execute(new HttpGet(uri
          .toString()));
      HttpEntity entity = response.getEntity();
      InputStream inputStream = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          inputStream));
      String line;
      ArrayList<Uri> uriList = new ArrayList<Uri>();
      do {
        line = reader.readLine();
        if (line != null) {
          if (!line.startsWith("#")) {
            uriList.add(Uri.parse(line.trim()));
          }
        }
      } while (line != null);
      if (uriList.size() > 0) {
        Uri[] res = new Uri[uriList.size()];
        return uriList.toArray(res);
      }
    } catch (Exception e) {

    }
    return new Uri[] { uri };
  }

}




Java Source Code List

.AbstractPlugin.java
.AudioFocusPlugin.java
.BackgroundedPlugin.java
.BoundPlayerHater.java
.BroadcastReceiver.java
.ClientPlugin.java
.Config.java
.ExpandableNotificationPlugin.java
.HeadphoneButtonGestureHelper.java
.IPlayerHater.java
.LockScreenControlsPlugin.java
.Log.java
.MediaPlayerPool.java
.NotificationPlugin.java
.OnAudioFocusChangedListener.java
.PebblePlugin.java
.PlaybackService.java
.PlayerHaterClient.java
.PlayerHaterListenerPlugin.java
.PlayerHaterListener.java
.PlayerHaterPlugin.java
.PlayerHaterServer.java
.PlayerHaterService.java
.PlayerHater.java
.PlayerStateWatcher.java
.Player.java
.PlaylistParser.java
.PlaylistSupportingPlayer.java
.PluginCollection.java
.Receiver.java
.RemoteControlButtonReceiver.java
.RemoteSong.java
.ServerPlayerHater.java
.ServicePlayerHater.java
.SongHost.java
.SongQueue.java
.Song.java
.Songs.java
.StatelyPlayer.java
.SynchronousPlayer.java
.ThreadsafePlayerHater.java
.ThreadsafeServicePlayerHater.java
.TouchableNotificationPlugin.java
org.prx.playerhater.BroadcastReceiver.java
org.prx.playerhater.PlaybackService.java
org.prx.playerhater.PlayerHaterListener.java
org.prx.playerhater.PlayerHaterPlugin.java
org.prx.playerhater.PlayerHater.java
org.prx.playerhater.Song.java
org.prx.playerhater.broadcast.HeadphoneButtonGestureHelper.java
org.prx.playerhater.broadcast.OnAudioFocusChangedListener.java
org.prx.playerhater.broadcast.Receiver.java
org.prx.playerhater.broadcast.RemoteControlButtonReceiver.java
org.prx.playerhater.ipc.ClientPlugin.java
org.prx.playerhater.ipc.PlayerHaterClient.java
org.prx.playerhater.ipc.PlayerHaterServer.java
org.prx.playerhater.ipc.ServerPlayerHater.java
org.prx.playerhater.mediaplayer.MediaPlayerPool.java
org.prx.playerhater.mediaplayer.Player.java
org.prx.playerhater.mediaplayer.PlaylistSupportingPlayer.java
org.prx.playerhater.mediaplayer.StatelyPlayer.java
org.prx.playerhater.mediaplayer.SynchronousPlayer.java
org.prx.playerhater.plugins.AbstractPlugin.java
org.prx.playerhater.plugins.AudioFocusPlugin.java
org.prx.playerhater.plugins.BackgroundedPlugin.java
org.prx.playerhater.plugins.ExpandableNotificationPlugin.java
org.prx.playerhater.plugins.LockScreenControlsPlugin.java
org.prx.playerhater.plugins.NotificationPlugin.java
org.prx.playerhater.plugins.PebblePlugin.java
org.prx.playerhater.plugins.PlayerHaterListenerPlugin.java
org.prx.playerhater.plugins.PluginCollection.java
org.prx.playerhater.plugins.ScrubbableLockScreenControlsPlugin.java
org.prx.playerhater.plugins.TouchableNotificationPlugin.java
org.prx.playerhater.service.PlayerHaterService.java
org.prx.playerhater.service.PlayerStateWatcher.java
org.prx.playerhater.songs.RemoteSong.java
org.prx.playerhater.songs.SongHost.java
org.prx.playerhater.songs.SongQueue.java
org.prx.playerhater.songs.Songs.java
org.prx.playerhater.util.Config.java
org.prx.playerhater.util.IPlayerHater.java
org.prx.playerhater.util.Log.java
org.prx.playerhater.util.PlaylistParser.java
org.prx.playerhater.wrappers.BoundPlayerHater.java
org.prx.playerhater.wrappers.ServicePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafeServicePlayerHater.java