5 votes

Récupérer les métadonnées de SHOUTcast en utilisant IcyStreamMeta

J'écris une application pour Android qui récupère les méta-données des flux mp3 SHOUTcast. J'utilise une classe assez astucieuse que j'ai trouvée en ligne et que j'ai légèrement modifiée, mais je rencontre toujours deux problèmes.

1) Je dois continuellement envoyer des pings au serveur pour mettre à jour les métadonnées à l'aide d'une TimerTask. Je n'aime pas trop cette approche, mais c'est tout ce que j'ai pu trouver.

2) Il y a une tonne métrique de collecte de déchets pendant que mon application est en cours d'exécution. La suppression de la TimerTask a permis de résoudre le problème de garbage collection. Je ne sais donc pas si je m'y prends mal ou si c'est normal.

Voici la classe que j'utilise :

public class IcyStreamMeta {
    protected URL streamUrl;
    private Map<String, String> metadata;
    private boolean isError;

public IcyStreamMeta(URL streamUrl) {
    setStreamUrl(streamUrl);

    isError = false;
}

/**
 * Get artist using stream's title
 *
 * @return String
 * @throws IOException
 */
public String getArtist() throws IOException {
    Map<String, String> data = getMetadata();

    if (!data.containsKey("StreamTitle"))
        return "";

    try {
        String streamTitle = data.get("StreamTitle");
        String title = streamTitle.substring(0, streamTitle.indexOf("-"));
        return title.trim();
    }catch (StringIndexOutOfBoundsException e) {
        return "";
    }
}

/**
 * Get title using stream's title
 *
 * @return String
 * @throws IOException
 */
public String getTitle() throws IOException {
    Map<String, String> data = getMetadata();

    if (!data.containsKey("StreamTitle"))
        return "";

    try {
        String streamTitle = data.get("StreamTitle");
        String artist = streamTitle.substring(streamTitle.indexOf("-")+1);
        return artist.trim();
    } catch (StringIndexOutOfBoundsException e) {
        return "";
    }
}

public Map<String, String> getMetadata() throws IOException {
    if (metadata == null) {
        refreshMeta();
    }

    return metadata;
}

public void refreshMeta() throws IOException {
    retreiveMetadata();
}

private void retreiveMetadata() throws IOException {
    URLConnection con = streamUrl.openConnection();
    con.setRequestProperty("Icy-MetaData", "1");
    con.setRequestProperty("Connection", "close");
    //con.setRequestProperty("Accept", null);
    con.connect();

    int metaDataOffset = 0;
    Map<String, List<String>> headers = con.getHeaderFields();
    InputStream stream = con.getInputStream();

    if (headers.containsKey("icy-metaint")) {
        // Headers are sent via HTTP
        metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
    } else {
        // Headers are sent within a stream
        StringBuilder strHeaders = new StringBuilder();
        char c;
        while ((c = (char)stream.read()) != -1) {
            strHeaders.append(c);
            if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
                // end of headers
                break;
            }
        }

        // Match headers to get metadata offset within a stream
        Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
        Matcher m = p.matcher(strHeaders.toString());
        if (m.find()) {
            metaDataOffset = Integer.parseInt(m.group(2));
        }
    }

    // In case no data was sent
    if (metaDataOffset == 0) {
        isError = true;
        return;
    }

    // Read metadata
    int b;
    int count = 0;
    int metaDataLength = 4080; // 4080 is the max length
    boolean inData = false;
    StringBuilder metaData = new StringBuilder();
    // Stream position should be either at the beginning or right after headers
    while ((b = stream.read()) != -1) {
        count++;

        // Length of the metadata
        if (count == metaDataOffset + 1) {
            metaDataLength = b * 16;
        }

        if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {              
            inData = true;
        } else {                
            inData = false;             
        }               
        if (inData) {               
            if (b != 0) {                   
                metaData.append((char)b);               
            }           
        }               
        if (count > (metaDataOffset + metaDataLength)) {
            break;
        }

    }

    // Set the data
    metadata = IcyStreamMeta.parseMetadata(metaData.toString());

    // Close
    stream.close();
}

public boolean isError() {
    return isError;
}

public URL getStreamUrl() {
    return streamUrl;
}

public void setStreamUrl(URL streamUrl) {
    this.metadata = null;
    this.streamUrl = streamUrl;
    this.isError = false;
}

public static Map<String, String> parseMetadata(String metaString) {
    Map<String, String> metadata = new HashMap<String, String>();
    String[] metaParts = metaString.split(";");
    Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
    Matcher m;
    for (int i = 0; i < metaParts.length; i++) {
        m = p.matcher(metaParts[i]);
        if (m.find()) {
            metadata.put((String)m.group(1), (String)m.group(2));
        }
    }

    return metadata;
}

}

Et voici ma minuterie :

private void getMeta() {
    timer.schedule(new TimerTask() {
        public void run() {
            try {
                icy = new IcyStreamMeta(new URL(stationUrl));

                runOnUiThread(new Runnable() {
                     public void run() {
                         try {
                             artist.setText(icy.getArtist());
                             title.setText(icy.getTitle());
                         } catch (IOException e) {
                             e.printStackTrace();
                         } catch (StringIndexOutOfBoundsException e) {
                             e.printStackTrace();
                         }
                     }
                });
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

        }
    },0,5000);

}

Merci beaucoup pour toute aide !

4voto

Karai17 Points 558

J'ai remplacé la classe IcyStreamMeta dans mon programme et je récupère les métadonnées à partir du fichier 7.html qui fait partie de la spécification SHOUTcast. L'utilisation des données est beaucoup moins importante et je pense que c'est une meilleure option.

J'utilise toujours la TimerTask, ce qui est acceptable. Il n'y a pratiquement plus de GC et je me contente d'utiliser 7.html et un peu de regex :)

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X