4

我目前正在制作一个与我的在线广播网站一起使用的应用程序,我正在使用 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 处于相对布局中

谢谢,在编程方面我是个菜鸟。

谢谢马克:)

4

2 回答 2

9

我只需要自己获取元数据,我基本上做了同样的事情: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();
}
于 2013-07-27T14:01:19.493 回答
0

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

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

于 2012-01-15T15:27:33.500 回答