Solved

android mediaplayer get progress info

Posted on 2012-03-19
1
702 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:
Santhana 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A brand new malware strain was recently discovered by security researchers at Palo Alto Networks dubbed “AceDeceiver.” This new strain of iOS malware can successfully infect non-jailbroken devices and jailbroken devices alike.
With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices. Find out how to create a mobile email signature with this guide.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

785 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