1. Android Central Question's Avatar
    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.
    04-29-2018 08:08 PM
  2. ManiacJoe's Avatar
    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.
    04-30-2018 03:39 PM
  3. Rukbat's Avatar
    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.
    04-30-2018 05:00 PM

Similar Threads

  1. android 8.0 no supported by websites or cars applications
    By Bernard Keogan in forum Android 8.0 Oreo
    Replies: 3
    Last Post: 01-14-2019, 12:51 AM
  2. Does Google collect too much personal information?
    By DMP89145 in forum General News & Discussion
    Replies: 96
    Last Post: 05-20-2018, 04:27 PM
  3. how to fix no one can hear me on my android phone
    By Android Central Question in forum Ask a Question
    Replies: 2
    Last Post: 04-30-2018, 01:13 PM
  4. how to fix no one can hear me on my android phone
    By Android Central Question in forum Ask a Question
    Replies: 2
    Last Post: 04-30-2018, 01:09 PM
  5. How to sync icloud calendar events created on android phone to mac ?
    By Android Central Question in forum Ask a Question
    Replies: 0
    Last Post: 04-29-2018, 06:01 PM
LINK TO POST COPIED TO CLIPBOARD