0

我正在使用 IcyStreamMeta.class 来检索元数据。这是 IcyStreamMeta.class

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

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

    /**
     * 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 "";

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

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

我在我的 MainActivity 中使用这个方法来获取元数据并更新文本框

private void getMeta()
    {
        Timer timer = new Timer();
        timer.schedule(new TimerTask()
        {
            public void run()
            {
                try
                {
                    IcyStreamMeta icy   = new IcyStreamMeta(new URL("http://176.28.31.39:8000"));
                    data                = icy.getArtist() + " - " + icy.getTitle();


                    runOnUiThread(new Runnable()
                    {
                         public void run()
                         {
                             textbox.setText(data);
                         }
                    });
                }
                catch (MalformedURLException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        },0,5000);
    }

对于当前的流标题+艺术家来说,它工作得非常好,但是每当歌曲完成并开始新的歌曲时,我的应用程序就会崩溃。为什么会这样?谁能帮我?

编辑:我收到此错误:

 09-23 14:57:46.095: E/AndroidRuntime(17321): FATAL EXCEPTION: Timer-0
09-23 14:57:46.095: E/AndroidRuntime(17321): java.lang.StringIndexOutOfBoundsException: length=15; regionStart=0; regionLength=-1
09-23 14:57:46.095: E/AndroidRuntime(17321):    at java.lang.String.startEndAndLength(String.java:593)
09-23 14:57:46.095: E/AndroidRuntime(17321):    at java.lang.String.substring(String.java:1474)
09-23 14:57:46.095: E/AndroidRuntime(17321):    at com.example.alifberadio.IcyStreamMeta.getArtist(IcyStreamMeta.java:38)
09-23 14:57:46.095: E/AndroidRuntime(17321):    at com.example.alifberadio.MainActivity$2.run(MainActivity.java:295)
09-23 14:57:46.095: E/AndroidRuntime(17321):    at java.util.Timer$TimerImpl.run(Timer.java:284)

它将我重定向到此代码

public String getArtist() throws IOException {
        Map<String, String> data = getMetadata();

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

        String streamTitle = data.get("StreamTitle");
        String title = streamTitle.substring(0, streamTitle.indexOf("-"));
        return title.trim();
    }
4

4 回答 4

1

附带说明一下,因为我似乎找不到 IcyStreamMeta.java 的原始主页,所以我想我会发布我修改过的部分,以摆脱那个可怕的正则表达式,如果艺术家或标题中有撇号,它就会中断..

public static Map<String, String> parseMetadata(String metaString) {
    Map<String, String> metadata = new HashMap();

    String[] metaParts = metaString.split(";");
    for (int i = 0; i < metaParts.length; i++) {
        String part = metaParts[i].substring(0, metaParts[i].indexOf("="));
        String songname = metaParts[i].substring(metaParts[i].indexOf("'")+1, metaParts[i].lastIndexOf("'"));
        metadata.put(part, songname);
    }

    return metadata;
}
于 2018-07-21T09:35:35.647 回答
0

我曾经遇到过同样的错误,它发生是因为我正在运行多个 TimerTask,并且需要在您的活动状态更改时将它们杀死,例如从 onCreate 调用的 getMeta()。

我还发现 IcyStreamMeta 在测试使用它的应用程序时在 Android 4.4(代号 KitKat)下被破坏。

于 2014-03-23T16:07:03.177 回答
0

当流中没有元数据时,您可能会遇到这个问题,因为像 In the Mood Music 这样的广播电台不会在他们的流中发送任何元数据。此外,在更改曲目时,它会在新艺术家数据之前清除元数据。所以你需要检查返回的数据是否存在。

于 2014-03-22T17:56:08.753 回答
0

从此改变

 public String getArtist() throws IOException {
    Map<String, String> data = getMetadata();

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

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

public String getTitle() throws IOException {
    Map<String, String> data = getMetadata();

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

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

对此

public String getArtist() throws IOException {
    Map<String, String> data = getMetadata();

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

    String streamTitle = data.get("StreamTitle");
    String title = streamTitle.length() > 0 ? streamTitle.substring(0, streamTitle.indexOf("-")) : " ";
    return title.trim();
}

public String getTitle() throws IOException {
    Map<String, String> data = getMetadata();

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

    String streamTitle = data.get("StreamTitle");
    String artist = streamTitle.length() > 0 ? streamTitle.substring(streamTitle.indexOf("-")+1) : " ";
    return artist.trim();
}
于 2017-04-17T00:49:02.630 回答