Receiving Info of a ShoutCast stream on Android

2020-02-29 04:25发布

问题:

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.

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

The main XML is in a relative layout

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

Thanks mark :)

回答1:

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


回答2:

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