获取元数据使用IcyStreamMeta Shoutcast一样数据、IcyStreamMeta、Shoutcast

2023-09-05 08:15:24 作者:深情喂冷风

我写了Android的一个应用程序,抓取元数据从Shoutcast一样的mp3流。我使用的是pretty的漂亮级我在网上发现,我稍微修改,但我仍然有两个问题。

I am writing an app for Android that grabs meta data from SHOUTcast mp3 streams. I am using a pretty nifty class I found online that I slightly modified, but I am still having 2 problems.

1)我要不断地ping服务器更新使用的TimerTask的元数据。我不喜欢这种方式,但它是所有我能想到的。

1) I have to continuously ping the server to update the metadata using a TimerTask. I am not fond of this approach but it was all I could think of.

2)有一公吨的垃圾收集,而我的应用程序正在运行。卸下的TimerTask摆脱了垃圾回收的问题,所以我不知道如果我只是做是错误的,或者如果这是正常的。

2) There is a metric tonne of garbage collection while my app is running. Removing the TimerTask got rid of the garbage collection issue so I am not sure if I am just doing it wrong or if this is normal.

下面是我使用的类:

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;
}

}

这里是我的定时器:

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);

}

许多AP preciation的任何援助!

Much appreciation for any assistance!

推荐答案

我把它换成我的程序的IcyStreamMeta类,并正在逐渐从7.html文件,它是Shoutcast一样规范的一部分的元数据。远不如数据的使用和所有的,所以我觉得这是一个更好的选择。

I've replaced the IcyStreamMeta class in my program and am getting the meta data from the 7.html file that is a part of the SHOUTcast spec. Far less data usage and all that so I feel it is a better option.

我仍在使用一个TimerTask,这是可以接受的。几乎没有任何的GC任何更多的,我很高兴使用7.html和一点点正则表达式。 :)

I am still using the TimerTask, which is acceptable. There is practically no GC any more and I am happy with using 7.html and a little regex. :)