4

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 :)

Alex Gittemeier
  • 4,863
  • 27
  • 54

2 Answers2

9

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();
}
Community
  • 1
  • 1
Tonithy
  • 2,270
  • 1
  • 17
  • 20
  • This looks really cool Tonithy, just what i am looking for. Im new to android programming however and have a couple of questions. 1) how often will this code run? 2) i tried putting your code inside my mainactivity class, the app compiles fine, but when running it doesnt seem to work i.e its not logging anything or doing the toasts, (i've included all the relevant imports) do i need to add an intent filter in the manifest file to get it to run this code at startup? or should it just run automatically? how do i call it from my main activity? sorry im a bit confused – user280109 Oct 09 '14 at 10:24
  • 1
    @user280109 I was pretty new to Android when I wrote this as well, lol. If you'll notice, the method signature is `public void getNowPlaying(View v)` that would lead me to believe that I tied it to a button in xml with `android:onClick="getNowPlaying"` being set on the button. That way every time you click the button it would download the metadata again. If you don't wish to use a button, you can just call `getNowPlaying(null)` in your `onCreate` method (or anywhere...), the method isn't doing anything with View parameter anyway... – Tonithy Oct 09 '14 at 11:18
  • I set it up to run the getnowplaying function when a button is clicked, as you suggested. But the code seems to fail at the following line: int interval = Integer.valueOf(conn.getHeaderField("icy-metaint")); the error is "E/AndroidRuntime(2993): java.lang.NumberFormatException: Invalid int: "null" I've tried with various shoutcast streams, but they all give the same error. – user280109 Oct 09 '14 at 23:08
  • ah nevermind, i've found a much simpler solution here: http://stackoverflow.com/a/6125139/280109 – user280109 Oct 09 '14 at 23:43
  • though i can't get that solution to work either, frustrating. – user280109 Oct 10 '14 at 01:49
  • found another solution that actually works, hooray. Heres the one that worked for me, after hours of false starts and dead ends :) : http://stackoverflow.com/a/26291428/280109 – user280109 Oct 10 '14 at 03:21
0

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:

Community
  • 1
  • 1
Brad
  • 146,404
  • 44
  • 300
  • 476