android mediaplayer buffer too large when playing a live feed.

  • Thread starter Thread starter Android Central Question
  • Start date Start date
A

Android Central Question

I have knocked up a media player that plays a live stream from a single radio station. It's simple and it functions, the trouble is sometimes it's three or four minutes behind. I'm assuming it's buffring. Any idea how i reduce this so it plays as close to realtime as possible? I know there will always be some latency but three or fout minutes is ridiculous

Here's the relevent code

public class LCRStreamClass extends Service implements MediaPlayer.OnPreparedListener
{
IBinder mBinder;
boolean mAllowRebind;
int mStartMode;
private static String ACTION_PLAY;
private static String ACTION_PREP;
private static String ACTION_STOP;
static MediaPlayer mediaPlayer = null;
static boolean bAutoStart;
static String sHost, url = "localhost:8000/";
SharedPreferences settings;
static boolean ispreped=false;

@override
public void onCreate() {
int a=4;
settings = getSharedPreferences("LCR_Stream_cofig", 0);
ACTION_PLAY = getResources().getString(R.string.ACTION_PLAY);
ACTION_PREP=getResources().getString(R.string.ACTION_PREP);
ACTION_STOP=getResources().getString(R.string.ACTION_StOP);

}
@override
public int onStartCommand(Intent intent, int flags, int startId) {
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
if (intent.getAction()!=null)
{
if (intent.getAction().equals(ACTION_PREP)) {
mediaPlayer = new MediaPlayer();
bAutoStart = settings.getBoolean("cofig_auto", false);//.putBoolean("cofig_auto",myswitch.isChecked());
sHost = settings.getString("config_host", "");
if (sHost == null)
url =getResources().getString(R.string.default_host); // your URL here
else
if (sHost.isEmpty())
url =getResources().getString(R.string.default_host); // your URL here
else
url = sHost;

try {

mediaPlayer.setDataSource(url);

}
catch (java.io.IOException e)
{

}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync(); // prepare async to not block main thread
}
if (intent.getAction().equals(ACTION_PLAY))
{

if ((mediaPlayer.isPlaying() == false) && ispreped) {
//mediaPlayer.seekTo(0);
mediaPlayer.start();
if (mediaPlayer.isPlaying())
Toast.makeText(this, "Playing Lincoln City radio", Toast.LENGTH_LONG).show();

// playButton.setText(R.string.stop_button);
// StatusView.setText("Stream Loaded");

}

}
if (intent.getAction().equals(ACTION_STOP))
{
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
ispreped=false;
intent.setAction(ACTION_PREP);
this.onStartCommand(intent,flags,startId);

}
}

}
return START_STICKY;

}
@override
public boolean stopService(Intent name) {
// TODO Auto-generated method stub

return super.stopService(name);
}

@override
public IBinder onBind(Intent intent) {
return null;
}

/** Called when all clients have unbound with unbindService() */
@override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}

/** Called when a client is binding to the service with bindService()*/
@override
public void onRebind(Intent intent) {

}
@override
public void onPrepared(MediaPlayer player)
{

ispreped=true;

// player.start();
}
@override
public void onDestroy() {

super.onDestroy();

}
}Any advice would be great. Thank you.
 
Your best bet for an answer to your programming question is to create an account here and repost your question in one of the developer forums.
 
Buffering is waiting for more content to come down the internet. If you have a 3 or 4 minute ping time, your internet connection is, for all practical purposes, useless. Even back in the days of full dial-up (one server would call the next server in line), it dodn't take that long to transfer a few dozen KB.

At a quick glance I don't see anything glaringly wrong, and I'm not about to build it to debug it, but that's what you have to do - debug it. See where it is when it's hanging there for 3-4 minutes. That should at least give you a clue of what's causing the problem.
 

Trending Posts

Forum statistics

Threads
955,947
Messages
6,966,179
Members
3,163,437
Latest member
scottdavila9