我正在为Android设备开发一个Radio应用程序,它应该从Shoutcast服务器获得流媒体。
声音工作正常,但我也要检索MetaData,这样我就可以显示当前播放的歌曲的标题,艺术家以及相册图片。
我使用了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;
}
}
感谢您的帮助!
答案 0 :(得分:6)
我创建了自己的类,以根据streamscraper的修改版本检索元数据。使用this jar(streamscraper),this jar(jsoup)和this类来检索元数据。该接口类似于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元数据支持专辑封面,因此您将无法检索它。
答案 1 :(得分:4)
由于@William-Seemann的答案中的课程不再可用。这是另一种选择(其好处是它不需要任何库):
将以下class添加到您的项目中。您现在可以按照here所示使用它。您也可以这样使用它:
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完全归功于创建项目。