Solved

android mediaplayer get progress info

Posted on 2012-03-19
1
681 Views
Last Modified: 2012-03-20
Hi,

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();
 
 mp.reset(); 
 mp.setDataSource(file); 
 mp.prepare();
 
 if (last_pos > 0) {
	 mp.seekTo(last_pos);
 }

 // the media could be just prepared.
 if (media.isAutoPlay()) {
	 mp.start();
 }
 
 mp.setOnPreparedListener(new OnPreparedListener() {
	@Override
	public void onPrepared(final MediaPlayer mp) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				int position;
				int duration = mp.getDuration();
				
				do {
					try {
						info.setText(app.getInfo());
						position = mp.getCurrentPosition();
						
						Thread.sleep(1000);
					} catch (Exception ex) { 
						break; 
					}
				} while (mp.isPlaying() && position <= duration); 
			}
		});
	}
});

Open in new window


any ideas?
0
Comment
Question by:svetoslavm
1 Comment
 
LVL 8

Accepted Solution

by:
santhanasamy earned 500 total points
ID: 37740840
Hi  svetoslavm,
Refer the attached code......


onCreate(){

.....................
//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);
setContentView(mProgressText);
MediaPlayer mMediaPlayer = MediaPlayer.create(mContext, R.raw.test);
mMediaPlayer.prepare();
mMediaPlayer.start();
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) {
                  do{
            
                        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();

      }
});
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
A short article about a problem I had getting the GPS LocationListener working.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now