接收Android上Shoutcast的流信息 [英] Receiving Info of a ShoutCast stream on Android

查看:307
本文介绍了接收Android上Shoutcast的流信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做一个应用程序,还有我的在线电台的网站,我编码它采用Android 2.2(API 8),我已经拿到了Shoutcast的流有两个按钮的工作。

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.

下面是我的主类code:

Here is the code on my main class:

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



    }


}

所以我想知道让我怎么接收流标题,歌曲,艺术家等。并且使它看起来

So I was wondering so how do I receive the stream title,song,artist etc.. and make it appear

主要XML是在一个相对布局

The main XML is in a relative layout

谢谢,我是一个总的小白,当谈到节目。

Thanks, I am a total noob when it comes to programming.

感谢马克:)

推荐答案

我刚刚得到的元数据我自己,我基本上做了同样的东西,从:<一href="http://stackoverflow.com/questions/4911062/pulling-track-info-from-an-audio-stream-using-php/4914538#4914538">Pulling轨道信息从音频流使用PHP 。大量的数据是在头部,所以你可以用那些,但我想要的是数据流标题,所以这就是我得到了什么。

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

这篇关于接收Android上Shoutcast的流信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆