Receiving Info of a ShoutCast stream on Android

前端 未结 2 1098
暗喜
暗喜 2021-01-03 08:34

I am currently making an app to go with my online radio site, I am coding it with Android 2.2 (API 8) and I have got the Shoutcast Stream working with two buttons.

H

相关标签:
2条回答
  • 2021-01-03 08:50

    What you're using to play the stream has no knowledge of (and doesn't care about) the metadata. You're going to have to deal with that separately.

    See these posts for something you can easily adapt to Android:

    • Pulling Track Info From an Audio Stream Using PHP
    • http://www.smackfu.com/stuff/programming/shoutcast.html
    0 讨论(0)
  • 2021-01-03 09:10

    I just had to get meta data myself, I basically did the same stuff from: Pulling Track Info From an Audio Stream Using PHP. A lot of data is in the headers so you can use those, but all I wanted was the Stream Title, so thats what I got.

    Activity mainAct = this;
    public void getNowPlaying(View v) {
        Log.w("getNowPlaying", "fired");
        new Thread(new Runnable() {
            public void run() {             
                String title = null, djName = null;
                try {
                    URL updateURL = new URL(YOUR_STREAM_URL_HERE);
                    URLConnection conn = updateURL.openConnection();
                    conn.setRequestProperty("Icy-MetaData", "1");
                    int interval = Integer.valueOf(conn.getHeaderField("icy-metaint")); // You can get more headers if you wish. There is other useful data.
    
                    InputStream is = conn.getInputStream();
    
                    int skipped = 0;
                    while (skipped < interval) {
                        skipped += is.skip(interval - skipped);
                    }
    
                    int metadataLength = is.read() * 16;
    
                    int bytesRead = 0;
                    int offset = 0;
                    byte[] bytes = new byte[metadataLength];
    
                    while (bytesRead < metadataLength && bytesRead != -1) {
                        bytesRead = is.read(bytes, offset, metadataLength);
                        offset = bytesRead;
                    }
    
                    String metaData = new String(bytes).trim();
                    title = metaData.substring(metaData.indexOf("StreamTitle='") + 13, metaData.indexOf(" / ", metaData.indexOf("StreamTitle='"))).trim();
                    djName = metaData.substring(metaData.indexOf(" / ", metaData.indexOf("StreamTitle='")) + 3, metaData.indexOf("';", metaData.indexOf("StreamTitle='"))).trim();
                    Log.w("metadata", metaData);
                    is.close();
                } catch (MalformedURLException e) { e.printStackTrace();
                } catch (IOException e) { e.printStackTrace(); }
    
                final String titleFin = title;
                final String djNameFin = djName;
                mainAct.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(mainAct, titleFin + "\n" + djNameFin, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
    }
    
    0 讨论(0)
提交回复
热议问题