Getting Metadata from Shoutcast stream Android

user1885868 picture user1885868 · May 10, 2013 · Viewed 8.6k times · Source

I am developing a Radio App for Android devices that is supposed to get streaming from a Shoutcast Server.

The sound is working fine but I want to retrieve the MetaData as well so I might be able to show the currently played song's Title, artist, as well as the Album Picture.

I used the MediaMetaDataRetriever but it doesn't seem to be working at all!

This is the code I used:

public class MainActivity extends FragmentActivity {

private ImageView bannerView;
private int[] pub = {R.drawable.banner, R.drawable.pubnautic, R.drawable.pubquad};
private int i = 0;

private ImageView play;
private ImageView pause;
private TextView title;
private MediaPlayer mediaPlayer;
private MediaMetadataRetriever retriever;
private String out;
private String url = "http://streamplus8.leonex.de:14910";
//adele-someonelikeyou
//Accordossie
//http://support.k-designed.net/test-z/music/adele-someonelikeyou.mp3
private ImageView albumpic; 
private byte[] image;
private boolean paused = false;

private ImageView exit;

private ImageView wish;

private ImageView reservation;

private ImageView contact;

private ImageView facebook;

private ImageView twitter;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bannerView = (ImageView) findViewById(R.id.banner);

    play = (ImageView) findViewById(R.id.play);
    pause = (ImageView) findViewById(R.id.pause);
    exit = (ImageView) findViewById(R.id.exit);
    albumpic = (ImageView) findViewById(R.id.artist);
    title = (TextView) findViewById(R.id.title);
    retriever = new MediaMetadataRetriever();
    mediaPlayer = new MediaPlayer();

    wish = (ImageView) findViewById(R.id.wish);

    reservation = (ImageView) findViewById(R.id.shop);

    contact = (ImageView) findViewById(R.id.contact);

    facebook = (ImageView) findViewById(R.id.facebook);

    twitter = (ImageView) findViewById(R.id.twitter);

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);  

    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        retriever.setDataSource(url);
        mediaPlayer.prepareAsync();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Please check your connection!", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

    play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                if(paused == true) {
                    mediaPlayer.start();
                } else {
                    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();

                        }
                    });
                }
                paused = false;
                image = retriever.getEmbeddedPicture();
                if(image != null) {
                    albumpic.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
                }
                out =  "Title: " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) + " \nArtist: " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
                title.setText(out);
                play.setVisibility(ImageView.GONE);
                pause.setVisibility(ImageView.VISIBLE);

            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    });

    pause.setClickable(true);

    pause.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pause.setVisibility(ImageView.GONE);
            play.setVisibility(ImageView.VISIBLE);
            mediaPlayer.pause();
            paused = true;
        }
    });

    wish.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent wishIntent = new Intent(MainActivity.this, Wish.class);
            startActivity(wishIntent);

        }
    });

    reservation.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent reservIntent = new Intent(MainActivity.this, Reservation.class);
            startActivity(reservIntent);
        }
    });

    contact.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent contactIntent = new Intent(MainActivity.this, Contact.class);
            startActivity(contactIntent);
        }
    });

    facebook.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent facebookIntent = new Intent(MainActivity.this, FacebookActivity.class);
            facebookIntent.putExtra("name", "Just now listening to: " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) + " by " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
            facebookIntent.putExtra("caption", "On AgadirAdventure Radio");
            facebookIntent.putExtra("description", "A Radio App for Android Devices by K-Designed");
            facebookIntent.putExtra("link", "http://www.agadiradventure.com/");
            facebookIntent.putExtra("picture", "http://agadiradventure.com/images/logo/logo1.png");
            startActivity(facebookIntent);

        }
    });

    twitter.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            initShareIntent("twi");

        }
    });

    exit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mediaPlayer.stop();
            onBackPressed();
        }
    });

    bannerView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.agadiradventure.com");
            Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(launchBrowser);
        }
    });

    Runnable r = new Runnable() {

        @Override
        public void run() {
            bannerView.setImageResource(pub[i]);
            i++;
            if(i >= pub.length) {
                i = 0;
            }
            bannerView.postDelayed(this, 3000);
        }
    }; 
    bannerView.postDelayed(r, 3000);

}

private void initShareIntent(String type) {
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                    info.activityInfo.name.toLowerCase().contains(type) ) {
                String sharedText = "Just now listening to: " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) + " by " + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) + "\nOn AgadirAdventure\nwww.agadiradventure.com";
                share.putExtra(Intent.EXTRA_TEXT, sharedText);
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return;

        startActivity(Intent.createChooser(share, "Select"));
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

Thanks for your help!

Answer

William Seemann picture William Seemann · May 10, 2013

I created my own class to retrieve the metadata based on a modified version of streamscraper. Use this jar (streamscraper), this jar (jsoup) and this class to retrieve the metadata. The interface is similar to MediaMetadataRetriever:

ShoutCastMetadataRetriever smr = new ShoutCastMetadataRetriever();
smr.setDataSource(<some url>);
String artist = smr.extractMetadata(ShoutCastMetadataRetriever.METADATA_KEY_ARTIST);
String title = smr.extractMetadata(ShoutCastMetadataRetriever.METADATA_KEY_TITLE);

I don't believe SHOUTcast metadata supports album art so you won't be able to retrieve that.