获取元数据从Shoutcast的流的Android(Getting Metadata from Sh

2019-09-02 12:53发布

我正在开发一个应用程序电台为应该接受来自SHOUTcast服务器流式Android设备。

声音是工作正常,但我要检索的元数据以及因此我也许能显示当前播放的歌曲的标题,艺术家,以及相册图片。

我用了MediaMetaDataRetriever,但它似乎并没有在所有的工作!

这是我使用的代码:

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

}

谢谢你的帮助!

Answer 1:

由于在应答类@威廉 - 塞曼是不再可用。 这里有一个选择(与它不需要任何库的好处):

下面的添加类到您的项目。 现在,您可以使用它作为显示在这里 。 您也可以使用这样的:

    String title = "";
    IcyStreamMeta meta = new IcyStreamMeta();
    try {
        meta.setStreamUrl(new URL(YOURURL));
        title = meta.getStreamTitle();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

全部信贷dazza5000创建项目。



Answer 2:

我创建了自己的类来检索基础上的修改版本的元数据streamscraper 。 使用这个罐子(streamscraper), 这个罐子(jsoup)和这个类检索的元数据。 该界面类似于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);

我不相信Shoutcast一样的元数据支持专辑封面,所以你将无法检索。



文章来源: Getting Metadata from Shoutcast stream Android