?
Solved

android mediaplayer get progress info

Posted on 2012-03-19
1
Medium Priority
?
734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
Santhana earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
For Sennheiser, comfort, quality and security are high priority areas. This paper addresses the security of Bluetooth technology and the supplementary security that Sennheiser’s Contact Center and Office (CC&O) headsets provide.  
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.
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…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

771 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