在Android上接收ShoutCast流的信息

时间:2012-01-14 20:39:12

标签: android metadata shoutcast

我目前正在制作一个应用程序来使用我的在线广播网站,我正在使用Android 2.2(API 8)进行编码,并且我使用了两个按钮来使用Shoutcast Stream。

以下是我主要课程的代码:

 public class GrooveOfMusicRadioActivity extends Activity {
      /** Called when the activity is first created. */


     MediaPlayer mediaPlayer;
     Button start, stop;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        start = (Button) findViewById(R.id.button1);
        stop = (Button) findViewById(R.id.button2);




        start.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mediaPlayer.start();

            }
        });
        stop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mediaPlayer.pause();
            }
        });





        String url = "http://67.212.165.106:8161"; // your URL here
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

        try {
            mediaPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }


}

所以我想知道如何收到流媒体标题,歌曲,艺术家等......并使其显示

主XML采用相对布局

谢谢,在编程时我是一个完全的菜鸟。

谢谢标记:)

2 个答案:

答案 0 :(得分:8)

我必须自己获取元数据,我基本上做了同样的事情:Pulling Track Info From an Audio Stream Using PHP。标题中有很多数据,所以你可以使用它们,但我想要的只是流标题,这就是我得到的。

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

答案 1 :(得分:0)

您用来播放流的内容并不了解(也不关心)元数据。你将不得不单独处理它。

有关您可以轻松适应Android的内容,请参阅这些帖子: