java – 使用IcyStreamMeta从SHOUTcast获取元数据

我正在为Android编写一个应用程序,用于从SHOUTcast mp3流中获取元数据.我正在使用我在网上找到的一个非常漂亮的课程,我稍微修改了一下,但我仍然有两个问题.

1)我必须不断ping服务器以使用TimerTask更新元数据.我不喜欢这种方法,但这是我能想到的.

2)当我的应用程序运行时,有一吨公吨的垃圾收集.删除TimerTask摆脱了垃圾收集问题所以我不确定我是做错了还是这是正常的.

这是我正在使用的课程:

public class IcyStreamMeta {
    protected URL streamUrl;
    private MapMetadata;
    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 {
    MapMetadata();

    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 {
    MapMetadata();

    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 MapMetadata() 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;
    MapMetaint")) {
        // 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 MapMetadata(String MetaString) {
    MapMetadata = new HashMapMetaParts = 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();
            }

        }
    },5000);

}

非常感谢任何帮助!

最佳答案
我已经在我的程序中替换了IcyStreamMeta类,并从7.html文件获取元数据,该文件是SHOUTcast规范的一部分.更少的数据使用和所有这些,所以我觉得这是一个更好的选择.

我仍在使用TimerTask,这是可以接受的.实际上几乎没有GC了,我很高兴使用7.html和一点regex.

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...