Solved

android mediaplayer get progress info

Posted on 2012-03-19
1
712 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding mobile browsers 9 84
Big gap on page with the Samsung Galaxy S7 EDGE and no other mobile device! 3 72
android location app 3 25
QQ problem 22 43
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.
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…

856 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