android mediaplayer get progress info


I am working on a media player for android.
I know that a thread should constantly run in order to take the current position of the audio.

String file = audioFile.getAbsolutePath();
 if (last_pos > 0) {

 // the media could be just prepared.
 if (media.isAutoPlay()) {
 mp.setOnPreparedListener(new OnPreparedListener() {
	public void onPrepared(final MediaPlayer mp) { Runnable() {
			public void run() {
				int position;
				int duration = mp.getDuration();
				do {
					try {
						position = mp.getCurrentPosition();
					} catch (Exception ex) { 
				} while (mp.isPlaying() && position <= duration); 

Open in new window

any ideas?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SanthanaTechnical LeadCommented:
Hi  svetoslavm,
Refer the attached code......


//I am creating mediaplayer for the mp3 resouce file from the raw folder...(Add a mp3 file into the raw folder)
mProgressText = new TextView(this);
MediaPlayer mMediaPlayer = MediaPlayer.create(mContext, R.raw.test);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener(){

      public void onPrepared(final MediaPlayer mp){
            final int lDuration=  mp.getDuration();
            AsyncTask<Void, Integer, Void> lMp3PlayTask = new AsyncTask<Void, Integer, Void> (){

                 protected void onPreExecute() {
                  mProgressText.setText("Playing Audio..");

                 protected Void doInBackground(Void... params) {
                        publishProgress(lDuration - mp.getCurrentPosition());

                       }while( mp.isPlaying() && mp.getCurrentPosition() <= lDuration);


                 protected void onProgressUpdate(Integer... progress) {

                  mProgressText.setText("Remining Time :"+progress[0]);

                 protected void onPostExecute(Void result) {

                  mProgressText.setText("Finished Playing Audio..");

               lMp3PlayTask .execute();


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.