I'm building a simple app for an online radio station. I using this code to use the MediaPlayer to stream the station. It works great with one problem. I don't know how to show the listener what they're actually listening too. My code:
package com.xxxx.android;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
@SuppressWarnings("unused")
public class MainActivity extends Activity implements OnClickListener {
private final static String RADIO_STATION_URL = "http://";
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
private TextView showdisplay;
private String strInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
showdisplay = (TextView) findViewById(R.id.lblDisplay);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
showdisplay.setText("Buffering..");
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource(RADIO_STATION_URL);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
How...specifically..and in great detail...do I update the Textview "showdisplay" with the artist and song title coming down from the radio stream in MediaPlayer?
Thanks
Quick research tells me that
android.media.MediaPlayer
only cares about stream data and nothing else. Therefore, if you want to also retrieve the stream's metadata (the station name, artist, song, etc.), you must do it separately, using a different object.The
android.media.MediaMetadataRetreiver
class seems to be built to do exactly that. It looks like you should research theMetadataRetreiver.extractMetadata()
function along with theMETADATA_KEY_ARTIST
andMETADATA_KEY_TITLE
keys.Here is the MediaMetadataRetreiver API. Also, look at this previous StackOverflow question.
I am not an Android developer so I cannot guarentee my code is correct but I think it looks something like this:
(Full disclosure: code modified from here)