Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the current state of MediaPlayer in Android

Tags:

android

I am not much experienced so I copied code from somewhere; but Now I can get the state mediaplayer; All I want to do is to get the current state of mediaplayer and then show pause button when it's playing ; and play button when it's paused/buffering;

I am using shoutcast streaming in it; I want to show pause button or something loading status when it is being loaded initially (buffering); and when it is loaded and started playing the the pause button ; and when for some reason it starts buffering again it should show play button or Loading status on main activity

here is my code;

   public class Myradio extends AsyncTask implements OnBufferingUpdateListener {
    private static Context mContext;
    // private static MyProgressDialog pdialog;
    public static MediaPlayer mp = new MediaPlayer();
    private MediaPlayer mpLoop = new MediaPlayer();

    public Myradio(Context theContext) {
        mContext = theContext;
        mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
                Log.v("Buffring Update", "");
                // TODO Auto-generated method stub

            }
        });

    }

    public static void startRadio(String streamUrl) {
        mp.reset();

        mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e(getClass().getName(), "Error in MediaPlayer: (" + what
                        + ") with extra (" + extra + ")");
                return false;
            }
        });
        try {
            mp.setDataSource(streamUrl);
            mp.prepare();
            mp.start();

        } catch (IllegalArgumentException e) {

        } catch (IllegalStateException e) {

        } catch (IOException e) {

        }
    }

    public static void stopRadio() {

        mp.stop();

    }

    public static void resumeRadio() {

        // mp.();
        // mp.setLooping(false);
        // mpLoop.stop();

    }

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // TODO Auto-generated method stub
        Log.v("Buffring Update", "Buffring Update");
    }

}
like image 722
Wasif Laeeq Avatar asked Aug 01 '13 11:08

Wasif Laeeq


1 Answers

You can do this like (see here):

if(MediaPlayer.isPlaying()){
  //show the pause button
}
else{
  //show the play button
}

Check this tut too.

like image 68
g00dy Avatar answered Sep 19 '22 23:09

g00dy