Java阅读Icy流似乎在开始时省略了大量数据

时间:2015-04-07 17:21:36

标签: java shoutcast

当我尝试用Java读取Icy流时,一开始似乎省略了很多mp3数据,因为第一个元数据块太早了。这是我的代码:

    sock = new Socket(url.getHost(), (url.getPort() == -1 ? 80 : url.getPort()));
    OutputStream out = sock.getOutputStream();
    String req = "GET " + (url.getPath().equals("") ? "/" : url.getPath()) + " HTTP/1.0\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n";
    out.write(req.getBytes());
    BufferedInputStream buffer = new BufferedInputStream(sock.getInputStream());
    BufferedReader read = new BufferedReader(new InputStreamReader(buffer));
    String line = "";
    String beginning = "";
    while(!beginning.equals("icy-metaint:")) {
        line = read.readLine();
        try {
            beginning = line.substring(0, 12);
        } catch (StringIndexOutOfBoundsException exc) {
        }
    }
    String metaintstr = line.substring(12);
    metaint = Integer.valueOf(metaintstr);
    while (!line.equals("")) {
        line = read.readLine();
    }
    in = new MetadataInputStream(buffer, metaint);

我的MetadataInputStream代码如下:

public class MetadataInputStream extends InputStream {
private InputStream in;
private int count;
private int metaint;
private String meta;
private MetadataChangeListener listen;

public MetadataInputStream(InputStream iin, int mmetaint) {
    in = iin;
    metaint = mmetaint;
    count = 0;
}

public int read() throws IOException {
    if (count == metaint) {
        count = 0;
        int length = in.read() * 16;
        System.err.println(length);
        byte[] info = new byte[length];
        in.read(info, 0, length);
        meta = new String(info);
        if (listen != null) {
            listen.metadataChanged(meta);
        }
    }
    count++;
    int res = in.read();
    return res;
}

public String getMetaData() {
    return meta;
}

public void addMetadataListener(MetadataChangeListener llisten) {
    listen = llisten;
}

}

但是,这是最令人惊讶的#34;事情,播放流工作正常!这是怎么回事?

0 个答案:

没有答案